Menu Close

Python range() Function

rank_math_breadcrumb]

In this tutorial, you will learn about the Python range() function. range function in Python returns the
sequence between the start and end integer. the range function is the most popular function to generate a sequence of numbers.

Python range() function

The python range function is a built-in function that is used to return the
immutable sequence of numbers between the start and end integer. The sequence of numbers increment by 1 ( By default ).

Syntax

The syntax of range function in Python is:-

range(start, stop, step)

Parameter

range function in Python support three parameters start, end and step parameter.

  • Start:- Optional. An integer number specifying at which position to start. Default is 0.
  • stop:- Optional. An integer number specifying at which position to stop (not included).
  • step (Optional):- Optional. An integer number specifying the incrementation. Default is 1.

Python range function example

Here we will understand Python range function along with various example.

#Empty range
a = list(range(0))
print(a)


#Using range() function with start parameter:
b = list(range(10))
print(b)

#Using range() function with start and end parameter:
c = list(range(1, 10))
print(c)

#Using range() function with start, end and step parameter:
x = list(range(1, 16, 2))
print(x)


#Create list of even number:
start = 2
stop = 14
step = 2
print(list(range(start, stop, step)))

#range() work with negative index:
start = 2
stop = -14
step = -2
print(list(range(start, stop, step)))

Output

[]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 3, 5, 7, 9, 11, 13, 15]
[2, 4, 6, 8, 10, 12]
[2, 0, -2, -4, -6, -8, -10, -12]

Example 2:

Use Python range function with python for loop.

#print table of 12.
for i in range(1, 11):
	print(f"{12} x {i}:- {12 * i}")

Output

12 x 1:- 12
12 x 2:- 24
12 x 3:- 36
12 x 4:- 48
12 x 5:- 60
12 x 6:- 72
12 x 7:- 84
12 x 8:- 96
12 x 9:- 108
12 x 10:- 120

Conclusion

In this guide, You have learned all about Python range function along with examples to generate a sequence of numbers. This is the most useful function in Python.

If you like this article, please share and keep visiting for further Python built-in functions tutorials.

Python built-in functions


For more information:- Click Here

Python Object Oriented Programming (OOPs)
Python Inheritance Tutorial

Related Posts