64
3 Loops and Branching
In [4]: list(x)
# convert to list
Out[4]: [0, 1, 2, 3, 4]
A General Call to range With a header like
for loop_variable in range(start, stop, step):
and a step > 0, loop_variable will run through the numbers start, start +
1*step, start + 2*step, ...., start + n*step, where start +n∗step <
stop <= start + (n + 1) ∗ step. So, the final number is as close as we can get to
the specified stop without equalling, or passing, it. For a negative step (step < 0,
example given below), the same thing applies, meaning that the final number can
not equal, or be more negative, than the argument stop. Note that an integer step
different from 1 and −1 is perfectly legal.
Different Ways of Calling range The function range is most often used in for
loops to produce a required sequence of integers, but the function is not restricted to
for loops only, of course. It may be called in different ways, e.g., utilizing default
values. Some examples are
In [1]: list(range(1, 11, 1))
Out[1]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [2]: list(range(1, 11))
# step not given, default 1
Out[2]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [3]: list(range(11))
# start not given either, default 0
Out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [4]: list(range(1, -11, -1))
Out[4]: [1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10]
When calling range, there is no argument specifying how many numbers should be
produced, like we saw with the linspace function of numpy. With range, this is
implied by the function arguments start, stop and step.
Computer Memory Considerations Note that range does not return all the
requested numbers at once. Rather, the call to range will cause the numbers to
be provided one by one during loop execution, giving one number per iteration.
This way, simultaneous storing of all (loop variable) numbers in computer memory
is avoided, which may be very important when there is a large number of loop
iterations to make.
3.1.5 Using break and continue
It is possible to break out of a loop, i.e., to jump directly to the first code line after
the loop, by use of a break statement. This might be desirable, for example, if a
certain condition is met during loop execution, which makes the remaining iterations
redundant.
With loops, it may also become relevant to skip any remaining statements of
an ongoing iteration, and rather proceed directly with the next iteration. That is,
contrary to the “loop stop” caused by a break statement, the loop continues to run
after a continue statement, unless the end of the loop has been reached.
3 Loops and Branching
In [4]: list(x)
# convert to list
Out[4]: [0, 1, 2, 3, 4]
A General Call to range With a header like
for loop_variable in range(start, stop, step):
and a step > 0, loop_variable will run through the numbers start, start +
1*step, start + 2*step, ...., start + n*step, where start +n∗step <
stop <= start + (n + 1) ∗ step. So, the final number is as close as we can get to
the specified stop without equalling, or passing, it. For a negative step (step < 0,
example given below), the same thing applies, meaning that the final number can
not equal, or be more negative, than the argument stop. Note that an integer step
different from 1 and −1 is perfectly legal.
Different Ways of Calling range The function range is most often used in for
loops to produce a required sequence of integers, but the function is not restricted to
for loops only, of course. It may be called in different ways, e.g., utilizing default
values. Some examples are
In [1]: list(range(1, 11, 1))
Out[1]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [2]: list(range(1, 11))
# step not given, default 1
Out[2]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [3]: list(range(11))
# start not given either, default 0
Out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [4]: list(range(1, -11, -1))
Out[4]: [1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10]
When calling range, there is no argument specifying how many numbers should be
produced, like we saw with the linspace function of numpy. With range, this is
implied by the function arguments start, stop and step.
Computer Memory Considerations Note that range does not return all the
requested numbers at once. Rather, the call to range will cause the numbers to
be provided one by one during loop execution, giving one number per iteration.
This way, simultaneous storing of all (loop variable) numbers in computer memory
is avoided, which may be very important when there is a large number of loop
iterations to make.
3.1.5 Using break and continue
It is possible to break out of a loop, i.e., to jump directly to the first code line after
the loop, by use of a break statement. This might be desirable, for example, if a
certain condition is met during loop execution, which makes the remaining iterations
redundant.
With loops, it may also become relevant to skip any remaining statements of
an ongoing iteration, and rather proceed directly with the next iteration. That is,
contrary to the “loop stop” caused by a break statement, the loop continues to run
after a continue statement, unless the end of the loop has been reached.
