Python for i in range()

Python for i in range()

We can use a range object with For loop in Python, to iterate over the elements of the range.

In this tutorial, we will learn how to iterate over elements of given range using For Loop.

Python - for i in range()

Examples

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 Code Copy

Output

0
1
2
3
4

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 Code Copy

Output

5
6
7
8
9

3. 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 Code Copy

Output

5
8
11
14

Summary

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

Code copied to clipboard successfully 👍