range() in Python allows us to create a sequence of numbers from a starting point up to an ending point.

Syntax:

range()
#OR
range(, )
#OR
range(, , )

Let’s see some examples using range():

#default starting point is always 0
x = range(10)
print(x)

Output

range(0, 10)
#converting range to list
x = list(range(10))
print(x)

Output

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Note: The last element is always excluded in range().

#using for loop to print range elements
for element in range(10):
    print(element)

Output

0
1
2
3
4
5
6
7
8
9
#using range & for loop to print even numbers till 20
for element in range(2, 20, 2):
    print(element)

Output

2
4
6
8
10
12
14
16
18

Comment your suggestion or doubts below.

Leave a Reply

14 + 16 =