📝 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])
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"])
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.
✅ True or False
22. You can change the values in a dictionary after it's created.
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.
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.
Correct! True! Each key in a dictionary must be unique, just like each contact in your phone can only have one entry.