range()

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

range()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does range(5) produce?
A 1, 2, 3, 4, 5
B 0, 1, 2, 3, 4
2. What does range(2, 5) produce?
A 2, 3, 4, 5
B 2, 3, 4
3. Is the stop value included in range()?
A No it stops before it
B Yes always included
4. What does range(0, 10, 2) produce?
A 0, 2, 4, 6, 8
B 0, 2, 4, 6, 8, 10
5. What does range(3, 0, -1) produce?
A 3, 2, 1
B 3, 2, 1, 0

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1range(1.5, 10) AOutput: [0, 1, 2]
2for i in range(5, 1): print(i) BError: range() needs integers
3range(2, 8, 2) COk: Counts down 5,4,3,2,1
4range(5, 0, -1) DError: No output — step needed
5print(list(range(3))) EOutput: [1, 2, 3, 4, 5]
6print(list(range(1,6))) FValid: 2, 4, 6

Definitions

range(stop)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
for i in _____(5): print(i)
2.
for i in range(1, _): print(i) # prints 1,2,3,4,5
3.
for i in range(0, 10, _): print(i) # counts by 2
4.
for i in range(5, 0, __): print(i) # counts down
5.
for i in range(_, 6): print(i) # starts at 1

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A # Print numbers 1 to 5
B total = 0
C for i in range(1, 5):
D print(i)
E total += i
Question 2
A # Count down from 10 to 1
B for i in range(10, 0):
C print(i)
D print('Done!')
Question 3
A # Print integers 1 to 4
B for i in range(1.0, 5.0):
C print(i)
D print('Done!')
Question 4
A total = 0
B for i in Range(5):
C total += i
D print('Sum:', total)
Question 5
A total = 0
B for i in range[5]:
C total += i
D print('Sum:', total)

Answer Key

range()
Section A: Quick Questions
Q1
B — 0, 1, 2, 3, 4
Q2
B — 2, 3, 4
Q3
A — No it stops before it
Q4
A — 0, 2, 4, 6, 8
Q5
A — 3, 2, 1
Section B: Match the Pairs
Q1
range(1.5, 10) → Error: range() needs integers
Q2
for i in range(5, 1): print(i) → Error: No output — step needed
Q3
range(2, 8, 2) → Valid: 2, 4, 6
Q4
range(5, 0, -1) → Ok: Counts down 5,4,3,2,1
Q5
print(list(range(3))) → Output: [0, 1, 2]
Q6
print(list(range(1,6))) → Output: [1, 2, 3, 4, 5]
Section C: Definitions
Q1
range(stop) — Creates numbers from 0 up to but not including stop.~~range(start, stop)
Section D: Fill in the Blanks
Q1
for i in range(5): print(i)
Q2
for i in range(1, 6): print(i) # prints 1,2,3,4,5
Q3
for i in range(0, 10, 2): print(i) # counts by 2
Q4
for i in range(5, 0, -1): print(i) # counts down
Q5
for i in range(1, 6): print(i) # starts at 1
Section E: Spot the Error
Q1
C — range(1, 5) stops before 5: use range(1, 6) to include 5
Q2
B — To count down, you need a negative step: range(10, 0, -1)
Q3
B — range() only works with integers: not floats like 1.0
Q4
B — range is lowercase: Range is not a valid function
Q5
B — range uses parentheses (), not square brackets []