Getting Started
Objects and Lists
Python uses dictionaries and lists to organize and manage collections of data through key-value pairs and ordered sequences.
Dictionaries
A dictionary is a collection of key-value pairs. It is used to store structured data.
Creating a Dictionary
Dictionaries are created using curly braces {}
with keys and values separated by colons.
pythonCopyEditperson = { "name": "Alice", "age": 30 }
Accessing Dictionary Values
Values can be accessed using square brackets or the get()
method.
pythonCopyEditprint(person["name"]) # Dot notation is not used in Python print(person.get("age")) # Safe access, returns None if key doesn’t exist
Modifying Dictionary Values
You can update existing properties or add new ones dynamically.
pythonCopyEditperson["age"] = 31 # Modify existing value person["city"] = "London" # Add new property
Deleting Dictionary Entries
Use the del
keyword to remove a key-value pair.
pythonCopyEditdel person["age"]
Iterating Over Dictionaries
Use a for
loop to iterate over keys and values.
pythonCopyEditfor key, value in person.items(): print(f"{key}: {value}")
Lists in Python
A list is an ordered collection of values.
Creating a List
pythonCopyEditfruits = ["apple", "banana", "cherry"]
Adding and Removing Elements
append(value)
– Adds to the endpop()
– Removes the last iteminsert(index, value)
– Adds at specific positionremove(value)
– Removes first matching value
pythonCopyEditfruits.append("orange") fruits.pop() fruits.insert(0, "grape") fruits.remove("banana")
Nested Dictionaries and Lists
Dictionaries can contain lists, and lists can contain dictionaries.
pythonCopyEdituser = { "name": "Alice", "hobbies": ["reading", "cycling"] } users = [ {"name": "Bob", "age": 25}, {"name": "Eve", "age": 28} ]
Conclusion
Dictionaries and lists are essential for managing and structuring data in Python. They are widely used for everything from simple data storage to complex data modeling. In the next section, we’ll explore modern Python features, including comprehensions, unpacking, and f-strings, which enhance productivity and readability.
Dictionaries
A dictionary is a collection of key-value pairs. It is used to store structured data.
Creating a Dictionary
Dictionaries are created using curly braces {}
with keys and values separated by colons.
pythonCopyEditperson = { "name": "Alice", "age": 30 }
Accessing Dictionary Values
Values can be accessed using square brackets or the get()
method.
pythonCopyEditprint(person["name"]) # Dot notation is not used in Python print(person.get("age")) # Safe access, returns None if key doesn’t exist
Modifying Dictionary Values
You can update existing properties or add new ones dynamically.
pythonCopyEditperson["age"] = 31 # Modify existing value person["city"] = "London" # Add new property
Deleting Dictionary Entries
Use the del
keyword to remove a key-value pair.
pythonCopyEditdel person["age"]
Iterating Over Dictionaries
Use a for
loop to iterate over keys and values.
pythonCopyEditfor key, value in person.items(): print(f"{key}: {value}")
Lists in Python
A list is an ordered collection of values.
Creating a List
pythonCopyEditfruits = ["apple", "banana", "cherry"]
Adding and Removing Elements
append(value)
– Adds to the endpop()
– Removes the last iteminsert(index, value)
– Adds at specific positionremove(value)
– Removes first matching value
pythonCopyEditfruits.append("orange") fruits.pop() fruits.insert(0, "grape") fruits.remove("banana")
Nested Dictionaries and Lists
Dictionaries can contain lists, and lists can contain dictionaries.
pythonCopyEdituser = { "name": "Alice", "hobbies": ["reading", "cycling"] } users = [ {"name": "Bob", "age": 25}, {"name": "Eve", "age": 28} ]
Conclusion
Dictionaries and lists are essential for managing and structuring data in Python. They are widely used for everything from simple data storage to complex data modeling. In the next section, we’ll explore modern Python features, including comprehensions, unpacking, and f-strings, which enhance productivity and readability.
Dictionaries
A dictionary is a collection of key-value pairs. It is used to store structured data.
Creating a Dictionary
Dictionaries are created using curly braces {}
with keys and values separated by colons.
pythonCopyEditperson = { "name": "Alice", "age": 30 }
Accessing Dictionary Values
Values can be accessed using square brackets or the get()
method.
pythonCopyEditprint(person["name"]) # Dot notation is not used in Python print(person.get("age")) # Safe access, returns None if key doesn’t exist
Modifying Dictionary Values
You can update existing properties or add new ones dynamically.
pythonCopyEditperson["age"] = 31 # Modify existing value person["city"] = "London" # Add new property
Deleting Dictionary Entries
Use the del
keyword to remove a key-value pair.
pythonCopyEditdel person["age"]
Iterating Over Dictionaries
Use a for
loop to iterate over keys and values.
pythonCopyEditfor key, value in person.items(): print(f"{key}: {value}")
Lists in Python
A list is an ordered collection of values.
Creating a List
pythonCopyEditfruits = ["apple", "banana", "cherry"]
Adding and Removing Elements
append(value)
– Adds to the endpop()
– Removes the last iteminsert(index, value)
– Adds at specific positionremove(value)
– Removes first matching value
pythonCopyEditfruits.append("orange") fruits.pop() fruits.insert(0, "grape") fruits.remove("banana")
Nested Dictionaries and Lists
Dictionaries can contain lists, and lists can contain dictionaries.
pythonCopyEdituser = { "name": "Alice", "hobbies": ["reading", "cycling"] } users = [ {"name": "Bob", "age": 25}, {"name": "Eve", "age": 28} ]
Conclusion
Dictionaries and lists are essential for managing and structuring data in Python. They are widely used for everything from simple data storage to complex data modeling. In the next section, we’ll explore modern Python features, including comprehensions, unpacking, and f-strings, which enhance productivity and readability.