Python range( ) Function in 30 seconds

Published by Abhay Rastogi on

python range

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

range(start, stop[, step])

Parameter Description
STARTStarting integer value is optional.(default 0)
STOPStop integer value is required.
STEPStep 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(startstop[, 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
Categories: python

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *