Recall & Review
beginner
What does the
range() function do in Python?The
range() function generates a sequence of numbers, which is often used to repeat actions a certain number of times in loops.Click to reveal answer
beginner
How do you use
range() to count from 0 to 4?Use
range(5). It creates numbers starting at 0 up to but not including 5, so 0, 1, 2, 3, 4.Click to reveal answer
intermediate
What are the three parameters of
range() and what do they mean?The three parameters are
start (where to begin), stop (where to end, not included), and step (how much to jump each time). Example: range(1, 10, 2) counts 1, 3, 5, 7, 9.Click to reveal answer
intermediate
What happens if you use a negative step in
range()?Using a negative step makes the numbers count down. For example,
range(5, 0, -1) counts 5, 4, 3, 2, 1.Click to reveal answer
beginner
Why is
range() useful in a for loop?range() tells the loop how many times to run by giving a sequence of numbers to go through. This helps repeat tasks easily without writing extra code.Click to reveal answer
What does
range(3) produce?✗ Incorrect
range(3) starts at 0 and goes up to but not including 3, so it produces 0, 1, 2.Which
range() call counts backwards from 5 to 1?✗ Incorrect
range(5, 0, -1) starts at 5 and counts down by 1 until it reaches 1 (stop is not included).What is the output of this code?<br>
for i in range(2, 7, 2): print(i)
✗ Incorrect
The loop starts at 2, adds 2 each time, and stops before 7, so it prints 2, 4, 6.
What happens if you call
range() with no arguments?✗ Incorrect
range() requires at least one argument; calling it without arguments causes an error.Which of these is a valid way to use
range()?✗ Incorrect
range(1, 10, 3) counts from 1 to 9 in steps of 3. The others produce empty sequences or an error.Explain how to use
range() in a for loop to repeat an action 5 times.Think about how range creates numbers and how the loop uses them.
You got /4 concepts.
Describe the role of the
start, stop, and step parameters in range().Imagine walking on steps: where you start, where you stop, and how big your steps are.
You got /3 concepts.