Python loop for – for i in range

by Alex
Python loop for - for i in range

Cycles are one of the basic control constructs in any programming language, and Python is no exception. In this article we will look at some examples of using for loops with range() in Python.

For loops in Python

For loops repeat certain code for a certain set of values. You may learn from the Python documentation that Python for loops work a little differently than languages such as JavaScript or C. A for loop assigns to an iterated variable each value from a given list, array, or string, and repeats the code in the body of the for loop for each iterated variable value thus set. In the example below, we use the for loop to output each number in our array.
# A simple example of the for loop
for i in [0, 1, 2, 3, 4, 5]:
print(i, end="; ") # will output: 0; 1; 1; 2; 3; 4; 5;
You can also include more complex logic in the body of the for loop. In the following example, we print the result of a small calculation based on the value of the variable i.
# A more complicated example
for i in [0, 1, 2, 3, 4, 5]:
x = (i-2)*(i+2) - i**2 + 4
print(x, end="; ") # will print: 0; 0; 0; 0; 0; 0;
When the values in the array for our for loop represent some pattern, we can use the Python range() function instead of typing in the contents of our array manually.

The Range function in Python

The range() function returns a sequence of integers based on the arguments passed to it. For more information, see the Python documentation on the range() function.
range(stop)
range(start, stop[, step])
The start argument is the first value in the range. If range() is called with only one argument, Python assumes that start = 0. The stop argument is the upper bound of the range. It is important to understand that the boundary value itself is not included in the sequence. In the example below, we have a range that starts with a default value of 0 and includes integers less than 6.
# Using range() with a single argument
for i in range(6):
print(i, end="; ") # will print: 0; 1; 2; 3; 4; 5;
In the following example we set start = -2 and include integers less than 4.
# This time we pass two arguments
for i in range(-2, 4):
print(i, end="; ") # will print: -2; -1; 0; 1; 1; 2; 3;
The optional step value controls the increment between sequence values. By default, step = 1. In our last example we use a range of integers from -2 to 6 and set step = 2.
# All range() arguments are used here
for i in range(-2, 6, 2):
print(i, end="; ") # will output: -2; 0; 2; 4;

Conclusion

In this article we looked at for loops in Python and the range() function. The for loops ensure that a block of code is repeatedly executed for all values in a specified list, array, string, or sequence defined with the range() function. As shown, we can use range() to simplify the writing of the for loop. When you call this function, you must specify a stop value, and you can also specify a starting value and a step between integers in the returned range.

Related Posts

LEAVE A COMMENT