📚 Chapter 8: Lists & Dictionaries

Test your understanding of Python's organizational superpowers!

📝 Lists: Your Digital Shopping List

1. What are lists best used for in Python?

  • Storing a single piece of information
  • Storing ordered collections of items
  • Making decisions in code
  • Creating loops
Correct! Lists are perfect for storing ordered collections of items, like a shopping list where order matters or a sequence of steps.

2. How do you create an empty list in Python?

  • list = ()
  • list = []
  • list = {}
  • list = ""
Correct! Square brackets [] create a list. Empty square brackets create an empty list, ready to have items added to it.

3. What's unusual about Python's list indexing?

  • It starts counting from 1
  • It starts counting from 0
  • It counts backwards
  • It uses letters instead of numbers
Correct! Python starts counting from 0, so the first item is at position 0, second at position 1, and so on. Yes, it's weird, but you'll get used to it!

4. What does the .append() method do?

  • Removes the last item from a list
  • Adds an item to the end of a list
  • Sorts the list alphabetically
  • Counts the items in a list
Correct! .append() adds a new item to the end of your list, like writing "chocolate" at the bottom of your grocery list.

5. What will this code output?

grocery_list = ["milk", "eggs", "bread"] print(grocery_list[1])
  • milk
  • eggs
  • bread
  • An error
Correct! Since Python starts counting from 0, position [1] is the second item, which is "eggs".
🛠️ List Operations

6. How do you find out how many items are in a list?

  • list.count()
  • len(list)
  • list.size()
  • list.length()
Correct! len() tells you how many items are in your list. Very handy when you need to know if your to-do list is getting out of hand!

7. What happens when you use .remove() on a list?

  • It removes the first item
  • It removes the last item
  • It removes the first occurrence of a specified value
  • It removes all items
Correct! .remove() finds the first occurrence of the value you specify and removes it, like saying "remove milk from my shopping list".

8. What's the difference between .remove() and del?

  • There's no difference
  • .remove() removes by value, del removes by position
  • del removes by value, .remove() removes by position
  • del is faster than .remove()
Correct! .remove() says "remove Ben" (by value) while del says "remove whoever's first" (by position).
🔒 Tuples: Fixed Collections

9. What's the main difference between lists and tuples?

  • Tuples are faster than lists
  • Tuples can't be changed after creation
  • Lists can store more items
  • Tuples use square brackets
Correct! Tuples are immutable - once created, they can't be changed. Think of them as writing your list in permanent marker instead of pencil.

10. How do you create a tuple?

  • Using square brackets []
  • Using parentheses ()
  • Using curly braces {}
  • Using quotation marks ""
Correct! Tuples use parentheses (), while lists use square brackets [] and dictionaries use curly braces {}.

11. When should you use a tuple instead of a list?

  • When you need to store a lot of data
  • When the data should not be modified
  • When you need to sort the data
  • When you need to add items frequently
Correct! Use tuples when the data should not be modified, like days of the week or coordinates. They're the "safety lock" version of lists.
📖 Dictionaries: Key-Value Pairs

12. What's the best analogy for a dictionary?

  • A shopping list
  • A contact book or filing system
  • A math calculator
  • A clock
Correct! Dictionaries are like contact books - you access information by meaningful names (keys) rather than positions.

13. How do you create a dictionary in Python?

  • Using square brackets []
  • Using parentheses ()
  • Using curly braces {}
  • Using quotation marks ""
Correct! Dictionaries use curly braces {} to group key-value pairs together.

14. What will this code output?

person = {"name": "Alex", "age": 30} print(person["name"])
  • Alex
  • name
  • 30
  • An error
Correct! person["name"] accesses the value associated with the key "name", which is "Alex".

15. What's the advantage of using person["name"] over person[0]?

  • It's faster
  • It's more readable and self-documenting
  • It uses less memory
  • It prevents errors
Correct! person["name"] immediately tells you what you're getting, while person[0] requires you to remember what's at position 0.
🔧 Dictionary Operations

16. How do you check if a key exists in a dictionary before accessing it?

  • if key exists in dict:
  • if key in dict:
  • if dict.has(key):
  • if dict[key] != None:
Correct! Use "if key in dict:" to check if a key exists before accessing it, like checking if a contact card has an email field.

17. What does the .get() method do?

  • Always returns the value for a key
  • Returns the value if the key exists, or None if it doesn't
  • Adds a new key-value pair
  • Removes a key-value pair
Correct! .get() is like a polite assistant - it gives you the value if it exists, or returns None instead of causing an error.

18. What happens if you try to access a dictionary key that doesn't exist?

  • It returns None
  • It returns an empty string
  • It causes a KeyError
  • It creates the key automatically
Correct! Python raises a KeyError, basically saying "Hey, you asked for a key that doesn't exist!" This is why checking with 'in' or using .get() is safer.
🎯 Practical Applications

19. Which is better for storing customer information?

Scenario: You need to store customer details like name, phone, email, and city.

  • A list: ["John", "123-456-7890", "john@email.com", "Boston"]
  • A dictionary: {"name": "John", "phone": "123-456-7890", "email": "john@email.com", "city": "Boston"}
  • Separate variables for each piece of information
  • A tuple: ("John", "123-456-7890", "john@email.com", "Boston")
Correct! A dictionary is perfect because it clearly labels each piece of information and allows easy access by meaningful names.

20. What's the best way to store a product inventory?

Scenario: You have multiple products, each with name, price, and stock quantity.

  • Three separate lists: names, prices, stock
  • A list of dictionaries
  • A dictionary of lists
  • One big dictionary
Correct! A list of dictionaries is perfect - each product is a dictionary with its details, and all products are in an ordered list.
✏️ Fill in the Blanks

21. Complete this contact management code:

contacts = [] new_contact = "name" "Alice", "phone" "555-1234" contacts(new_contact)

Fill in: The symbols and method needed to create a dictionary and add it to a list.

Correct answers: "{", ":", ":", "}", ".append" - This creates a dictionary with key-value pairs and adds it to the contacts list.
✅ True or False

22. You can change the values in a dictionary after it's created.

  • True
  • False
Correct! True! Unlike tuples, dictionaries are mutable - you can add, update, or remove key-value pairs.

23. Lists and dictionaries can both store multiple types of data.

  • True
  • False
Correct! True! Both lists and dictionaries can store strings, numbers, other lists, dictionaries, and more - all in the same collection.

24. Dictionary keys must be unique.

  • True
  • False
Correct! True! Each key in a dictionary must be unique, just like each contact in your phone can only have one entry.

🎉 Quiz Complete!

Your Score: 0/24

Keep practicing!