Python range Function [With Examples]

Introduction

The range() function in Python returns the sequence of the provided integer between the specified range. range() is a Python built-in function. It is used when a user must repeat an action a certain number of times. range() in Python(3.x) is simply a renamed version of xrange in Python (2.x). The range() function is used to generate a numeric sequence.

Because the Python range() function for loop is widely used, understanding it is essential when working with any type of Python code. The range() function is most commonly used in Python to iterate sequence types (Python range() List, string, etc.) with for and while loops.

Syntax:

range(start, stop, step)

Parameters:

  • start: integer starting from which the sequence of integers is to be returned
  • stop: integer before which the sequence of integers is to be returned. The range of integers ends at stop – 1.
  • step: integer value which determines the increment between each integer in the sequence

Return: It returns the object of the class range.

Code 01: Demonstration of Python range()

# welcome to softhunt.net
# Python Program to
# show range() basics
# printing a number
print('Numbers: ')
for i in range(15):
	print(i, end=" ")
print()

# using range for iteration
print('using range for iteration: ')
l = [5, 10, 15, 20, 25]
for i in range(len(l)):
	print(l[i], end=" ")
print()

# performing sum of natural
# number
sum = 0
for i in range(1, 16):
	sum = sum + i
print("Sum of first 15 natural number :", sum)

Output:

Numbers: 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
using range for iteration: 
5 10 15 20 25 
Sum of first 15 natural number : 120

Number of Ways to call a Python range()

There are three ways to call a range() Function

  1. range(stop)
  2. range(start, stop)
  3. range(start, stop, step)

range(stop)

When you call range() with a single argument, you’ll obtain a sequence of numbers that starts at 0 and includes every whole number up to, but not including, the number you specified as the stop.

Code 02:

# welcome to softhunt.net
# Python program to
# print whole number
# using range()

# printing first 5
# whole number
print('printing first 5: ')
for i in range(5):
	print(i, end=" ")
print()

# printing first 10
# whole number
print('printing first 10 :')
for i in range(10):
	print(i, end=" ")
print()

Output:

printing first 5: 
0 1 2 3 4 
printing first 10 :
0 1 2 3 4 5 6 7 8 9 

range(start, stop)

When you call range() with two arguments, you get to choose not only where the series of numbers ends but also where it begins, so you don’t have to start at 0. A user can use range() to produce a sequence of integers from X to Y. (X, Y).

Code 03:

# welcome to softhunt.net
# Python program to
# print natural number
# using range

# printing a natural
# number upto 20
print('printing a natural upto 20 :')
for i in range(1, 21):
	print(i, end=" ")
print()

# printing a natural
# number from 5 to 20
print('printing a natural from 10 to 20 :')
for i in range(10, 21):
	print(i, end=" ")
print()

Output:

printing a natural upto 20 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 
printing a natural from 10 to 20 :
10 11 12 13 14 15 16 17 18 19 20 

range(start, stop, step)

When a user calls range() with three parameters, he or she may specify not only where the series of numbers will begin and end, but also how much the difference between one number and the next will be. If the user does not specify a step, range() will operate as if the step is 1.

Code 04:

# welcome to softhunt.net
# Python program to
# print all number
# divisible by 5 and 7

# using range to print number
# divisible by 5
print('divisible by 5: ')
for i in range(0, 30, 5):
	print(i, end=" ")
print()

# using range to print number
# divisible by 7
print('divisible by 7: ')
for i in range(0, 50, 7):
	print(i, end=" ")
print()

Output:

divisible by 5: 
0 5 10 15 20 25 
divisible by 7: 
0 7 14 21 28 35 42 49 

Code Examples of Python range Function

Incrementing with the range using a positive step

If a user wants to increment, the steps must be a positive integer.

Code 05:

# welcome to softhunt.net
# incremented by 4
print('incremented by 4: ')
for i in range(2, 15, 4):
	print(i, end=" ")
print()

# incremented by 3
print('incremented by 3: ')
for i in range(2, 15, 3):
	print(i, end=" ")
print()

# incremented by 2
print('incremented by 2: ')
for i in range(2, 15, 2):
	print(i, end=" ")
print()

Output:

incremented by 4: 
2 6 10 14 
incremented by 3: 
2 5 8 11 14 
incremented by 2: 
2 4 6 8 10 12 14 

Python range() backward

If a user decides to decrease, the steps must be a negative number.

Code 06:

# welcome to softhunt.net
# decremented by 4
print('decremented by 4: ')
for i in range(15, 2, -4):
	print(i, end=" ")
print()

# decremented by 3
print('decremented by 3: ')
for i in range(15, 2, -3):
	print(i, end=" ")
print()

# decremented by 2
print('decremented by 2: ')
for i in range(15, 2, -2):
	print(i, end=" ")
print()

Output:

decremented by 4: 
15 11 7 3 
decremented by 3: 
15 12 9 6 3 
decremented by 2: 
15 13 11 9 7 5 3 

Python range() float

The range() function in Python does not support float values. In other words, the user cannot use a floating-point or non-integer value in any of its arguments. Users can only enter integer numbers.

Code 07:

# welcome to softhunt.net
# using a float number
for i in range(2.5):
	print(i)
print()

# using a float number
for i in range(15.75):
	print(i)
print()

Output:

Traceback (most recent call last):
  File "<string>", line 3, in <module>
TypeError: 'float' object cannot be interpreted as an integer

Concatenation of two range() functions

The chain() method of the itertools module may be use to concatenate the results of two range() functions. The chain() method is used to display all the values in iterable targets provided in its parameters one after the other.

Code 08:

# welcome to softhunt.net
from itertools import chain

# Using chain method
print("Concatenating the result: ")
res = chain(range(6), range(6, 20, 2))

for i in res:
	print(i, end=" ")
print()

Output:

Concatenating the result: 
0 1 2 3 4 5 6 8 10 12 14 16 18 

Accessing range() with index value

Using index value to access range().  The range() function returns a series of numbers as an object that can be retrieve by its index value. Its object allows for both positive and negative indexing.

Code 09:

# welcome to softhunt.net

element = range(5)[0]
print("First element:", element)

element = range(5)[-1]
print("Last element:", element)

element = range(5)[3]
print("Fourth element:", element)

Output:

First element: 0
Last element: 4
Fourth element: 3

Points to Remember about range() Function

  • range() function only works with the integers i.e. whole numbers.
  • All arguments must be integers. Users can not pass a string or float number or any other type in a start, stop and step argument of a range().
  • All three arguments can be positive or negative.
  • The step value must not be zero. If a step is zero python raises a ValueError exception.
  • range() is a type in Python
  • Users can access items in a range() by index, just as users do with a list

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. Python for Loop with Examples

Leave a Comment