Range Function¶
-
Replace
<1>
in the following code with a call torange()
so that the for loop prints out all integers from 1 to 10, inclusive.1for i in <1>:
2print(i)
3
(range1)
Therange
function takes 1, 2, or 3 integer parameters. The parameters arerange(start=0, stop, step=1)
. If astart
value is not provided, the value0
is used. If astep
value is not provided, the value1
is used. Thestop
value indicates the upper limit that the result should be less than. E.g., ifstop
is10
, then the result of therange
call will end at9
.31for i in range(1, 11):
2print(i)
3
(range1_a)
-
Replace
<1>
in the following code with a call torange()
so that the for loop prints out all integers from 0 to 99, inclusive.31for i in <1>:
2print(i)
3
(range2)
Remember that you can omit the first parameter if you want the result to start at 0.31for i in range(100):
2print(i)
3
(range2_a)
-
Replace
<1>
in the following code with a call torange()
so that the for loop prints out all integers from -10 to 10, inclusive.31for i in <1>:
2print(i)
3
(range3)
Parameters can be negative, and ifstep
is one, the results will increase by 1 each time through the loop.31for i in range(-10, 11):
2print(i)
3
(range3_a)
-
Replace
<1>
in the following code with a call torange()
so that the for loop prints out all even integers from 0 to 100, inclusive.31for i in <1>:
2print(i)
3
(range4)
Parameters can be negative, and ifstep
is 1, the results will increase by 1 each time through the loop.31for i in range(0, 101, 2):
2print(i)
3
(range4_a)
-
Replace
<1>
in the following code with a call torange()
so that the for loop prints out all multiples of 5 from 0 to 200, inclusive.31for i in <1>:
2print(i)
3
(range5)
Go back and review previous questions if you need a hint.31for i in range(0, 201, 5):
2print(i)
3
(range5_a)
-
Replace
<1>
in the following code with a call torange()
so that the for loop prints out all integers from 10 down to 1, inclusive.31for i in <1>:
2print(i)
3
(range6)
Provide a step of
-1
.range()
produces values down to but not equal to or less than thestop
value, if the step is negative.31for i in range(10, 0, -1):
2print(i)
3
(range6_a)