0
0
Pythonprogramming~20 mins

For loop execution model in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
For Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested for loops with break
What is the output of this Python code?
Python
result = []
for i in range(3):
    for j in range(3):
        if j == 1:
            break
        result.append((i, j))
print(result)
A[(0, 0), (1, 0), (2, 0)]
B[(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)]
C[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
D[(0, 0), (1, 0), (2, 0), (2, 1)]
Attempts:
2 left
💡 Hint
Remember that 'break' stops the inner loop only.
Predict Output
intermediate
1:30remaining
For loop with else clause output
What will be printed by this code?
Python
for num in range(3):
    print(num)
else:
    print('Done')
ADone
B
0
1
2
C
0
1
2
Done
D
0
1
Done
Attempts:
2 left
💡 Hint
The else block runs if the loop completes normally.
Predict Output
advanced
1:30remaining
Effect of modifying loop variable inside for loop
What is the output of this code?
Python
nums = [1, 2, 3]
for n in nums:
    n += 10
print(nums)
A[1, 2, 3]
B[11, 12, 13]
C[1, 12, 3]
DTypeError
Attempts:
2 left
💡 Hint
Changing the loop variable does not change the list elements.
Predict Output
advanced
1:30remaining
Loop with continue and else behavior
What will this code print?
Python
for i in range(3):
    if i == 1:
        continue
    print(i)
else:
    print('Finished')
A
0
1
2
Finished
B
0
1
2
C
0
2
D
0
2
Finished
Attempts:
2 left
💡 Hint
Continue skips the current iteration but does not stop the loop.
🧠 Conceptual
expert
1:30remaining
Understanding for loop iteration and variable scope
Consider this code snippet. What is the value of i after the loop finishes?
Python
for i in range(5):
    pass
print(i)
ANameError
B4
C0
D5
Attempts:
2 left
💡 Hint
The loop variable keeps the last value it had in the loop.