You want to print all even numbers from 2 to 10 using a loop. Which code correctly uses a loop to do this?
hard📝 Application Q15 of 15
Python - For Loop
You want to print all even numbers from 2 to 10 using a loop. Which code correctly uses a loop to do this?
AAll of the above
Bfor i in range(2, 11):
if i % 2 == 0:
print(i)
Ci = 2
while i <= 10:
print(i)
i += 2
Dfor i in range(2, 11, 2):
print(i)
Step-by-Step Solution
Solution:
Step 1: Analyze for i in range(2, 11, 2):
print(i)
Uses for loop with step 2 from 2 to 10 inclusive, prints even numbers correctly.
Step 2: Analyze for i in range(2, 11):
if i % 2 == 0:
print(i) and C
for i in range(2, 11):
if i % 2 == 0:
print(i) uses for loop with condition to print evens; i = 2
while i <= 10:
print(i)
i += 2 uses while loop incrementing by 2. Both print even numbers correctly.
Step 3: Understand All of the above
Since A, B, and C all correctly print even numbers from 2 to 10, All of the above is correct.
Final Answer:
All of the above -> Option A
Quick Check:
All options print evens correctly = All of the above [OK]
Quick Trick:Multiple loop styles can print even numbers correctly [OK]
Common Mistakes:
MISTAKES
Forgetting step in range for even numbers
Not including 10 in range end
Incorrect increment in while loop
Master "For Loop" in Python
9 interactive learning modes - each teaches the same concept differently