📖 Scenario: Imagine you are organizing a small party and you have a list of guests. You want to greet each guest by name.
🎯 Goal: You will create a list of guest names, set up a greeting message, use a for loop to greet each guest, and finally print all greetings.
📋 What You'll Learn
Create a list called guests with the exact names: 'Alice', 'Bob', 'Charlie'
Create a variable called greeting with the exact value 'Hello'
Use a for loop with the variable guest to iterate over guests
Before the loop, create a list called messages that stores each greeting message combining greeting and guest
Print each greeting message stored in messages after the loop
💡 Why This Matters
🌍 Real World
Greeting guests or customers by name is common in apps, websites, and events to make people feel welcome.
💼 Career
Understanding how to use for loops to process lists is a fundamental skill for many programming jobs, including web development, data processing, and automation.
Progress0 / 4 steps
1
Create the guest list
Create a list called guests with these exact names: 'Alice', 'Bob', 'Charlie'
Python
Hint
Use square brackets [] to create a list and separate names with commas.
2
Set up the greeting message
Create a variable called greeting and set it to the string 'Hello'
Python
Hint
Use quotes to create a string. For example: greeting = 'Hello'
3
Use a for loop to create greeting messages
Before the loop, create a list called messages. Use a for loop with the variable guest to iterate over guests. Inside the loop, append each greeting message combining greeting and guest separated by a space to messages.
Python
Hint
Use messages.append(f"{greeting} {guest}") inside the loop to add each message.
4
Print all greeting messages
Write a for loop with the variable message to iterate over messages and print each message.
Python
Hint
Use print(message) inside the loop to show each greeting.
Practice
(1/5)
1. What does a for loop do in Python?
easy
A. Repeats a block of code for each item in a sequence
B. Runs code only once
C. Stops the program immediately
D. Creates a new variable
Solution
Step 1: Understand the purpose of a for loop
A for loop runs code repeatedly for each item in a list, string, or range. 'Repeats a block of code for each item in a sequence' matches this; others do not.
Final Answer:
Repeats a block of code for each item in a sequence -> Option A
Quick Check:
For loop = repeat for each item [OK]
Hint: For loops repeat actions for every item in a list [OK]
Common Mistakes:
Thinking for loops run only once
Confusing for loops with if statements
Believing for loops stop the program
2. Which of the following is the correct syntax for a for loop in Python?
easy
A. for (i=0; i<5; i++) print(i)
B. for i to range(5) print(i)
C. for i in range(5): print(i)
D. foreach i in range(5): print(i)
Solution
Step 1: Recall Python for loop syntax
Python uses 'for variable in sequence:' followed by indented code. 'for i in range(5): print(i)' matches; others use wrong keywords or styles.
Final Answer:
for i in range(5): print(i) -> Option C
Quick Check:
Python for loop = for variable in sequence: [OK]
Hint: Use 'for variable in sequence:' syntax in Python [OK]
Common Mistakes:
Using 'to' instead of 'in'
Using C-style for loop syntax
Using 'foreach' which is not Python
3. What is the output of this code?
for i in range(3):
print(i * 2)
medium
A. 2 4 6
B. 0 1 2
C. 1 2 3
D. 0 2 4
Solution
Step 1: Trace the loop range and output
range(3) gives i=0,1,2. Prints i*2: 0, 2, 4.
Final Answer:
0 2 4 -> Option D
Quick Check:
Multiply each i by 2 = 0 2 4 [OK]
Hint: Multiply loop variable by 2 for each iteration [OK]
Common Mistakes:
Printing i instead of i*2
Starting count from 1 instead of 0
Confusing range(3) with range(1,3)
4. Find the error in this code:
for i in range(5)
print(i)
medium
A. Indentation error on print line
B. Missing colon ':' after range(5)
C. range(5) is invalid syntax
D. Variable i is not defined
Solution
Step 1: Check for syntax errors in for loop
Python requires ':' after the for statement. The code misses ':' after range(5).
Final Answer:
Missing colon ':' after range(5) -> Option B
Quick Check:
For loop needs ':' after header [OK]
Hint: Always add ':' after for loop header [OK]
Common Mistakes:
Forgetting colon ':' after for statement
Incorrect indentation
Assuming range(5) is invalid
5. You want to create a list of squares for numbers 0 to 4 using a for loop. Which code correctly does this?
hard
A. squares = []
for i in range(5):
squares.append(i*i)
print(squares)
B. squares = [i*i for i in range(5)]
print(squares)
C. squares = []
for i in range(5):
squares = i*i
print(squares)
D. for i in range(5):
squares.append(i*i)
print(squares)
Solution
Step 1: Identify correct code for list of squares using for loop
Initialize squares = [], then for i in range(5): append i*i, then print. squares = []
for i in range(5):
squares.append(i*i)
print(squares) works; others miss init, use comp, or overwrite.
Final Answer:
squares = []\nfor i in range(5):\n squares.append(i*i)\nprint(squares) -> Option A
Quick Check:
Initialize list, append squares in loop [OK]
Hint: Initialize list before loop, append inside loop [OK]
Common Mistakes:
Not initializing list before appending
Overwriting list variable inside loop
Using list comprehension but question asks for for loop
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.