Python range () function

Python Methods and Functions

range () is usually used for looping, so knowing the same is key when working with any kind of Python code. The most common use of the range () function in Python is — it is an iteration of a sequence type (List, string, etc.) with a for and a while loop.

Python Basics range () :
Simply put, range () allows the user to generate a series of numbers within a given range. Depending on how many arguments the user passes to the function, the user can decide where this series of numbers starts and ends, and how big the difference is between one number and the next. range () takes basically three arguments.

  • start : an integer from which a sequence of integers should be returned
  • stop: an integer before which a sequence of integers should be returned.
    Range of integers ending in the foot — 1.
  • step: an integer value that defines the increment between each integer in the sequence

# Python program for
# show range () basics

  
# print number

for i in range ( 10 ):

print (i, end = "" )

print ()

  
# using a range for iteration

l = [ 10 , 20 , 30 , 40 ]

for i in range ( len (l)):

print (l [i], end = "" )

print ()

 
# doing the sum of naturals
#number

sum = 0

for i in range ( 1 , 11 ):

sum = sum + i

print ( "Sum of first 10 natural number:" , sum )

Output:

 0 1 2 3 4 5 6 7 8 9 10 20 30 40 Sum of first 10 natural number: 55 

There are three ways to call range ():

  • range (stop) takes one argument.
  • range (start, stop) takes two arguments.
  • range (start, stop, step) takes three arguments.

Range (stop)

When the user calls range () with one argument, the user will receive a series of numbers starting at 0 and including all integers up to but not including the numbers that the user provided as a stop. For example —

# Python program for
# print an integer
# using range ()

 
# printing the first 10
# integer

for i in range ( 10 ):

  print (i, end = "" )

print ()

 
# print the first 20
# integer

for i in range ( 20 ):

print (i, end = " " )

Exit:

 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 

range (start, stop)

When the user calls range () with with two arguments, the user has to decide not only where the series of numbers ends, but also where it starts, so the user doesn`t have to start at 0 all the time. User can use range () to generate a series of numbers from X to Y using the range (X, Y). For example —

# Python program for
# print a natural number
# using a range

 
# printing natural
# up to 20

for i in range ( 1 , 20 ):

print (i, end = "" )

print ()

 
# print natural
# 5 to 20

for i in range ( 5 , 20 ):

print (i, end = " " )

Exit:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 

range (start, stop, step)

When the user calls range () with three arguments, the user can choose not only where the series of numbers will start and stop, but also how great the difference will be between one number and the next. If the user does not provide a step, then range () will automatically behave as if the step is 1.

# Python program for
# print all numbers
# divisible by 3 and 5

 
# use range for printing the number
# divisible by 3

for i in range ( 0 , 30 , 3 ):

print (i, end = "" )

print ()

 
# use a range to print the number
# divisible by 5

for   i in range ( 0 , 50 , 5 ):

print (i, end = " " )

  

Output:

 0 3 6 9 12 15 18 21 24 27 0 5 10 15 20 25 30 35 40 45 

In this example we are printing an even number from 0 to 10, so we select the starting point from 0 (start = 0) and stop the series at 10 (stop = 10). To print an even number, the difference between one number and the next must be 2 (step = 2), after providing a step, we get the following output (0, 2, 4, 8).

Increase with a range using a positive step:
If the user wants to increase, then they need step to be a positive number. For example:

# Python program for
# increase from
# assortment ()

 
# increases by 2

for i in range ( 2 , 25 , 2 ):

  print ( i, end = "" )

print ()

 
# increased by 4

for i in range ( 0 , 30 , 4 ):

print (i, end = "" )

print ()

  
# increased by 3

for i in range ( 15 , 25 , 3 ):

print (i, end = "" )

Output:

 2 4 6 8 10 12 14 16 18 20 22 24 0 4 8 12 16 20 24 28 15 18 21 24 

Decrease with a range using a negative steps:
If the user wants to decrease, then he needs step to be a negative number. For example:

# Python program for
# decrease with
# assortment ()

 
# increases by -2

for i in range ( 25 , 2 , - 2 ):

  print (i, end = "" )

print ()

 
# increases by -4

for i in range ( 30 , 1 , - 4 ):

print (i, end = "" )

print ()

 
# increases by -3

for i in range ( 25 , - 6 , - 3 ):

print (i, end = "" )

Output:

 25 23 21 19 17 15 13 11 9 7 5 3 30 26 22 18 14 10 6 2 25 22 19 16 13 10 7 4 1 -2 -5  

Using floating point numbers in the Python () range:
The Python range () function does not support floating point numbers. those. the user cannot use a floating point or non-integer number in any of his arguments. The user can only use whole numbers. For example

# Python program for
# show with float
# number in range ()

 
# using floating point number

for i in range ( 3.3 ):

print (i)

 
# using float

for i in range ( 5.5 ):

  print (i)

Output:

 for i in range (3.3): TypeError: `float` object cannot be interpreted as an integer  

Things to remember about the Python range () function:

  • range () only works with integers, that is, integers.
  • All arguments must be integers. The user cannot pass a string or floating point number or any other type in the start , stop and step range () arguments.
  • All three arguments can be positive or negative.
  • The step value must not be null. If the stride is zero, Python raises a ValueError.
  • range () — this is a type in Python
  • # Python program for
    # show range () type

     
    # type checking
    # assortment

    type ( range ( 3 ))

  • The user can access the elements in range () by index, so the same as the user does with the list:
  • # Python program for
    # access to elements in the range

      
    # access to elements

    range ( 3 ) [ 1 ]

     
    # access to elements

    range ( 3 ) [ 2 ]





Get Solution for free from DataCamp guru