Tuples & Dictionaries
Explore immutable tuples and relational dictionaries for storing and mapping structured datasets.
1. Tuples
Tuples are ordered collections of items, written with parentheses (). The key difference is that tuples are immutable: once declared, their values and size cannot be modified, added, or removed. Attempting to do so raises a TypeError.
2. Dictionaries
A dictionary is an unordered collection of key-value pairs, written with curly braces {}. Each key must be unique, and it maps to a value (which can be of any data type):
- Keys are usually strings or integers.
- Values are accessed using square brackets containing the key:
dict[key]. - You can add or update key-value pairs using assignment:
dict[key] = value. - Use the
delkeyword to delete keys:del dict[key].
💻 Python Code Examples
Example 1: Declaring a Tuple
coords = (10.5, 20.3)
print(coords)
print(coords[0])
Description: Declares a tuple to store coordinate coordinates in RAM.
Example 2: Tuple Unpacking
point = (5, 10)
x, y = point
print(x)
print(y)
Description: Unpacks tuple values directly into separate variables.
Example 3: Declaring Dictionaries
student = {
"name": "Peter",
"age": 22,
"course": "Python"
}
print(student["name"])
Description: Creates a dictionary storing student details and accesses name key.
Example 4: Adding and Mutating Dictionary Keys
student = {"name": "Peter"}
student["age"] = 22
student["name"] = "Ann"
print(student)
Description: Adds a new key-value pair and updates an existing one.
Example 5: Deleting Dictionary Keys
student = {"name": "Ann", "age": 22}
del student["age"]
print(student)
Description: Deletes the key 'age' from the dictionary student in-place.
Example 6: Iterating Dictionary Items
student = {"name": "Peter", "age": 22}
for key, val in student.items():
print(f"{key}: {val}")
Description: Loops through all key-value pairs in a dictionary using the .items() method.
🧠 Active Retrieval Quizzes
Answer the following 10 multiple-choice questions to test your storage strength.
Question 1: What is the key difference between lists and tuples?
Question 2: Which syntax character is used to declare a tuple?
Question 3: What exception is raised when trying to run coords[0] = 5 on a tuple?
Question 4: What is tuple unpacking?
Question 5: What is a dictionary in Python?
Question 6: Which syntax character is used to declare a dictionary?
Question 7: How do you access the value associated with the key 'age' in dict student?
Question 8: What happens if you reference a dictionary key that does not exist (e.g., student['email'])?
Question 9: Which method returns all key-value pairs in a dictionary?
Question 10: How do you delete a key-value pair from a dictionary?
🖥️ Output Prediction Challenges
Read each code snippet carefully, analyze its execution flow, and predict the exact string result written to the console.
Predict the output of the code snippet below. Type the exact output in the console.
coords = (1, 2)
print(coords[1])
Predict the output of the code snippet below. Type the exact output in the console.
coords = (5, 10)
x, y = coords
print(x + y)
Predict the output of the code snippet below. Type the exact output in the console.
d = {"a": 1, "b": 2}
print(d["a"])
Predict the output of the code snippet below. Type the exact output in the console.
d = {"x": 5}
d["x"] = 10
print(d["x"])
Predict the output of the code snippet below. Type the exact output in the console.
d = {"a": 1}
d["b"] = 2
print(len(d))
🎮 Interactive Code Playground
Type your own Python code below and click "Run Code" to execute it instantly in your browser!
Console ready.
Stuck on any concept, or want to explore an edge case? Don't hesitate to ask follow-up questions to your instructor! Active questioning helps build storage strength.
📋 Progress Status
📌 Check the box above to mark this weekend's lesson complete. Your dashboard status ring will update automatically.