The range() function

The range() is an inbuilt-in function that generates the integer numbers between a given range.  The range is between start integer and stop integer-1. It  returns a  range object.

The range object can be iterated using the for loop .

Syntax and arguments

range(start, stop[, step])

It has two variants

range(stop)

​​​​​​​range(start, stop, [step])

  1. Start: It is optional, by default, it starts with 0 if not specified. It is  a starting number of the range object
  2. Stop: It is an upper limit. The range() doesn’t include stop number in the resultant range object.
  3. Step It is optional, by default its value is 1. Step  is a difference between each number in the result. The value of the step can be positive or negative.

Iterating range using for loop

Example range() with stop

# range(stop) Example

for number in range(5):

             print(number, end=' ')

How range() with stop argument works

The range object returns  range object start with 0  and ends with 5  (stop -1) .

For loop iterates range object and print number o to 4

Output

0 1 2 3 4

Example range() with start, stop

# range(stop) Example

for number in range(1,5):

          print(number, end=' ')

How range() with start, stop argument works

The range object returns  range object start with 1  and ends with 5  (stop -1) .

For loop iterates range object and print number 1 to 4

Output

1 2 3 4

​​​​​​​Example range() with start, stop, step

# range(stop) Example

for number in range(2,22,2):

             print(number, end=' ')

How range() with start, stop argument work

The range object returns  range object start with 2  and ends with 20  (22 -2 {step} )

For loop iterates range object and print number  from 2   to  20, 2, 4 , 6 ….20

Output

2 4 6 8 10 12 14 16 18 20

Points to remember

The function range() only works with the integers.

Start, Stop, Step can be positive or negative.

 If the step is zero ValueError exception is raised.

 

Inclusive range

The range() function is return the range that excludes the stop, to make the range(stop) inclusive we can play a small trick, you have to add the step into the stop  t0 make  range() function inclusive.

start =1

stop = 5

step = 1

stop = stop + step

   for i in range(start, stop, step):

    print(i, end=' ')

Output

2 4 6 8 10 12 14 16 18 20

Convert range() to List

The range() can be converted to the Python list by using function list(). The range is passed as an argument to function list. For example.

print("Converting range() to list")

_list = list( range(1, 10, 2))

print("printing new list", _list)

 

Output

Converting range() to list

Printing new list [1, 3, 5, 7, 9]

Accessing range()  elements using index value

Examples

print(range(1,11)[0])

print(range(-1,-10,-1)[4])

 

print(range(2,22,2)[0])

print(range(2,22,2)[1])

print(range(2,22,2)[2])

print(range(2,22,2)[3])

print(range(2,22,2)[4])

print(range(2,22,2)[5])

print(range(2,22,2)[6])

print(range(2,22,2)[7])

print(range(2,22,2)[8])

print(range(2,22,2)[9])

Output

1

-5

2

4

6

8

10

12

14

16

18

20