Example HTML page

The range( ) function

range ( ) function

The range( ) function is the built-in function that is used to return the sequence of numbers between the start number to the end number(excluding the last number). Assume that you have given range from 0-5 i.e range(0,5) it prints all the numbers between 0 and 5(0,1,2,3 and 4) but not 5.

There are three parameters for range( ) function in Python

  1. Start-Starting number in the sequence.
  2. Stop– Prints upto this number by excluding it
  3. Step – difference between each number in the sequence.(optional)

Syntax:

range(start,stop,[step])

range( ) is illustrated in the following examples

>>> range(1)

range(0, 1)

>>> range(-1)

range(0, -1)

>>> range(0)

range(0, 0)

 

>>> c=range(1,8)

>>> for y in c:

print(y)

 

 

1

2

3

4

5

6

7

Note: If we want to use range( ) then you would have to use any list.

>>> for x in range(7):                                                  #Using one parameter

print(x)

 

 

0

1

2

3

4

5

6

If you don’t specify the starting number it takes ‘0’ by default.

range( ) using two parameters

>>> for y in range(2,6):                                        #Using two parameters with no step count

print(y)

 

 

2

3

4

5

range( ) using three parameters

>>> for z in range(3,15,1):                                     #Using three parameters with step count 1

print(z)

 

 

3

4

5

6

7

8

9

10

11

12

13

14

Here in the above code 3 is starting number, 15 is the ending number whereas 1 is the step count. Hence with ‘1’ as the difference the numbers will print from starting to end number.

>>> for z in range(3,15,2):                                    #Using three parameters with step count 2

print(z)

 

 

3

5

7

9

11

13

Here in the above code 3 is starting number, 15 is the ending number whereas 2 is the step count. Hence with ‘2’ as the difference the numbers will print from starting to end number.

>>> for z in range(3,-15,-1):                                            #With step count -1

print(z)

 

 

3

2

1

0

-1

-2

-3

-4

-5

-6

-7

-8

-9

-10

-11

-12

-13

-14

Another example of range( ) using three parameters with -1 as its step count

>>> for z in range(0,-15,-1):

print(z)

 

 

0

-1

-2

-3

-4

-5

-6

-7

-8

-9

-10

-11

-12

-13

-14

Another example of range( ) using three parameters with ‘0’ as the last number

>>> for z in range(15,0,-1):

print(z)

 

 

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

An example program for the multiplication of table 5.

>>> n=int(input(“The multiplication of :”))

The multiplication of : 5

>>> for b in range(1,11):

print(n,’x’,b,’=’,n*b)

 

 

5 x 1 = 5

5 x 2 = 10

5 x 3 = 15

5 x 4 = 20

5 x 5 = 25

5 x 6 = 30

5 x 7 = 35

5 x 8 = 40

5 x 9 = 45

5 x 10 = 50

Explanation: Here we took the range between 1 and 11 i.e., starting number is 1 and ending number is 11(because it excludes the ending number and prints upto 10)

For the same example with the step count 2 is shown in below:

>>> n=int(input(“The multiplication of :”))

The multiplication of : 5

>>> for b in range(1,11,2):

print(n,’x’,b,’=’,n*b)

 

 

5 x 1 = 5

5 x 3 = 15

5 x 5 = 25

5 x 7 = 35

5 x 9 = 45

 

Example HTML page

Leave a Reply

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

Pin It on Pinterest