Loops are one of the main control structures in any programming language, and Python is no different.
In this article, we will look at a couple of examples using
for loops with Python's
Here's an Interactive Scrim of a Python For Loop
For Loops in Python
for loops repeat a portion of code for a set of values.
As discussed in Python's documentation,
for loop sets the iterator variable to each value in a provided list, array, or string and repeats the code in the body of the
for loop for each value of the iterator variable.
In the example below, we use a
for loop to print every number in our array.
# Example for loop for i in [1, 2, 3, 4]: print(i, end=", ") # prints: 1, 2, 3, 4,
We can include more complex logic in the body of a
for loop as well. In this example we print the result of a small computation based on the value of our iterator variable.
# More complex example for i in [1, 3, 5, 7, 9]: x = i**2 - (i-1)*(i+1) print(x, end=", ") # prints 1, 1, 1, 1, 1,
When the values in the array for our
for loop are sequential, we can use Python's
range() function instead of writing out the contents of our array.
The Range function in Python
range() function provides a sequence of integers based upon the function's arguments. Additional information can be found in Python's documentation for the
range(stop) range(start, stop[, step])
start argument is the first value in the range. If
range() is called with only one argument, then Python assumes
start = 0.
stop argument is the upper bound of the range. It is important to realize that this upper value is not included in the range.
In the example below, we have a range starting at the default value of
0 and including integers less than
# Example with one argument for i in range(5): print(i, end=", ") # prints: 0, 1, 2, 3, 4,
In our next example, we set
start = -1 and again include integers less than
# Example with two arguments for i in range(-1, 5): print(i, end=", ") # prints: -1, 0, 1, 2, 3, 4,
step value controls the increment between the values in the range. By default,
step = 1.
In our final example, we use the range of integers from
5 and set
step = 2.
# Example with three arguments for i in range(-1, 5, 2): print(i, end=", ") # prints: -1, 1, 3,
In this article, we looked at
for loops in Python and the
for loops repeat a block of code for all of the values in a list, array, string, or
We can use a
range() to simplify writing a
for loop. The
stop value of the
range() must be specified, but we can also modify the
starting value and the
step between integers in the