Python for i in range() | Explained With Examples

In all programming languages, including Python, sometimes programs must repeat multiple times to complete a specific task. To achieve this purpose, the concept of loops is used in the programming world. The “for” loop in Python iterates on many sequences such as list, tuple, string, and “range()” function.

In this Python guide, we will teach you how to use a for loop to iterate over the range() function with numerous examples. The following aspects are discussed in this Python Guide:

So let’s begin!

What is Python for i in range()?

Python for i in range()” has three important expressions. The first one is the “for” loop, which iterates the value of the range() function. The second expression is “i”, which is a temporary variable, and any other name can be used instead of “i” such as z,q,v, etc. Lastly, the “range()” function defines the range between two numbers. The syntax of the “range()” function is shown below:

range(start, stop, step)

The range function takes three parameter values “start”, “stop”, and “step”. The “stop” parameter is mandatory to define inside the “range()” function, and other parameters are optional.

Note: The step size of the “range()” function is set to “1” by default. This indicates that on every iteration of the range() function, the step size value will be incremented by “1”. We can also specify a particular value for the step size parameter. If the step size value is negative, the step will be decremented according to the defined step value.

Let’s make it more clear by the following examples:

Example 1: Python for i in range() With One Argument

In the example given below, the “range()” function takes one argument and returns the sequence of values as an output.

Code:

for i in range(4):
    print(i)

In the above code, the “range()” function takes “4” as an input argument. The temporary value “i” stores every number of range functions and prints them on the screen one by one.

Output:

The output shows the value of the number within the specified range (0-3).

Example 2: Python for i in range() With Two Arguments

In the example given below, the “range()” function takes two arguments and returns the sequence of values as an output. The two parameters represent the starting and ending limits of the “range()” function. Let’s understand it via the following example:

Code:

for i in range(5, 10):
    print(i)

In the following code, the “range()” function takes two parameter values. The first parameter value indicates the start “5” (including) of the range number, and the second parameter indicates the end “10” (excluding) of the range number. Here the range starts from the number “5” and ends at the number  “10”.

Output:

The output shows that the range value is from “5 to 9”.

Example 3: Python for i in range() with Three Arguments

In the example mentioned below, the “range()” function takes three parameter values inside its parentheses. The third argument’s value allows us to create step size between the sequence of numbers.

To understand, let’s consider the following examples:

Code:

for i in range(1, 15, 2):
    print(i)

In the above code, the “range()” function takes three parameter values. The first and second parameter value is the starting and ending limit of the range number. The third parameter indicates the step size of the range number, i.e., 2 in this example.

Output:

The output shows the value of the range number from “1-15” with a step size of “2”.

Example 4: Python for i in range() with len() function

The “len()” function is used in Python to calculate the length of the string, list, dictionary, etc. In this example the “len()” function value is the argument value of “range()” function. The “for” loop iterates on the range of length of the list. Let understand it via the following code:

Code:

Name = ['JOSEPH', 'DAVID', 'MILTON']

for i in range(len(Name)):
    print(Name[i])

In the above code, the list values are initialized and stored in a variable named “Name”. The “range()” function uses the “len()” function inside the parentheses to get the list length. For loop iterates on the list and prints out each element on the screen.

Output:

The above output shows the element value one by one.

That is all from this tutorial!

Conclusion

In Python, the “for” loop is used to iterate over many sequences such as list, tuple, string, and “range()” function. For loop along with the range() function is used to iterate over the blocks of code within the specified range. The “range()” function must have a “stop” argument value. The “len()” function can also be used inside the “range()” function to get the length of the string and iterate on it using the “for loop” statement. This python guide explained using the for loop with the range() function through practical examples.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×