Python range( ) Function in 30 seconds
Python range( )
A series of numbers between the start integer and the stop integer is created from the python range() function.
The benefit of the range type over a standard list or a tuple is that a range object will still take the same (small) amount of memory, regardless of its sizes (as only the start, stop and phase values are stored, each variable measured according to the requirements).
range parameters
Syntax
Parameter | Description |
---|---|
START | Starting integer value is optional.(default 0) |
STOP | Stop integer value is required. |
STEP | Step integer value is optional.(default 1) |
range
(stop)
range function can easily print numbers just bypassing the single value as stop integer.it will print till n-1.
# range(stop)
>>> list(range(9))
[0, 1, 2, 3, 4, 5, 6, 7, 8]
# OR
>>> r = range(8)
>>> for num in r:
print(num)
1
2
3
4
5
6
7
range
(start,stop)
start and stop values can also passed from where to start number and printing the last one.
# range(start,stop)
>>> list(range(6, 18))
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
# OR
>>> r = range(6, 18)
>>> for num in x:
print(num)
6
7
8
9
10
11
12
13
14
15
16
17
range
(start, stop[, step])
range function also have step for skipping the values and then print.
# range(start, stop[, step])
>>> list(range(6, 18, 2))
[6, 8, 10, 12, 14, 16]
# OR
>>> r = range(6, 18, 2)
>>> for num in r :
print(num)
6
8
10
12
14
16
python range reverse
The python range will print the value in reverse order by simply bypassing the negative value in the third argument and giving the highest value in the first.
# range(start, stop[, step])
>>> list(range(18, 2, -2))
[18, 16, 14, 12, 10, 8, 6, 4]
#or
>>> r = range(18, 2, -2)
>>> for num in r :
print(num)
18
16
14
12
10
8
6
4
0 Comments