Bird
Raised Fist0
Pythonprogramming~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

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
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.

Practice

(1/5)
1. What does range(5) do in a Python for loop?
easy
A. Generates numbers from 0 to 4
B. Generates numbers from 1 to 5
C. Generates numbers from 0 to 5
D. Generates numbers from 1 to 4

Solution

  1. Step 1: Understand the range function

    range(5) starts at 0 by default and stops before 5.
  2. Step 2: Identify the numbers generated

    The numbers generated are 0, 1, 2, 3, 4.
  3. Final Answer:

    Generates numbers from 0 to 4 -> Option A
  4. Quick Check:

    range(5) = 0,1,2,3,4 [OK]
Hint: Remember range stops before the end number [OK]
Common Mistakes:
  • Thinking range(5) includes 5
  • Starting count at 1 by default
  • Confusing stop value as inclusive
2. Which of the following is the correct syntax to loop 3 times using range()?
easy
A. for i in range(1, 3):
B. for i in range(3):
C. for i in range(0, 3, 2):
D. for i in range(3, 0):

Solution

  1. Step 1: Check each range syntax

    for i in range(1, 3): loops from 1 to 2 (2 times), for i in range(3): loops 0 to 2 (3 times), for i in range(0, 3, 2): loops 0 and 2 (2 times), for i in range(3, 0): runs zero times because start > stop without negative step.
  2. Step 2: Identify the correct loop count

    Only for i in range(3): loops exactly 3 times: 0,1,2.
  3. Final Answer:

    for i in range(3): -> Option B
  4. Quick Check:

    range(3) loops 3 times [OK]
Hint: range(n) loops exactly n times starting at 0 [OK]
Common Mistakes:
  • Using range(1, 3) which loops 2 times
  • Using invalid range with start > stop
  • Confusing step parameter
3. What is the output of this code?
for i in range(2, 7, 2):
    print(i, end=' ')
medium
A. 0 2 4 6
B. 2 3 4 5 6
C. 2 4 6 8
D. 2 4 6

Solution

  1. Step 1: Understand the range parameters

    range(2, 7, 2) starts at 2, stops before 7, stepping by 2.
  2. Step 2: List the numbers generated

    Numbers are 2, 4, 6.
  3. Final Answer:

    2 4 6 -> Option D
  4. Quick Check:

    range(2,7,2) = 2,4,6 [OK]
Hint: Start, stop, step control numbers generated [OK]
Common Mistakes:
  • Including 7 in output
  • Using wrong step size
  • Starting from 0 instead of 2
4. Find the error in this code:
for i in range(5, 1):
    print(i)
medium
A. No error, prints 5 to 1
B. SyntaxError due to wrong range syntax
C. Loop does not run because start > stop without step
D. Infinite loop

Solution

  1. Step 1: Analyze the range parameters

    range(5, 1) means start at 5, stop before 1, with default step 1.
  2. Step 2: Understand loop behavior

    Since start is greater than stop and step is positive, the loop runs zero times.
  3. Final Answer:

    Loop does not run because start > stop without step -> Option C
  4. Quick Check:

    range(5,1) empty range [OK]
Hint: Positive step needs start < stop to run [OK]
Common Mistakes:
  • Expecting loop to run backwards
  • Thinking syntax error occurs
  • Assuming infinite loop
5. You want to print all odd numbers from 1 to 15 using range(). Which code does this correctly?
hard
A. for i in range(1, 16, 2): print(i)
B. for i in range(0, 15, 2): print(i)
C. for i in range(1, 15): if i % 2 == 1: print(i)
D. for i in range(1, 16): print(i % 2)

Solution

  1. Step 1: Identify odd numbers range

    Odd numbers from 1 to 15 are 1,3,5,...,15.
  2. Step 2: Check each option

    for i in range(1, 16, 2): print(i) uses range(1,16,2) which generates 1,3,5,...,15 directly. for i in range(0, 15, 2): print(i) starts at 0 (even). for i in range(1, 15): if i % 2 == 1: print(i) misses 15 (range(1,15) goes to 14) and uses extra if check. for i in range(1, 16): print(i % 2) prints 1 or 0, not numbers.
  3. Final Answer:

    for i in range(1, 16, 2): print(i) -> Option A
  4. Quick Check:

    range(1,16,2) = odd numbers [OK]
Hint: Use step=2 starting at 1 for odd numbers [OK]
Common Mistakes:
  • Starting at 0 for odd numbers
  • Forgetting to include last number
  • Printing modulo instead of number