Know All About Range in Python [Syntax, Parameters, Examples included]
A built-in function in Python using which a sequence of numbers is generated within a given range is called range in Python. To implement range in Python, you need to make use of a function called range() function.
Necessity to Use Range() Function in Python:
- Whenever an action needs to be performed a certain number of times, then you must make use of range in Python to generate a sequence of numbers within a given range.
- The range() function is used in loops like for loop, while loop etc. to iterate through the sequences.
Range() Function Is in Three Different Ways
Range() Function Is in Three Different Ways Based on the Parameters You Are Passing to the Function. They Are:
- range(stop)
- range(start, stop)
- range(start, stop, step)
The syntax to define range() function in Python is as follows:
range(stop)
where,
- Stop represents the integer till which the sequence of integers should be generated
- The sequence of integers is generated using the range() function, by passing just one argument stop, begins from 0
Example 1:
Python program to show the range() function in Python by passing just one argument stop to the range() function, to generate a sequence of integers and display the output on the screen:
#defining a for loop to iterate through the generated sequence of numbers starting from 0 to 4 using range() function
for each_element in range(5):
print("The first value is = ", each_element)
print()
You can see the output of the above program in the snapshot below:
range(start, stop)
where,
- Start represents the integer from which the sequence of integers must begin
- Stop represents the integer till which the sequence of integers that must be generated
- The sequence of integers generated using range() function by passing two arguments start and stop, begins from the integer specified as start till the integer specified as stop, but not including the integer stop
Example 2:
Python program to demonstrate the range() function in Python by passing two arguments start and stop to the range() function, to generate a sequence of integers and display the output on the screen:
#defining a for loop to iterate through the generated sequence of numbers starting from 1 to 5 using range() function
for each_element in range(1,6):
print(“The first value is = “, each_element)
print()
You can see the output of the above program in the snapshot below:
range(start, stop, step)
where,
- Start represents the integer from which the sequence of integers must begin
- Stop represents the integer till which the sequence of integers that must be generated
- Step represents the difference between consecutive integers in the generated sequence
- If the argument step is not specified in the range() function, then the value of the step is 1 by default for the sequence of integers generated
- The sequence of integers generated using range() function by passing three arguments- start, stop and step begins from the integer specified as start till the integer specified as stop, but not including the integer stop
Example 3:
Python program to demonstrate the range() function in Python by passing three arguments start, stop and step, to the range() function to generate a sequence of integers and display the output on the screen:
#defining a for loop to iterate through the generated sequence of numbers starting from 1 till 6 with a difference of 2 between consecutive integers in the generated sequence using range() function
for each_element in range(1,6, 2):
print("The first value is = ", each_element)
print()
The output of the above program is shown in the snapshot below:
Incrementing With Range and Decrementing With Range
- The step value passed as an argument to the range() function can be a positive value or negative value
- If the step value passed as an argument to the range() function is positive, then the generated sequence using the range() function is an incrementing range with the step, as the difference between consecutive integers in the generated sequence
- If the step value passed as an argument to the range() function is negative, then the generated sequence using the range() function is a decrementing range, with the step as the difference between consecutive integers in the generated sequence
Example 4:
Python program to demonstrate incrementing with range and decrementing with the range using range() function in Python to generate a sequence of integers and display the output on the screen:
#defining a for loop to iterate through the generated sequence of numbers starting from 1 to 10 with a difference of 3 between consecutive integers in the generated sequence using range() function
for each_element in range(1,10, 3):
print("The first value is = ", each_element)
print()
#defining a for loop to iterate through the generated sequence of numbers starting from 10 till 1 with a difference of -3 between consecutive integers in the generated sequence using range() function
for each_element in range(10,1, -3):
print("The first value is = ", each_element)
print()
The output of the above program is shown in the snapshot below:
Concatenation of the Result of Two Range() Functions:
- The sequence of integers generated using two different range() functions can be concatenated using the chain() function
- The chain() function iterates through the generated sequence of integers from each range() function passed as arguments to the chain() function one after the other
The syntax to define chain() function is as follows:
chain(range, range1)
where range represents the first range() function to generate a sequence of integers to be concatenated with the sequence of integers generated from the second range() function specified as range 1.
Example 5:
Python program to demonstrate chain() function in Python to concatenate two sequences of integers generated from two range() functions and display the output on the screen:
#importing the module chain
from itertools import chain
#using chain() function to concatenate the sequence of integers generated from two range() functions
con_seq = chain(range(3), range(5,3,-1))
#defining a for loop to iterate through the sequence of numbers generated using chain() function
for each_element in con_seq:
print("The first value is = ", each_element)
print()
The output of the above program is shown in the snapshot below:
Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer
Resource : https://www.simplilearn.com/tutorials/python-tutorial/range-in-python