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
Iterating over lists
📖 Scenario: You are helping a small bookstore organize its daily sales data. The store records the number of books sold each day in a list. You will write a simple program to go through this list and work with the sales numbers.
🎯 Goal: Build a program that creates a list of daily book sales, sets a target sales number, counts how many days met or exceeded the target using a loop, and then prints the count.
📋 What You'll Learn
Create a list called daily_sales with the exact values: 12, 7, 15, 9, 20, 5
Create a variable called target and set it to 10
Use a for loop with the variable sales to iterate over daily_sales
Inside the loop, count how many days have sales greater than or equal to target
Print the final count using print(count)
💡 Why This Matters
🌍 Real World
Counting how many days meet a sales goal helps businesses understand their performance and plan better.
💼 Career
This skill is useful for data analysis, reporting, and automating simple business tasks.
Progress0 / 4 steps
1
Create the daily sales list
Create a list called daily_sales with these exact values: 12, 7, 15, 9, 20, 5
Python
Hint
Use square brackets [] to create a list and separate numbers with commas.
2
Set the target sales number
Create a variable called target and set it to the number 10
Python
Hint
Use the equals sign = to assign the value 10 to the variable target.
3
Count days meeting the target
Create a variable called count and set it to 0. Then use a for loop with the variable sales to iterate over daily_sales. Inside the loop, add 1 to count if sales is greater than or equal to target.
Python
Hint
Start with count = 0. Use a for loop to check each sales. Use an if statement to compare sales with target. Increase count by 1 when condition is true.
4
Print the count
Write a print statement to display the value of count
Python
Hint
Use print(count) to show the final number of days meeting the target.
Practice
(1/5)
1. What does the following code do?
for item in [1, 2, 3]:
print(item)
easy
A. Prints the whole list once
B. Prints numbers 1 to 3 without spaces
C. Prints each number 1, 2, and 3 on a new line
D. Causes an error because of the loop
Solution
Step 1: Understand the for loop over a list
The loop goes through each element in the list [1, 2, 3] one by one.
Step 2: Print each element during iteration
Each element (1, then 2, then 3) is printed on its own line because of the print statement.
Final Answer:
Prints each number 1, 2, and 3 on a new line -> Option C
Quick Check:
Loop prints items one by one [OK]
Hint: For loops print each list item separately [OK]
Common Mistakes:
Thinking it prints the whole list at once
Expecting numbers to print on the same line
Confusing loop variable with list itself
2. Which of these is the correct syntax to loop over a list named fruits?
easy
A. for fruit in fruits:
print(fruit)
B. for fruits in fruit:
print(fruits)
C. loop fruit in fruits:
print(fruit)
D. for fruit to fruits:
print(fruit)
Solution
Step 1: Identify correct for loop syntax
The correct Python syntax uses 'for variable in list:' to loop over items.
Step 2: Check each option
for fruit in fruits:
print(fruit) uses 'for fruit in fruits:', which is correct. Others use invalid keywords or reversed names.
Final Answer:
for fruit in fruits:
print(fruit) -> Option A
Quick Check:
Use 'for item in list:' syntax [OK]
Hint: Remember: for variable in list: [OK]
Common Mistakes:
Swapping variable and list names
Using wrong keywords like 'loop' or 'to'
Missing colon after for statement
3. What is the output of this code?
numbers = [2, 4, 6]
sum = 0
for n in numbers:
sum += n
print(sum)
medium
A. 12
B. 0
C. 246
D. Error
Solution
Step 1: Understand the loop adding numbers
The loop adds each number in the list [2, 4, 6] to sum, starting from 0.
Step 2: Calculate the total sum
sum = 0 + 2 + 4 + 6 = 12
Final Answer:
12 -> Option A
Quick Check:
Sum of list items = 12 [OK]
Hint: Add each item inside the loop to get total [OK]
Common Mistakes:
Printing sum before loop
Concatenating numbers as strings
Forgetting to initialize sum to 0
4. Find the error in this code:
items = [10, 20, 30]
for i in items
print(i)
medium
A. List name is incorrect
B. Wrong variable name in loop
C. Indentation error in print
D. Missing colon after for statement
Solution
Step 1: Check for syntax errors in for loop
The for loop must end with a colon ':' to be valid syntax.
Step 2: Identify missing colon
The code misses ':' after 'for i in items', causing a syntax error.
Final Answer:
Missing colon after for statement -> Option D
Quick Check:
For loops need ':' at end [OK]
Hint: Always put ':' after for loop header [OK]
Common Mistakes:
Forgetting colon after for statement
Using wrong variable names
Incorrect indentation of print
5. You have a list of lists: data = [[1, 2], [3, 4], [5, 6]]. How do you print all numbers one by one using iteration?
hard
A. print(data[0][0], data[1][1], data[2][2])
B. for sublist in data:
for num in sublist:
print(num)
C. for i in range(len(data)):
print(data[i])
D. for num in data:
print(num)
Solution
Step 1: Understand nested lists and iteration
Each item in data is a list itself, so we need two loops: one for each sublist, one for numbers inside.
Step 2: Use nested for loops to access all numbers
The outer loop goes through each sublist, the inner loop prints each number inside that sublist.
Final Answer:
for sublist in data:
for num in sublist:
print(num) -> Option B
Quick Check:
Nested loops print all inner items [OK]
Hint: Use nested loops for lists inside lists [OK]