Skip to main content

Chapter 2 - Python

 

LIST

In Python, a list is a built-in data type that represents an ordered sequence of elements. Lists are versatile and can contain elements of different data types, including numbers, strings, and even other lists. Here are some basic operations you can perform on lists:

1. Creating a List:

python
my_list = [1, 2, 3, "apple", "banana"]

2. Accessing Elements:

You can access elements in a list using their indices. Indices start from 0.

python
first_element = my_list[0] # Access the first element second_element = my_list[1] # Access the second element last_element = my_list[-1] # Access the last element

3. Slicing:

You can extract a subset of a list using slicing.

python
subset = my_list[1:4] # Extract elements from index 1 to 3 (excluding 4)

4. Appending Elements:

You can add elements to the end of a list using the append() method.

python
my_list.append("orange")

5. Inserting Elements:

You can insert an element at a specific position using the insert() method.

python
my_list.insert(1, "grape") # Insert "grape" at index 1

6. Removing Elements:

You can remove elements by value using the remove() method or by index using the pop() method.

python
my_list.remove("banana") # Remove the element with the value "banana" popped_element = my_list.pop(2) # Remove and return the element at index 2

7. Updating Elements:

You can update the value of an element by assigning a new value.

python
my_list[0] = 100 # Update the value at index 0 to 100

8. Length of a List:

You can get the length of a list using the len() function.

python
length = len(my_list)

9. Concatenating Lists:

You can concatenate two lists using the + operator.

python
new_list = my_list + [5, 6, 7]

10. Checking Membership:

You can check if an element is present in a list using the in keyword.

python
is_present = "apple" in my_list

11. Iterating Over a List:

You can iterate over the elements of a list using a for loop.

python
for item in my_list: print(item)

These are some of the basic operations you can perform on lists in Python. Lists are widely used for storing and manipulating data in various programming scenarios.




Dictionary


In Python, a dictionary is a built-in data type that represents an unordered collection of key-value pairs. Each key in a dictionary must be unique, and it is associated with a specific value. Dictionaries are defined by curly braces {}. Here are some basic operations you can perform on dictionaries:

1. Creating a Dictionary:

python
my_dict = {"name": "John", "age": 25, "city": "New York"}

2. Accessing Values:

You can access the value associated with a key using square brackets [].

python
name = my_dict["name"] age = my_dict["age"]

3. Updating Values:

You can update the value associated with a key.

python
my_dict["age"] = 26

4. Adding New Key-Value Pairs:

You can add new key-value pairs to a dictionary.

python
my_dict["occupation"] = "Engineer"

5. Removing Key-Value Pairs:

You can remove a key-value pair using the del keyword or the pop() method.

python
del my_dict["city"] # Remove the key-value pair with key "city" removed_value = my_dict.pop("age") # Remove and return the value for the key "age"

6. Checking Membership:

You can check if a key is present in a dictionary using the in keyword.

python
is_present = "age" in my_dict

7. Getting Keys and Values:

You can get a list of all keys or values using the keys() and values() methods, respectively.

python
all_keys = my_dict.keys() all_values = my_dict.values()

8. Getting Key-Value Pairs:

You can get a list of all key-value pairs using the items() method.

python
all_items = my_dict.items()

9. Iterating Over a Dictionary:

You can iterate over the keys, values, or items of a dictionary using a for loop.

python
# Iterate over keys for key in my_dict: print(key) # Iterate over values for value in my_dict.values(): print(value) # Iterate over key-value pairs for key, value in my_dict.items(): print(key, value)

10. Length of a Dictionary:

You can get the number of key-value pairs in a dictionary using the len() function.

python
length = len(my_dict)

Dictionaries are commonly used for representing structured data and are suitable for scenarios where you need to quickly look up values based on unique keys.

Comments

Popular posts from this blog

PIANO MUSIC THEORY

  Time signature The time signature of a piece of music indicates how many beats are in each bar. A time signature allows a musician to count a steady beat while playing a piece. The time signature is written at the beginning of the  staff . It comes after the  clef  and key signatures. You may find certain pieces of music have include changes to different time signatures. This will be marked on the sheet music, so always check through a piece of music to ensure you are aware of any changes of time signature it might have. Metronome mark A composer may include a  metronome  mark to indicate the  tempo  - how fast or slow the music should be played. For example, the metronome mark above tells you there are 80 crotchet beats per minute. Key signatures The key signature tells you which notes should be played as  sharps  or  flats  throughout a piece of music and therefore what key the piece should be played in. The examples above ...

KeyNote Tutorial - Self Reference

 1. How to edit the slide layout (like SlideMaster in PPT) 2. Change the slide to 16:9 3. Help on any topic 4. Border of a shape 5. Changing to Ink Style 6. Change the Point appropriately to make the size bigger 7. Change line color 8. Copy Style 9. Animation (Magic Move) 10. Animation Line Draw 11. Copy and Paste Animation 12.  Ordering the line draw 13. When the transition should happen can be changed 14. CMD and + for increasing text size 15. CMD + SHIFT + . (or ,) for resizing the slide 16. Hand Written Text 17. Remove the cursor 18. BLACKBOARD EFFECT EXAMPLE Change the slide layout 19. Duplicate the slide 20. Rename to Blackboard 21. Choose Advanced Image fill 22. Change the color to your choice for Blackboard 23. Exit Slide Layout 24. Change slide layout to Blackboard 25. Change line style and line color ' 26. Change Text Color 27. Record Voice 28. Delete Recording (bottom Right) 29. Continue Recording from a specific point 28. Export CUSTOM SHAPES SAVING 1. Custom Shape...