Introduction to Python Range Function

Introduction to Python Range Function

3 mins read1.8K Views Comment
clickHere
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Jun 12, 2023 11:01 IST

The range function in Python returns a sequence of numbers starting from the given input value (by default: 0), increments the number at the defined step size (by default: 1), and stops before the specified value. In this article, we will discuss python range function, and how to use them.

2022_11_MicrosoftTeams-image-69.jpg

Python offers a number of built-in functions and types that are always available to use. In this article, we will discuss one such built-in function that represents an immutable sequence of numbers and is commonly used for looping a specific number of times in the for loops.

Table of Content

What is Python Range Function?

Definition

The range function in Python returns a sequence of numbers starting from the given input value (by default: 0), increments the number at the defined step size (by default: 1), and stops before the specified value.

  • range () functions are most commonly used for loops to iterate sequences on a sequence of numbers.
  • range () function only takes integer values.
All About Python Lists Methods
All About Python Lists Methods
In this guide, you will learn all about lists in Python: accessing lists, changing list elements, looping through a list, and other list operations with examples.
Understanding Tuples in Python
Understanding Tuples in Python
In this guide, you will learn all about Tuples in Python: creating tuples, accessing tuple items, looping through a tuple, and other tuple operations with the help of examples.
Difference Between List and Tuple in Python
Difference Between List and Tuple in Python
Confused with what list and tuples in Python. Don’t worry the article will clear all your doubts. List and Tuple in Python are the data structures that store and manipulate...read more

Syntax

 
range (start, stop, step_size)
Copy code

Parameter

  • start: the value from where the sequence will start. 
    • It is optional. 
    • If not defined, the value starts from 0.
  • stop: the next value after the sequence will stop.
    • Not optional.
  • step_size: denotes the difference between two values, i.e., how much we have to add to get the next value.
    • It is optional.
    • If not defined, the step_size will be 1.
    • The value should be an integer value (whole number)
      • if step_size = 0, it will throw an error.

How to use the Python Range function?

Python range() functions are used in three different formats, depending on the number of parameters used at a time.

  • range (stop)

range (stop) function will return the list of values from 0 to stop – 1. It will take only positive values. 

  • range(stop) function considers only positive integers 
    • If you input a floating value (or any other data type in Python), it will return a TypError. 
    • If we input a negative value, it will show nothing.

Let’s take some examples:

Example 1:

 
#print the first 10 whole number
number = range(10)
print(list(number))
Copy code

Output

Example 2:

 
#print the first 10 integers using for loop
for i in range(10):
print(i, end =" ")
print()
Copy code

Output

Example 3

 
#using a negative value for the stop
for i in range(-10):
print(i, end =" ")
print()
Copy code

Output

nothing

Example 4

 
#using a floating value for the stop
for i in range(10.5):
print(i, end =" ")
print()
Copy code

Output

  • range (start, stop)

Here, we will use two parameters (or arguments) with the range() function, i.e., start and stop. As a result, we will get the sequence of integers as a start, start + 1, start + 2, start + 3, ……., stop -1

Note: Here, the start and stop values can be either positive or negative.

Let’s move to examples:

Example – 1:

 
# start and stop both are positive
#list the number of integers between 5 to 10.
for i in range(5, 10):
print(i, end =" ")
print()
Copy code

Output

Example – 2

 
# start = -ve & stop = +ve
#list the number of integers between -5 to 10.
for i in range(-5, 10):
print(i, end =" ")
print()
Copy code

Output

Example -3

 
#start and stop both are negative
#list the number of integers between -10 to -5.
for i in range(-10, -5):
print(i, end =" ")
print()
Copy code

Output

  • range (start, stop, step_size)

Here, we will use all three parameters with the range function, i.e., start, stop, and step size.

  • It is used when we don’t want to traverse an iterable sequentially but want to access every n-th value of the sequence.
  •  range(start, stop, step_size) will produce a sequence such as: 

start, start + step, start + 2*step, start + 3*step, ……., start + n *step

where,

start + n*step < stop < start + (n+1) step.

  • Here, the value of step size can be negative.
    • In this case, start > stop.

Let’s take some examples:

Example-1:

 
#list the number of integers between 0 to 20 at the step size of 2
for i in range (0, 20, 2):
print(i, end =" ")
print()
Copy code

Output

Example -2

 
#negative step-size
#lsit the number of integers between 0 to 20 at the step size of -2
for i in range (20, 0, -2):
print(i, end =" ")
print()
Copy code

Output

Reversed Range

How will you reverse the output produced by the range() function?

In Python, we can do it in two ways:

  • By negative step size (step size = -1)
  • By reverse function
    • reversed (range()) function displays the sequence of numbers produced by the range() function in descending or reversed order.

Let’s take an example to know how it works:

 
#using reversed function
#lsit the number of integers (in decreasing order) between 0 to 20 at the step size of 2
for i in reversed(range (0, 20, 2)):
print(i, end =" ")
print()
Copy code

Output

Conclusion

In this article, we have briefly discussed the range () function in Python, with examples of how it works in different conditions.
Hope you will like the article.
Keep Learning!!
Keep Sharing!!

About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio