0% found this document useful (0 votes)
47 views2 pages

Python's Range Expression: For in

The range expression generates integers for use in for loops. range(n) generates integers from 0 to n-1. range(m, n) generates integers from m to n-1. range(m, n, j) generates integers from m increasing or decreasing in steps of j, stopping before n. The generated numbers start at the first argument and stop before reaching the last argument. Be careful of off-by-one errors and only use integer arguments.

Uploaded by

ratheeshbr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views2 pages

Python's Range Expression: For in

The range expression generates integers for use in for loops. range(n) generates integers from 0 to n-1. range(m, n) generates integers from m to n-1. range(m, n, j) generates integers from m increasing or decreasing in steps of j, stopping before n. The generated numbers start at the first argument and stop before reaching the last argument. Be careful of off-by-one errors and only use integer arguments.

Uploaded by

ratheeshbr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python’s range expression The third form of the range expression works like this:

Recall that a range expression


generates integers that can be for k in range(n): range(m, n, j) generates integers starting at m,
used in a FOR loop, like this: in “steps” of j,
... k ...
stopping when the generated integer would be greater than
In that example, k takes on the
or equal to n (if j is positive) or when the generated integer
values 0, 1, 2, ... n-1, as the loop runs. That is:
would be less than or equal to n (if j is negative).

range(n) generates n integers starting at 0


(and hence ending at n-1). For example, the loops below generate the output shown to the right.

5
for k in range(5, 11, 2):
Python allows two other forms of the range expression, for your 7
print(k) 9
convenience. You never have to use these forms (the single-
argument form is sufficient), but they are often handy.

8
range(m, n) generates integers starting at m,
7
ending at n-1 (and hence for k in range(8, 3, -1):
6
generates n-m integers if n  m). print(k) 5
4
For example, the loop shown below to the left generates the output
shown below to the right.
Caution: In all three forms, the generated numbers start at the
5
first number and stop just before reaching the “stop” number:
for k in range(5, 9): 6
7 for k in range(30): does not include 30
print(k)
8 for k in range(3, 56): does not include 56
for k in range(10, 40, 5): does not include 40
Caution: range(m, n) generates NO integers if n  m. For
example, the loop for k in range(9, 5): runs NO times: for k in range(40, 10, -5): does not include 10

So the example to the right does for k in range(5, 0, -1):


for k in range(9, 5): NOT generate 5, 4, 3, 2, 1, 0, as
(no output) print(k)
print(k) the student hoped. (Figure out
why and then look at the next page.)
Answer: the loop to the right Summary:
stops just before it reaches 0, for k in range(5, 0, -1):
so it generates 5, 4, 3, 2, 1 print(k)
(which may or may not be
what you intend). range(n) generates n integers starting at 0
(and hence ending at n-1).

Another common error is to write this, range(m, n) generates integers starting at m,


which runs NO times, when you ending at n-1 (and hence
meant to write this: for k in range(0, 5, -1): generates n-m integers if n  m).
... k ...
for k in range(5, 0, -1):
range(m, n, j) generates integers starting at m,
... k ... in “steps” of j,
stopping when the generated integer would be greater than
or equal to n (if j is positive) or when the generated integer
for k in range(5, 0): would be less than or equal to n (if j is negative).
Or, similarly, to write this ... k ...
when you meant the 3-argument
expression (with a negative step), Don’t hesitate to use the two and three-argument forms when they
as above. clarify the code, but be aware of the pitfalls that may arise.

The arguments in a range


expression must be integers. for k in range(n / 2):
So yet another common ... k ...
mistake is to write this
when the following is necessary
even if the argument n is even.

for k in range(n // 2):


... k ...

You might also like