The Python range() function returns a sequence of numbers, in a given range. The most common use of it is to iterate sequences on a sequence of numbers using Python loops.
Example
In the given example, we are printing the number from 0 to 4.
Python
for i in range(5):
print(i, end=" ")
print()
Output:
0 1 2 3 4
Syntax of Python range() function
Syntax: range(start, stop, step)
Parameter :
- start: [ optional ] start value of the sequence
- stop: next value after the end value of the sequence
- step: [ optional ] integer value, denoting the difference between any two numbers in the sequence
Return : Returns an object that represents a sequence of numbers
What is the use of the range function in Python
In simple terms, range() allows the user to generate a series of numbers within a given range. Depending on how many arguments the user is passing to the function, the user can decide where that series of numbers will begin and end, as well as how big the difference will be between one number and the next. Python range() function takes can be initialized in 3 ways.
- range (stop) takes one argument.
- range (start, stop) takes two arguments.
- range (start, stop, step) takes three arguments.
Python range (stop)
When the user call range() with one argument, the user will get a series of numbers that starts at 0 and includes every whole number up to, but not including, the number that the user has provided as the stop.

Python range visualization
Example of Python range (stop)
In this example, we are printing the number from 0 to 5. We are using the range function in which we are passing the stopping of the loop.
Python3
# printing first 6
# whole number
for i in range(6):
print(i, end=" ")
print()
Output:
0 1 2 3 4 5
Python range (start, stop)
When the user call range() with two arguments, the user gets to decide not only where the series of numbers stops but also where it starts, so the user doesn’t have to start at 0 all the time. Users can use range() to generate a series of numbers from X to Y using range(X, Y).

Python range visualization
Example of Python range (start, stop)
In this example, we are printing the number from 5 to 19. We are using the range function in which we are passing the starting and stopping points of the loop.
Python3
# printing a natural
# number from 5 to 20
for i in range(5, 20):
print(i, end=" ")
Output:
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Python range (start, stop, step)
When the user call range() with three arguments, the user can choose not only where the series of numbers will start and stop, but also how big the difference will be between one number and the next. If the user doesn’t provide a step, then range() will automatically behave as if the step is 1. In this example, we are printing even numbers between 0 and 10, so we choose our starting point from 0(start = 0) and stop the series at 10(stop = 10). For printing an even number the difference between one number and the next must be 2 (step = 2) after providing a step we get the following output (0, 2, 4, 8).

