range(), online
Six activities your students do on their own devices. See who's finished, and their scores.
Learning
Understanding
Testing
range() in one minute
What students need to know
range(start, stop, step) — starts at 2, counts by 2, stops before 8. Step of 2, stopping before 10 — so 10 is excluded.
Stop is 0, which is excluded — count down stops at 1. range(1, 4) gives 1, 2, 3 — that's 3 numbers.
Mistakes students make
- range(1, 5) stops before 5: use range(1, 6) to include 5.
- To count down, you need a negative step: range(10, 0, -1).
- range() only works with integers: not floats like 1.0.
- range is lowercase: Range is not a valid function.
PREVIOUS · MODULE 20For Loops
YOU ARE HERE · MODULE 21range()
NEXT · MODULE 22Error Handling – try/except basics
What students will learn in this module
Every question in Module 21
Quick questions
- What does range(5) produce?A1, 2, 3, 4, 5B0, 1, 2, 3, 4
- What does range(2, 5) produce?A2, 3, 4, 5B2, 3, 4
- Is the stop value included in range()?ANo it stops before itBYes always included
- What does range(0, 10, 2) produce?A0, 2, 4, 6, 8B0, 2, 4, 6, 8, 10
- What does range(3, 0, -1) produce?A3, 2, 1B3, 2, 1, 0
Key terms
Students type the meaning of:
range(stop)range(start, stop)range(start, stop, step)Step
Match the pairs
range(1.5, 10)Match it to what it doesfor i in range(5, 1):
print(i)Match it to what it doesrange(2, 8, 2)Match it to what it doesrange(5, 0, -1)Match it to what it doesprint(list(range(3)))Match it to what it doesprint(list(range(1,6)))Match it to what it doesFill in the blanks
for i in _____(5): print(i)for i in range(1, _): print(i) # prints 1,2,3,4,5for i in range(0, 10, _): print(i) # counts by 2for i in range(5, 0, __): print(i) # counts downfor i in range(_, 6): print(i) # starts at 1
Spot the error
Question 1
A# Print numbers 1 to 5Btotal = 0Cfor i in range(1, 5):D print(i)E total += i
Question 2
A# Count down from 10 to 1Bfor i in range(10, 0):C print(i)Dprint('Done!')
Question 3
A# Print integers 1 to 4Bfor i in range(1.0, 5.0):C print(i)Dprint('Done!')
Question 4
Atotal = 0Bfor i in Range(5):C total += iDprint('Sum:', total)
Question 5
Atotal = 0Bfor i in range[5]:C total += iDprint('Sum:', total)
