How to use the range function in Python
What is the range() function?
The range function in Python generates a random list of numbers which is generally used to iterate over items using for loops.
Uses cases:
A range function might be called:
-
When an action needs to be performed
Nnumber of times. -
In order to iterate over any iterable object.
Range() parameters
The range() function has two sets of parameters:
-
range(stop) -
range(start, stop, step)
Here is what these terms mean:
Start: Starting number of the sequence.
Stop: Generate integers (whole numbers) up to, but not including this number.
Step: Difference between each number in the sequence.
Note: All parameters must be integers and can either be positive or negative.
range() is 0-index based, meaning list indexes start at 0, not 1.
1 of 6
Implementation
Now let’s take a look at an example implementing the range() function.
# One parameterprint "Sequence with One parameter"for i in range(4):print(i)# Two parametersprint "Sequence with Two parameters"for i in range(2, 8):print(i)# Three parametersprint "Sequence with Three parameters"for i in range(5, 12, 3):print(i)# Going backwardsprint "Backward Sequence"for i in range(20, -10, -5):print(i)# iterating a listprint "Printing List Elements"mylist = ['alpha', 'bravo', 'charlie']for i in range(0, len(mylist)): #len function calculates the length of the listprint(mylist[i])
Free Resources
Copyright ©2025 Educative, Inc. All rights reserved