Python for i in range()

Python for i in range()

In this tutorial, we will learn how to iterate for loop each element in the given range.

Python - for i in range()

Example 1: for i in range(x)

In this example, we will take a range from 0 until x, not including x, in steps of one, and iterate for each of the element in this range using for loop.

Python Program

for i in range(5):
    print(i)
Run

Output

0
1
2
3
4

Example 2: for i in range(x, y)

In this example, we will take a range from x until y, including x but not including y, insteps of one, and iterate for each of the element in this range using for loop.

Python Program

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

Output

5
6
7
8
9

Example 2: for i in range(x, y, step)

In this example, we will take a range from x until y, including x but not including y, insteps of step value, and iterate for each of the element in this range using for loop.

Python Program

for i in range(5, 15, 3):
    print(i)
Run

Output

5
8
11
14

Summary

In this tutorial of Python Examples, we learned how to iterate over a range() using for loop.