# Python Range Function

The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.

`range(start, stop[, step])`

• start- The value of the start parameter (or 0 if the parameter was not supplied)
• stop – The value of the stop parameter
• step – The value of the step parameter (or 1 if the parameter was not supplied)

Examples

``````>>> list(range(1))

>>> list(range(2))
[0, 1]
>>> list(range(1,10,2))
[1, 3, 5, 7, 9]
>>> list(range(0,-10,-2))
[0, -2, -4, -6, -8]
>>> list(range(1,0))
[]

>>> range(5)
range(0, 5)
>>> for i in range(5):
...     print(i)
...
0
1
2
3
4
>>> range(2,12)
range(2, 12)
>>> for i in range(2,12):
...     print(i)
...
2
3
4
5
6
7
8
9
10
11
>>> range(-5,5)
range(-5, 5)
>>> for i in range(-5,5):
...     print(i)
...
-5
-4
-3
-2
-1
0
1
2
3
4
>>> days =["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]
>>> for i in range(len(days)):
...     print(i,days[i])
...
0 Sun
1 Mon
2 Tue
3 Wed
4 Thu
5 Fri
6 Sat
>>> print(range(1,6))
range(1, 6)
>>> list(range(0,10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(1,10))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(1,50,5))
[1, 6, 11, 16, 21, 26, 31, 36, 41, 46]
>>> mylist = list(range(0,10))
>>> mylist
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> 5 in mylist
True
>>> 10 in mylist
False

>>> mylist.index(3)
3
>>> mylist.index(0)
0
>>> mylist.index(10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 10 is not in list
>>> mylist[:5]
[0, 1, 2, 3, 4]
>>> list(range(0))
[]
>>> list(range(1,0))
[]``````

References

• https://docs.python.org/3/library/stdtypes.html#range
• https://docs.python.org/3/tutorial/controlflow.html
• https://docs.python.org/3/library/functions.html#func-range
• https://docs.python.org/3/library/stdtypes.html#typesseq-range