0
0
Pythonprogramming~20 mins

Iteration using range() in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Range Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of range() with step
What is the output of this Python code?
result = []
for i in range(2, 10, 3):
    result.append(i)
print(result)
Python
result = []
for i in range(2, 10, 3):
    result.append(i)
print(result)
A[2, 6, 9]
B[2, 5, 8]
C[2, 3, 4, 5, 6, 7, 8, 9]
D[3, 6, 9]
Attempts:
2 left
💡 Hint
Remember that range(start, stop, step) includes start but excludes stop.
Predict Output
intermediate
2:00remaining
Counting iterations with negative step
What will be printed by this code?
count = 0
for x in range(10, 0, -2):
    count += 1
print(count)
Python
count = 0
for x in range(10, 0, -2):
    count += 1
print(count)
A5
B6
C0
D4
Attempts:
2 left
💡 Hint
Count how many numbers are generated from 10 down to 1 stepping by -2.
🧠 Conceptual
advanced
1:30remaining
Behavior of range() with equal start and stop
What does list(range(5, 5)) produce in Python?
ARaises ValueError
B[5]
C[0, 1, 2, 3, 4]
D[]
Attempts:
2 left
💡 Hint
Think about whether the stop value is included or excluded.
Predict Output
advanced
2:30remaining
Output of nested loops with range()
What is the output of this code?
result = []
for i in range(1, 4):
    for j in range(i):
        result.append(j)
print(result)
Python
result = []
for i in range(1, 4):
    for j in range(i):
        result.append(j)
print(result)
A[0, 1, 0, 1, 2]
B[1, 2, 3]
C[0, 0, 1, 0, 1, 2]
D[0, 1, 2]
Attempts:
2 left
💡 Hint
Look at how many times the inner loop runs for each outer loop iteration.
🔧 Debug
expert
1:30remaining
Identify the error in this range usage
What error does this code raise?
for i in range(5, 1, 1):
    print(i)
Python
for i in range(5, 1, 1):
    print(i)
APrints nothing
BPrints 5, 6, 7, 8, 9
CRaises ValueError
DInfinite loop
Attempts:
2 left
💡 Hint
Check if the step direction matches the start and stop values.