Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why Lists Are Used
๐ Scenario: Imagine you are organizing a grocery shopping list. You want to keep track of all the items you need to buy in one place. Using a list helps you do this easily.
๐ฏ Goal: You will create a list of grocery items, add a new item, and then print the full list to see all your shopping needs.
๐ What You'll Learn
Create a list called groceries with these exact items: 'milk', 'eggs', 'bread'
Add the item 'apples' to the groceries list
Print the groceries list to show all items
๐ก Why This Matters
๐ Real World
Lists are used everywhere to keep groups of things together, like shopping lists, to-do lists, or storing names.
๐ผ Career
Understanding lists is important for any programming job because they help manage collections of data efficiently.
Progress0 / 4 steps
1
Create the grocery list
Create a list called groceries with these exact items: 'milk', 'eggs', 'bread'
Python
Hint
Use square brackets [] to create a list and separate items with commas.
2
Add an item to the list
Add the item 'apples' to the groceries list using the append() method
Python
Hint
Use groceries.append('apples') to add an item to the list.
3
Print the grocery list
Print the groceries list to show all items
Python
Hint
Use print(groceries) to display the list.
4
Explain why lists are useful
Create a variable called reason and set it to the string explaining why lists are useful: 'Lists help us keep multiple items together in order and allow easy adding or removing of items.' Then print the reason variable
Python
Hint
Use reason = '...' to store the explanation and print(reason) to show it.
Practice
(1/5)
1. Why do we use lists in Python?
easy
A. To store multiple items in one variable
B. To store only one item at a time
C. To perform mathematical calculations only
D. To create functions
Solution
Step 1: Understand the purpose of lists
Lists are designed to hold many items together in one place.
Step 2: Compare options with list features
Only To store multiple items in one variable correctly states that lists store multiple items in one variable.
Final Answer:
To store multiple items in one variable -> Option A
Quick Check:
Lists hold many items = To store multiple items in one variable [OK]
Hint: Lists hold many items together in one variable [OK]
Common Mistakes:
Thinking lists store only one item
Confusing lists with functions
Assuming lists are for math only
2. Which of the following is the correct way to create a list in Python?
easy
A. my_list = [1, 2, 3]
B. my_list = (1, 2, 3)
C. my_list = {1, 2, 3}
D. my_list = <1, 2, 3>
Solution
Step 1: Recall list syntax in Python
Lists are created using square brackets [] around items.
Step 2: Match options with correct syntax
my_list = [1, 2, 3] uses square brackets, so it is correct. Others use parentheses, curly braces, or angle brackets which are incorrect for lists.
Final Answer:
my_list = [1, 2, 3] -> Option A
Quick Check:
Lists use [] brackets = my_list = [1, 2, 3] [OK]
Hint: Lists use square brackets [] to hold items [OK]
Python lists start indexing at 0, so fruits[1] is the second item.
Step 2: Identify the item at index 1
The list is ['apple', 'banana', 'cherry'], so index 1 is 'banana'.
Final Answer:
banana -> Option D
Quick Check:
fruits[1] = banana [OK]
Hint: List indexes start at 0, so index 1 is second item [OK]
Common Mistakes:
Thinking index starts at 1
Confusing list with dictionary
Expecting an error for valid index
4. Find the error in this code that tries to add an item to a list:
my_list = [1, 2, 3]
my_list.add(4)
print(my_list)
medium
A. Missing brackets in print statement
B. Using add() instead of append() to add item
C. List cannot hold numbers
D. List must be declared with curly braces
Solution
Step 1: Identify method to add items to list
Python lists use append() to add items, not add().
Step 2: Check the code for method usage
The code uses my_list.add(4), which causes an error because add() is not a list method.
Final Answer:
Using add() instead of append() to add item -> Option B
Quick Check:
Use append() to add items to list [OK]
Hint: Use append() to add items to lists, not add() [OK]
Common Mistakes:
Using add() which is for sets
Thinking lists can't hold numbers
Incorrect print syntax assumptions
5. You have a list of student names and want to add a new student, remove one who left, and keep the order. Which data structure should you use and why?
hard
A. Use a dictionary because it stores key-value pairs
B. Use a set because it automatically sorts items
C. Use a list because it keeps order and allows adding/removing items
D. Use a tuple because it is immutable
Solution
Step 1: Understand requirements for data structure
The data structure must keep order and allow adding/removing students.
Step 2: Match requirements with data structure features
Lists keep order and allow adding/removing items. Sets do not keep order. Dictionaries store key-value pairs, not just items. Tuples are immutable and cannot be changed.
Final Answer:
Use a list because it keeps order and allows adding/removing items -> Option C
Quick Check:
Lists keep order and are changeable [OK]
Hint: Lists keep order and allow changes, perfect for student lists [OK]