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.

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.

**Must Read:** For Loop in Python (Practice Examples)

**Must Check:** Python Online Course and Certification

**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.

**Syntax**

range (start, stop, step_size)

**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 numbernumber = range(10)print(list(number))

**Output**

**Example 2:**

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

**Output**

**Example 3**

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

**Output**

nothing

**Example 4**

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

**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()

**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()

**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()

**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**.

- In this case,

Let’s take some examples:

**Example-1:**

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

**Output**

**Example -2**

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

**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 2for i in reversed(range (0, 20, 2)): print(i, end =" ")print()

**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**

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