0
0
Pythonprogramming~5 mins

Iteration using range() in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A3
B1, 2, 3
C0, 1, 2, 3
D0, 1, 2
Which range() call counts backwards from 5 to 1?
Arange(1, 6, 1)
Brange(5, 0, -1)
Crange(5, 1)
Drange(0, 5, -1)
What is the output of this code?<br>
for i in range(2, 7, 2): print(i)
A2 3 4 5 6
B2 4
C2 4 6
D2 3 5
What happens if you call range() with no arguments?
AIt produces an error
BIt produces an empty sequence
CIt counts from 0 to 10
DIt counts from 1 to 10
Which of these is a valid way to use range()?
Arange(1, 10, 3)
Brange(5, 1, 1)
Crange(0, 5, -1)
Drange()
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.