Python programming language provides
range() function in order to create a sequence of numbers in a different start number, increments, etc.
range() function provides lightweight creation of sequences during run-time which makes this type of execution faster.
Syntax and Parameters
range() function has the following syntax where 3 parameters can be accepted but not all of them are mandatory. Only the STOP parameter is mandatory.
STARTis used to specify the start number of the sequence. This number can be a positive or negative value like 4, -8, etc.
STOPspecifies the number we want to end the sequence. STOP is a must for range() function usage.
STEPis used to specify the increment value. If the step is not specified the default value will be 1. We can also specify the STEP negative in order to decrement from the given START number to the STOP number.
Create Simple Sequence From 1 to 10
We will start using
range() function in order to create a simple sequence. We will just specify the START and STOP. We will start at 1 and increment to the 10 with one by one.
Create Sequence Which Increments 2
In the previous example, we will use 1 as an increment value. We can specify the increment value explicit the value we want. We will just add the increment value as the 3rd parameter to the range() function. In this example, we will start at 0 and count to 10 by incrementing 2.
Create List From range()
As stated previously
range() function is computed during executions of the code which means if we do not run
range() and set to a different type like list it will be just a text. Let’s look below code which is just printed the
range(0,10) not the sequence.
So we need to create new data structures from the
range() function. range() function will literally create a list where we can use the created sequence as a list like below.
Iterate or Loop with range() Function
range() function creates sequences and these sequences generally used to loop or iterate. We can use range() function inside a loop statement like
while etc to iterate over elements of the sequence. In this example, we will iterate from 1 to the 20 with range function in for a loop.
for i in range(1,20): print(i)
Default Start Number Of range() Function
As stated in the syntax part only STOP number is must in range() function. So we do not have to provide the start number to the range() function. The default start number is 0. In this example, we will use the default start number and only specify the end number as 20.
for i in range(20): print(i)
Create Sequence Only Setting End Number
We can create a sequence with the range() function by only setting the stop or end number. In this example, we will start by default start number which 0 and increment to the 20.
for i in range(20): print(i)
Create Backward Sequence or Negative Step
Up to now, we have look how to create sequences with forwarding or positive steps. range() function also supports negative steps where the sequence will start from start number and take negative steps or decrement to the stop number. In this example, we will start from -2 and decrement to the -10 one by one. We have to also specify the decrement value which is -1
for i in range(-2,-10,-1): print(i)
range() vs xrange()
range() is a function provided in Python3 but in Python version 2 xrange() was used popularly. xrange() function is eliminated in Python3 so we can not use it. range() function also provides very efficient executaion to the xrange() function.
1 thought on “Python range() Function Tutorial with Examples”
Excellent and giving useful inputs.