Python range() loop function

Here we will see what the Python range() function does;which is also known as the python range loop.The term range step will be also discussed here.

Python range()

The range() function generate a range of values starting from 0.The end value generated is one less than the value passed as the argument to the range().So to generate numbers from 0 to 10 you have pass 11 as the argument.

Link : Python for loop

>>> for num in range(11):
	print( num )

	
0
1
2
3
4
5
6
7
8
9
10

We can also obtain negative range of values from range() function,we will see that later.

What happens if we just try to print a range().

>>> print( range(10) )
range(0, 10)

The output is just an object that returns a successive items when you iterate over.


Passing arguments to range() and range step

The range() function can accept one two or three arguments.When one argument is passed to range() it signify the end value of the range of numbers the function can generate.And the code example given above is an instance of such case.

Passing two arguments to range()

When two arguments is passed :
 
i)The first argument determine the starting value of the range of number the function should generate.
 
ii)The first argument is the starting value of the range of value the function will generate.The second argument determine the end point of the range of value,with the last value of the range being one smaller than the second argument.

>>> for num in range( 4 , 15 ):
	print( num )

	
4
5
6
7
8
9
10
11
12
13
14

Since 15 is the second argument the last value should be one lesser than 15 hence 14 is the last value of the range.

By passing two arguments we can also generate a range of negative values.

>>> for num in range( -8 , 0 ):
	print( num )

	
-8
-7
-6
-5
-4
-3
-2
-1


Passing three arguments with the range step

The range() function can also accept three arguments:
 
i)The first argument is the starting value of the range to be generated.
 
ii)Teh second argument is the end point of the range of the value generated with the last value being one lesser than the second argument.
 
iii)The third argument is known as the range step.Using this argument we can step over some of the values when the range is generated.Suppose 3 is pass as step then two numbers are always skip in the range generated.The stepping of numbers start from the first number of the range.

>>> for num in range( 2 , 19 , 3 ):
	print( num )

	
2
5
8
11
14
17

In the range the first number is 2 after which two numbers are skipped and then 5 is taken into account,after which two numbers are skipped again and thus 8 is taken as the number and so on.At the end after 17 is generated two numbers are skipped and the number obtained is 20 but the end point of the range is 19 so 20 is not accounted.

Another example is shown below.

>>> for num in range( -15 , 14 , 5 ):
	print( num )

	
-15
-10
-5
0
5
10

Here 5 is pass as the range step so 4 numbers are skipped each time a value is generated.


Use of range()

As we have seen range() can be use to generate a different type of range of numbers.But one specific an important use of range() is to generate items for data types like list ,tuple,set and dictionary.

Link : Dictionary fromkeys() method

>>> #creating list from python
>>> ls=list(range (6))
>>> ls
[0, 1, 2, 3, 4, 5]
>>> ls1=list( range(23 , 90 , 8) )
>>> ls1
[23, 31, 39, 47, 55, 63, 71, 79, 87]
>>> #creating dictionary using range()
>>> d={}
>>> d1=d.fromkeys( range(5) , 'text')
>>> d1
{0: 'text', 1: 'text', 2: 'text', 3: 'text', 4: 'text'}


Leave a Reply

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