range Function in Python

In this class, we discuss range function in python.

For Complete YouTube Video: Click Here

range Function

Before we go and discuss about range function. we should have some basic on operators, and conditional statements. Click here.

Here we understand what is range function and how we generate numbers using range function.

Use of this function will be understood in our next classes.

Examples:

range(6) This is going to generate numbers from 0 to 5.

The values that are generate are 0,1,2,3,4,5.

One number less than the given number. By default starting number is considered as 0.

General parameters of range function are given as below.

range(start,stop,step)

start takes the starting integer value.

Stop take the ending integer value. Number generation stops one value less than the stop value.

step take the step count. ie how many steps to take before generating new number.

We discuss many examples below.

range(5,10). Here we gave only two inputs. It will consider as start and stop values.

The numbers generated by the above function are 5,6,7,8,9.

Example:

range(5,20,2) three inputs are given step count is given 2.

The output numbers generated by the above function are 5,7,9,11,13,15,17,19.

Example:

range(-2,5) the output is -2,-1,0,1,2,3,4.

Example:

range(8,1,-1) In order to generate values in reverse order this example helps.

If the step count is negative value. numbers are generated in reverse.

The output for the given function are 8,7,6,5,4,3,2.

We can achieve this reverse order using the reversed function.

Example:

reversed(range(2,8)) will generate an output 7,6,5,4,3,2.