← Back to Hub
Module 4 Week 12 • Storing data using keys, immutability, mapping, and items checks

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 del keyword 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?

Explanation: Lists are mutable and can be changed in-place; tuples are immutable and cannot be changed after definition.

Question 2: Which syntax character is used to declare a tuple?

Explanation: Tuples are declared using parentheses ().

Question 3: What exception is raised when trying to run coords[0] = 5 on a tuple?

Explanation: Mutating an immutable tuple object raises a TypeError.

Question 4: What is tuple unpacking?

Explanation: Tuple unpacking assigns each element of a tuple to a corresponding variable.

Question 5: What is a dictionary in Python?

Explanation: A dictionary is a collection of key-value pairs, where each key maps to a value.

Question 6: Which syntax character is used to declare a dictionary?

Explanation: Dictionaries are declared using curly braces {}.

Question 7: How do you access the value associated with the key 'age' in dict student?

Explanation: You access dictionary values using square brackets containing the key: student['age'].

Question 8: What happens if you reference a dictionary key that does not exist (e.g., student['email'])?

Explanation: Accessing a non-existent key raises a KeyError.

Question 9: Which method returns all key-value pairs in a dictionary?

Explanation: The .items() method returns a sequence of key-value tuples, which is useful for loops.

Question 10: How do you delete a key-value pair from a dictionary?

Explanation: Use the del keyword followed by the dictionary key reference to delete the key-value pair.

🖥️ Output Prediction Challenges

Read each code snippet carefully, analyze its execution flow, and predict the exact string result written to the console.

Challenge 1 of 5

Predict the output of the code snippet below. Type the exact output in the console.

coords = (1, 2)
print(coords[1])
Console ready. Type your prediction below...
Challenge 2 of 5

Predict the output of the code snippet below. Type the exact output in the console.

coords = (5, 10)
x, y = coords
print(x + y)
Console ready. Type your prediction below...
Challenge 3 of 5

Predict the output of the code snippet below. Type the exact output in the console.

d = {"a": 1, "b": 2}
print(d["a"])
Console ready. Type your prediction below...
Challenge 4 of 5

Predict the output of the code snippet below. Type the exact output in the console.

d = {"x": 5}
d["x"] = 10
print(d["x"])
Console ready. Type your prediction below...
Challenge 5 of 5

Predict the output of the code snippet below. Type the exact output in the console.

d = {"a": 1}
d["b"] = 2
print(len(d))
Console ready. Type your prediction below...

🎮 Interactive Code Playground

Type your own Python code below and click "Run Code" to execute it instantly in your browser!

📝 Python Code Editor
Python WebAssembly runtime is unloaded. Click 'Run Code' to load.
🖥️ Output Console
Console ready.
💬 Teacher Consultation

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

Lesson Complete?

📌 Check the box above to mark this weekend's lesson complete. Your dashboard status ring will update automatically.