Python range visualization
Example of Python range (start, stop, step)
In this example, we are printing the number from 0 to 9 with the jump of 2. We are using the range function in which we are passing the starting and stopping points with the jump of the iterator.
Python3
for i in range(0, 10, 2):
print(i, end=" ")
print()
Output:
0 2 4 6 8
Incrementing the Range using a Positive Step
If a user wants to increment, then the user needs steps to be a positive number.
Python3
# incremented by 4
for i in range(0, 30, 4):
print(i, end=" ")
print()
Output :
0 4 8 12 16 20 24 28
Python range() using Negative Step
If a user wants to decrement, then the user needs steps to be a negative number.
Python3
# incremented by -2
for i in range(25, 2, -2):
print(i, end=" ")
print()
Output :
25 23 21 19 17 15 13 11 9 7 5 3
Python range() with Float Values
Python range() function doesn’t support float numbers. i.e. user cannot use floating-point or non-integer numbers in any of its arguments. Users can use only integer numbers.
Python3
# using a float number
for i in range(3.3):
print(i)
Output :
for i in range(3.3):
TypeError: 'float' object cannot be interpreted as an integer
Python range() with More Examples
Concatenation of two range() functions using itertools chain() method
The result from two range() functions can be concatenated by using the chain() method of itertools module. The chain() method is used to print all the values in iterable targets one after another mentioned in its arguments.
Python3
from itertools import chain
# Using chain method
print("Concatenating the result")
res = chain(range(5), range(10, 20, 2))
for i in res:
print(i, end=" ")
Output :
Concatenating the result
0 1 2 3 4 10 12 14 16 18
Accessing range() with an Index Value
A sequence of numbers is returned by the range() function as its object that can be accessed by its index value. Both positive and negative indexing is supported by its object.
Python3
ele = range(10)[0]
print("First element:", ele)
ele = range(10)[-1]
print("\nLast element:", ele)
ele = range(10)[4]
print("\nFifth element:", ele)
Output :
First element: 0
Last element: 9
Fifth element: 4
range() function with List in Python
In this example, we are creating a list and we are printing list elements with the range() in Python.
Python
fruits = ["apple", "banana", "cherry", "date"]
for i in range(len(fruits)):
print(fruits[i])
Output :
apple
banana
cherry
date
Some Important points to remember about the Python range() function
- The range() function only works with 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.
Python range() function – FAQs
What Does the range() Function Do in Python?
The range() function generates a sequence of numbers, which is commonly used for looping a specific number of times in for loops. It produces an immutable sequence of integers from the start (inclusive) to the stop (exclusive) with a specified step.
How to Create a Sequence of Numbers with range()?
You can create a sequence of numbers using range() by specifying the start, stop, and step arguments.
Example:
for i in range(5):
print(i)
Output:
0
1
2
3
4
In this example, range(5) generates numbers from 0 to 4.
What Are the Parameters of the range() Function?
The range() function has three parameters:
start (optional): The starting value of the sequence. The default is 0.stop (required): The ending value of the sequence (exclusive).step (optional): The difference between each number in the sequence. The default is 1.
Syntax:
range(start, stop, step)
How to Use a Negative Step in the range() Function?
You can use a negative step to generate a sequence in reverse order.
Example:
for i in range(10, 0, -2):
print(i)
Output:
10
8
6
4
2
In this example, range(10, 0, -2) generates numbers from 10 to 2, decrementing by 2.
How to Generate a List Using range()?
To generate a list using range(), you can use the list() function to convert the range object into a list.
Example:
numbers = list(range(5))
print(numbers)
Output:
[0, 1, 2, 3, 4]
Another example with all parameters:
numbers = list(range(2, 10, 2))
print(numbers)
Output:
[2, 4, 6, 8]
Similar Reads
id() function in Python
In Python, id() function is a built-in function that returns the unique identifier of an object. The identifier is an integer, which represents the memory address of the object. The id() function is commonly used to check if two variables or objects refer to the same memory location. Python id() Fun
3 min read
Python 3 - input() function
In Python, we use the input() function to take input from the user. Whatever you enter as input, the input function converts it into a string. If you enter an integer value still input() function converts it into a string. Python input() Function SyntaxSyntax: input(prompt) Parameter: Prompt: (optio
3 min read
Python int() Function
Python int() function returns an integer from a given object or converts a number in a given base to a decimal. Example: In this example, we passed a string as an argument to the int() function and printed it. C/C++ Code age = "21" print("age =", int(age)) Output: age = 21Python
4 min read
Python len() Function
len() function returns number of items in an object that can be characters, words, or elements in a sequence. Let’s start with a simple example to understand the len() functions with strings: [GFGTABS] Python s = "GeeksforGeeks" # Use len() to find the length of the string length = len(s)
2 min read
Python map() function
The map() function is used to apply a given function to every item of an iterable, such as a list or tuple, and returns a map object (which is an iterator). Let's start with a simple example of using map() to convert a list of strings into a list of integers. [GFGTABS] Python s = ['1', '
4 min read
Python - max() function
Python max() function returns the largest item in an iterable or the largest of two or more arguments. It has two forms. max() function with objectsmax() function with iterablePython max() function With ObjectsUnlike the max() function of C/C++, the max() function in Python can take any type of obje
4 min read
memoryview() in Python
Python memoryview() function returns the memory views objects. Before learning more about memoryview() function let's see why do we use this function. Why do we use memoryview() function? As Memory view is a safe way to expose the buffer protocol in Python and a memoryview behaves just like bytes in
4 min read
Python min() Function
Python min() function returns the smallest of the values or the smallest item in an iterable passed as its parameter. Example: Find Python min integer from the list [GFGTABS] Python numbers = [23,25,65,21,98] print(min(numbers)) [/GFGTABS]Output 21Python min() Function Syntaxmin(a, b, c, ..., key=fu
4 min read
Python next() method
Python's next() function returns the next item of an iterator. Example Let us see a few examples to see how the next() method in Python works. C/C++ Code l_iter = iter(l) print(next(l_iter)) Output1 Note: The .next() method was a method for iterating over a sequence in Python 2. It has been replaced
4 min read
Python oct() Function
Python oct() function takes an integer and returns the octal representation in a string format. In this article, we will see how we can convert an integer to an octal in Python. Python oct() Function SyntaxSyntax : oct(x) Parameters: x - Must be an integer number and can be in either binary, decimal
2 min read
ord() function in Python
Python ord() function returns the Unicode code from a given character. This function accepts a string of unit length as an argument and returns the Unicode equivalence of the passed argument. In other words, given a string of length 1, the ord() function returns an integer representing the Unicode c
3 min read
Python pow() Function
Python pow() function returns the result of the first parameter raised to the power of the second parameter. Syntax of pow() Function in Python Syntax: pow(x, y, mod) Parameters : x : Number whose power has to be calculated.y : Value raised to compute power.mod [optional]: if provided, performs modu
2 min read
Python print() function
The python print() function as the name suggests is used to print a python object(s) in Python as standard output. Syntax: print(object(s), sep, end, file, flush) Parameters: Object(s): It can be any python object(s) like string, list, tuple, etc. But before printing all objects get converted into s
2 min read
Python range() function
The Python range() function returns a sequence of numbers, in a given range. The most common use of it is to iterate sequences on a sequence of numbers using Python loops. Example In the given example, we are printing the number from 0 to 4. [GFGTABS] Python for i in range(5): print(i, end="
7 min read
Python reversed() Method
Python reversed() method returns an iterator that accesses the given sequence in the reverse order. Example: [GFGTABS] Python # creating a list cars = ["nano", "swift", "bolero", "BMW"] # reversing the list reversed_cars = list(reversed(cars)) #printing the li
4 min read
round() function in Python
Python round() function is a built-in function available with Python. It will return you a float number that will be rounded to the decimal places which are given as input. If the decimal places to be rounded are not specified, it is considered as 0, and it will round to the nearest integer. In this
6 min read
Python slice() function
In this article, we will learn about the Python slice() function with the help of multiple examples. Example C/C++ Code String = 'Hello World' slice_obj = slice(5,11) print(String[slice_obj]) Output: World A sequence of objects of any type (string, bytes, tuple, list, or range) or the object which i
5 min read
Python sorted() Function
sorted() function returns a new sorted list from the elements of any iterable like (e.g., list, tuples, strings ). It creates and returns a new sorted list and leaves the original iterable unchanged. Let's start with a basic example of sorting a list of numbers using the sorted() function. [GFGTABS]
3 min read
Python str() function
The str() function in Python is an in-built function that takes an object as input and returns its string representation. It can be used to convert various data types into strings, which can then be used for printing, concatenation, and formatting. Let’s take a simple example to converting an Intege
4 min read
sum() function in Python
The sum of numbers in the list is required everywhere. Python provides an inbuilt function sum() which sums up the numbers in the list. [GFGTABS] Python arr = [1, 5, 2] print(sum(arr)) [/GFGTABS]Output8 Sum() Function in Python Syntax Syntax : sum(iterable, start) iterable : iterable can be anything
4 min read
type() function in Python
The type() function is mostly used for debugging purposes. Two different types of arguments can be passed to type() function, single and three arguments. If a single argument type(obj) is passed, it returns the type of the given object. If three argument types (object, bases, dict) are passed, it re
5 min read
zip() in Python
The zip() function in Python combines multiple iterables such as lists, tuples, strings, dict etc, into a single iterator of tuples. Each tuple contains elements from the input iterables that are at the same position. Let’s consider an example where we need to pair student names with their test scor
3 min read