range in python list


range( ) in Python.

In this post we will learn about range( ) in Python. Let's start..!

Range is a type of Sequence. What it means ? Sequence is simply a numbers ( positive numbers and negative numbers ). For iterating a these numbers we uses range( ) function. 

But What is iteration ? 

Iteration :

It simply means a taking a number or value one by one. 

for ex. Suppose we have a box having 10 gifts inside it. Also we have 10 students and now you have to give one gift to each students. Simply you will take gifts one by one from box and will give it one to each student. The process of taking one gift and giving to a student this is same as the concept of iteration. 

Now How to use range( ) function ? 

To use range( ) function simply write a range( parameter ). But How many parameters ?  This function can take three parameters. Third parameter is optional. We can give variable or actual value as a parameter in range( ) function.




1} single parameter :

Syntax : range( ending_number )

If you give a single parameter in range( ) function this will start iterating always from zero (0) to ending_number - 1. ( ending_number means the parameter number given by you )

for ex. range( 10 )  Here ending_number is 10 so minus 1 from 10 i.e. 10 - 1 = 9. So this will iterate values from 0 to 9. ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ). 

We can store these values into a list , tuple , or set. Create a variable to store the list; give equal to sign and simply write a list( range( ending_number ) ) . This will convert this iterated range into a list. 


# creating list using range() function
mylist = list(range(10))

print(mylist)

Output :

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


[ if you don't know about datatypes in or how to create a list and how to work with list just click here to learn more about datatypes in Python ]


2} two parameter : 

Syntax : range( starting_number , ending_number )

If you give a two parameter in range() function this will start iterating from starting_number to ending_number - 1. ( starting_number and ending_number means the parameter number given by you )

for ex. range( 5 , 10 )  Here starting_number is 5 and ending_number is 10 so minus 1 from 10 i.e. 10 - 1 = 9. So this will iterate values from 5 to 9. ( 5, 6, 7, 8, 9 ). 

We can store these values into a list , tuple , or set. Create a variable to store the list; give equal to sign and simply write a list( range( starting_number , ending_number ) ) . This will convert this iterated range into a list. 


# creating list using range() function with two parameters
mylist = list(range( 5, 10 ))

print(mylist)

Output :

[5, 6, 7, 8, 9]


3} three parameter : ( ascending order)

Syntax : range( starting_number , ending_number , difference )

If you give a three parameter in range() function this will start iterating from starting_number to ending_number - 1; but with a difference. ( starting_number , ending_number and difference means the parameter number given by you

for ex. range( 5 , 20 , 3 )  Here starting_number is 5 and ending_number is 20 so minus 1 from 20 i.e. 20 - 1 = 19. So this will iterate values from 5 to 19 with difference of 3. ( 5, 8, 11, 14, 17 ). 

We can store these values into a list , tuple , or set. Create a variable to store the list; give equal to sign and simply write a list( range( starting_number , ending_number , difference ) ) . This will convert this iterated range into a list. 


# creating list using range() function with three parameters
mylist = list(range( 5, 20, 3 ))

print(mylist)

Output :

[5, 8, 11, 14, 17]


4} three parameter : ( descending order)

The order of these numbers is ascending order. If you want these numbers in descending order then your starting_number should be greater than ending_number and your difference should be in minus (-).

for ex. range( 15 , 2 , -2 )  Here starting_number is 15 and ending_number is 2 for descending order you should add 1 to the ending number. So add 1 to 2 i.e. 2 + 1 = 3. So this will iterate values from 15 to 3 in descending order with difference of -2. ( 15, 13, 11, 9, 7, 5, 3 ). 

We can store these values into a list , tuple , or set. Create a variable to store the list; give equal to sign and simply write a list( range( starting_number , ending_number , difference ) ) . This will convert this iterated range into a list.


# creating list using range() function with three parameters in descending order
mylist = list(range( 15, 2, -2 ))

print(mylist)

Output :

[15, 13, 11, 9, 7, 5, 3]


This is all about range( ) function in Python.


If you have any doubts in this post then please ask me in comment section below. I will try to solve it.


If you like these post and you have learned something from this then share this post with your friends and family.

Keep learning always.....!!! 😇😇😇




Previous Post  Next Post