Range Function in Python-Complete Tutorial

Range in Python

This Range Function in Python-Complete Tutorial is made for you to give complete knowledge of Range Function. At the end of this tutorial, you will have full knowledge of Range Function in Python.

Hello, & Welcome! to the Range Function in Python-Complete Tutorial.

Part 8- Range() Function in Python-Complete Tutorial

In this article of Python Tutorial, you will learn following-

  1. What is the Range() Function?
  2. Syntax of Range() Function.
  3. Range() Function arguements.
  4. Some Range() Function Examples.
  5. Range() Function with loop.
  6. Python Range Reverse.

What is the Range() Function?

Range() Function basically generates a list of numbers for us, which we can use to iterate over with for loop.

Let’s understand the range() function more deeply with the help of an example.

print("Show numbers from the range of 0 to 8")
for n in range(8):
    print(n, end=',')

OUTPUT-
Show numbers from the range of 0 to 8
0,1,2,3,4,5,6,7
  • Here, in the example, it creates a range from 0 to 7. It doesn’t include the last number, that’s why 8 is not printed.

Syntax of Range() Function.

Syntax of Range() Function –

for variable_name in range(start_point,end_point,step_no.)
  • Here, variable_name is the name of the variable. It may be anything. Inside the range(), there are three arguments, in which two are optional. end_point is a compulsory argument, rest two are optional.

Range() Function arguements.

As you see in the syntax of range() function, there are three arguments, two are optional- start_point and step_no.

Let’s discussed these arguments in detail.

  1. Start Point- it is the start point of the range, from where the range starts. If you didn’t define the start-no, then it will start from 0. It’s by default value is 0.
  2. End Point- This is also known as the upper limit of the range. This is a compulsory argument in the range. It prints the number up to this upper limit, but it doesn’t print the endpoint number. For example, if you define endpoint as 8 so range end at 7 (endpoint-1)
  3. Step Number- The step number is the difference or gap between two numbers. If you don’t define step number, then it assumes it as 1. The default value of the step number is 1. For example, if you want to print the Table of 2, then you need to pass the step number as 2, so it will print the number in the gap of 2.

Some Range() Function Examples.

Example 1: Only define End Point-

If you want to define only the endpoint and don’t define the start point and step number, then you can do it by the following example-

for n in range(7)
   print(n, end=',')

OUTPUT-
0,1,2,3,4,5,6
  • Here, start point and step number is not defined, that’s why it considers as start point=0 and step number=1

Example 2: Only define Start Point and End Point-

If you wanna define the start point and endpoint of the range, then you can do it by the following example-

for n in range(3,10):
    print(n, end=',')

OUTPUT-
3,4,5,6,7,8,9
  • Here, you define the start point as 3 and endpoint as 10, that’s why it starts from 3 up to 9 {(endpoint-1)->(10-1) =9}

Example 3: Define Start Point, End Point, and Step Number

If you wanna define all three arguments in the range, then you can do it by the following example-

for n in range(0,20,4):
   print(n, end=',')

OUTPUT-
0,4,8,12,16
  • Here, 0 represents the start point, 20 represents the endpoint and 4 represents the step number. As step number is 4, that’s why you can see numbers are in the gap of 4 like- 0+4=4, 4+4=8 and so on.

Range() Function with loop.

As we learn in for() loop tutorial, for() loop is used to perform some operation repeatedly until the condition gets false. We can use loop function in the range.

By using for() loop, the range() function doesn’t produce all numbers in one go. The range() function produces the next number only when for loop iteration gave an order to produce.

Let’s understand with the help of an example-

burgers= ['cheesy','chicken','veg','supreme','double']
for n in range(len(burgers)):
   print(n,burgers[n])

OUTPUT-
0   cheesy
1   chicken
2   veg
3   supreme
4   double
  • Here, by using len(burgers), we can get total elements of a list.
  • In print(n,burgers[n]), n represents the index position and burgers[n] shows the item in every specific index position.
  • The variable n is not getting the all values 0,1,2,3,4 at one go. First, n starts at 0, then n=1 and so on.

Python Range Reverse.

If you want to reverse the range numbers, which means start it from the endpoint to start point, then you can do it by using negative numbers.

Let’s consider the previous example of Burgers if you want to reverse the list, like-

4  double
3  supreme
2  veg
1  chicken
0  cheesy

So, you can do it by providing -1 as the start point, endpoint, and step number. Let’s understand with the help of an example-

burgers= ['cheesy','chicken','veg','supreme','double']
for n in range(len(burgers-1,-1,-1)):
   print(n,burgers[n])

OUTPUT-
4  double
3  supreme
2  veg
1  chicken
0  cheesy
  • Here, (burgers-1) represents the start point as 4(reverse order), next -1 shows the endpoint as 0, and the third -1 represents the step number.

Note-

  • Range() function arguments must be only integers. You can’t pass float or string in start point, endpoint, and step numbers.
  • You can’t provide 0 in the step number, it gives you an error.

That’s all for Range Function in Python, I hope now you have a better understanding of Range Function.

congratulations! You successfully learned Range() Function in Python.

Range() Function in Python-Complete Tutorial

In the next tutorial, we will start learning Functions in Python

Till then, Enjoy Learning Python!

Are you looking for Best Books on Python for Beginners and for Intermediate/Experts?

Check these Books, These Books are the best selling and covered everything in detail.

I hope you will find these Books helpful-

Best Selling Python Books for Beginners

Best Selling Python Books for Intermediates/Experts

All the Best!

Back to Python Tutorial

Thank YOU!

Though of the Day…

“Live as if you were to die tomorrow. Learn as if you were to live forever.” 

Mahatma Gandhi

author image

Written By Aqsa Zafar

Founder of MLTUT, Machine Learning Ph.D. scholar at Dayananda Sagar University. Research on social media depression detection. Create tutorials on ML and data science for diverse applications. Passionate about sharing knowledge through website and social media.

Leave a Comment

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