There are many iterables in Python like list, tuple etc. range() gives another way to initialize a sequence of numbers using some conditions.
range() is commonly used in for looping hence, knowledge of same is key aspect when dealing with any kind of Python code.
Syntax : range(start, stop, step)
Parameters :
start : Element from which sequence constructed has to start. (default:0)
stop : Element number at which numbers in sequence have to end (exclusive).
step : Can be +ve or -ve number, denoting the elements need to be skipped during filling of list. (default:1)Returns : The list using the formula :
list[n] = start + step*n (for both positive and negative step) where, n >=0 and list[n] = 0 and list[n] > stop (for negative step)
Returns ValueError if step is 0. Value constraint is checked in case of step, failing to meet returns empty sequence, else returns sequence according to formula.
Code #1 : Demonstrating range() without step parameter
# Python3 code to demonstrate the # working of range() without step # using range() lis1 = list(range(6)) lis2 = list(range(3, 6)) lis3 = list(range(-6, 2)) # initializing list using range, 1 parameter # only stop parameter print("List generated using 1 parameter : " + str(lis1)) # initializing list using range, 2 parameters # only step and stop parameters print("List generated using 2 parameters : " + str(lis2)) # initializing list using range, 2 parameter # only step and stop parameters print("List generated using 2 parameters with negatives : " + str(lis3)) |
Output:
List generated using 1 parameter : [0, 1, 2, 3, 4, 5] List generated using 2 parameters : [3, 4, 5] List generated using 2 parameters with negatives : [-6, -5, -4, -3, -2, -1, 0, 1]
Code #2 : Demonstrating range() using step
# Python 3 code to demonstrate the # working of range() with step # initializing list using range # using step print("List generated using step : " + str(list(range(3, 10, 2)))) # initializing list using range # using negative step print("List generated using negative step : " + str(list(range(10, -5, -3)))) # initializing list using range # using negative step, # value contraints fail case print("List generated using step, value contraints fail : " + str(list(range(10, -5, 3)))) # initializing list using range # using 0 step # error print("List generated using 0 step : " + str(list(range(3, 7, 0)))) |
Output:
List generated using step : [3, 5, 7, 9] List generated using negative step : [10, 7, 4, 1, -2] List generated using step, value contraints fail : []
Exception:
Traceback (most recent call last):
File "/home/bdae725dff7b38d3681eee38f6a6d434.py", line 23, in
print("List generated using 0 step : " + str(list(range(3, 7, 0))))
ValueError: range() arg 3 must not be zero
Recommended Posts:
- Python Pytorch range() method
- range() vs xrange() in Python
- Python - Test if List contains elements in Range
- Python | Numbers in a list within a given range
- Python | Count unset bits in a range
- Python range() function
- Python | Count set bits in a range
- Python | Generate random numbers within a given range and store in a list
- Python List Comprehension | Three way partitioning of an array around a given range
- Python | Contiguous Boolean Range
- Python program to print all negative numbers in a range
- Python | Find missing numbers in a sorted list range
- Python program to print all even numbers in a range
- Python program to print all odd numbers in a range
- Python program to print all positive numbers in a range
- Python | range() does not return an iterator
- range() to a list in Python
- Python | Remove all sublists outside the given range
- Python | Print list elements in circular range
- Python | Front and rear range deletion in a list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

