What Does “for i in range” Mean In Python (With Examples)
To simply put it, python’s “for i in range” is a way to output values within the range()
function. By doing so, it allows you to easily produce the values within a given range.
But to fully understand how this line of code works, we have to break down the code to its core principles.
Free Bonus: Click Here To Get A FREE Introduction To Python Course and learn the basics of Python 3, such as Lists, NumPy, Functions and Packages.
How does python “for i in range” work?
The “for i in range()” uses a for loop function to iterate the values within the defined range parameters. The range builtin isn’t actually a method, it’s a type, in much the same way as str and list.
By iterating each member within the range values, you can actually access each member, this allows things like membership testing.
It is known as traversing when you loop over each value or go over members 1 by 1.
Python’s range syntax is structured like this: range(start, stop, step)
Start | Optional. An integer position start number. Default is 0 |
Stop | Required. An integer position stop number (doesn’t include the integer stop value itself). |
Step | Optional. An integer value specific incrementation values. Default is 1. |
The interesting thing with the range()
function is it doesn’t include the last value, this is because programmers prefer 0-based indexing.
It isn’t important to know the reasoning behind this, but if you would like to know more then, you can find out on stackoverflow.
For beginners out there, you may wonder why the function has an “i
” value. This “i
” value is merely an easy-to-use variable and an industry-standard way to represent the current increment in a loop.
So, every time the function loops, i
becomes the next value in the range.
You can also replace the “i
” value to any other lettering or words that you’d like.
If you’re still unsure of the i
variable, we recommend reading our article “What Does ‘i’ Mean In Python (Beginner Explained)“.
For a better understanding, this pseudo code below perfectly demonstrates how the function works.
” for everything in this range() apply the following condition”.
To better understand the full utility of the “for i in range” function,let’s take a look at a few examples below.
Example: Incrementing range values
Example: For loop in range (0,5)
for i in range(0,5):
print(i)
Output:
0
1
2
3
4
The “for i in range” in its simplest form automatically increments the values one integer at a time. As you can see there were 5 outputs starting from 0 and ending in 4.
Example: Incrementing range values with steps
Example: For loop in range (0,5,2)
for i in range(0,5,2):
print(i)
Ouput:
0
2
4
As you can see once we included the step parameter of 2. The values have now only outputted even numbers.
Example: decreasing range values
Example: For loop in range(5,0,-1)
for i in range(5,0,-1):
print(i)
Output:
5
4
3
2
1
Since we are decreasing the values, our start value should be 5 and ending in 0 with a step of -1. As you can see python’s 0-based indexing has taken into effect where it did not output the last value of 0. Rather it ended at integer 1.
Example: decreasing range values with steps
Example: For loop in range(5,0,-2)
for i in range(5,0,-2):
print(i)
Output:
5
3
1
As you can see, the output is all odd numbers, but why is this? This is because of the starting number of “5”, decreasing values increments of 2 from an odd number always generates odd values.
Frequently Asked Questions
Python for i in range helps iterate a series of values inside the range function. Where the value “i” is a temporary variable used to store the integer value of the current position in the range of the for loop. The value I can be replaced by another other name such as “x”, “z” and etc.
The “for i in range” loop helps iterate a series of predefined values. By iterating each member within the range values, you actually access each member, this allows things like membership testing.