Methods and Functions

Python Range

The python range is a built-in function. The range() function prints a list or sequence of numbers, beginning from 0 by default, and increments the return value by 1 (by default), and stops the list at a specified number given by the user. range() function is just a renamed version of the xrange() function which was used in Python2.

Basics of Python Range

range python

Range() python function is a popular and widely used function in Python, particularly when you are working with for and while loops. It is immutable (whose value can not be changed) and returns a sequence of numbers.

Python range Syntax

range(start, stop, step)

Parameters of range python

The range() function consumes less memory compared to tuples or lists, as it only stores the start, stop, and step values. The range() function takes in three range parameters.

range(stop_value) A range() function defined like this, returns a sequence of numbers, by default considering starting value as 0 and stops at the provided stop_value.
range(start_value, stop_value) A range() function defined like this, returns a sequence of numbers starting from the provided start value, increments by 1 value, and stops at the provided stop_value.
range(start_value, stop_value, step_value) A range() function defined like this returns a sequence of numbers from the provided start value to stop value with the difference between the numbers being equivalent to the step value.

Return value from python range function

The range() function returns a sequence of numbers in the range specified by the programmer. Now, let us find out how to return values using the range() function.

                    

For seq in range(5):
    print(seq)

The above code returns a sequence of numbers like this:

Since we gave only the stop value it by default started from 0 and stopped before the upper bound value which is 5.

Examples of range() python

Let us look at some examples of how to use the range() function to get more idea on how it works and how to use it.

Example 1: Working of range in python

A. Using both start and stop values

                    

for seq in range(1,10):
    print(seq)

The above code returns a sequence of numbers like this:

Since we have given the starting value this time, it returned a sequence of numbers starting from 1 instead of 0.

B. Using all the start, stop, and step values

                    

for seq in range(10,100,10):
    print(seq)

The above code returns a sequence of numbers like this:

                    

10

20

30

40

50

60

70

80

90

Since we have given the step value is 10, it returned a sequence of numbers with differences between them equivalent to the provided step value.

Example 2: Creating a list of even number between the given numbers using range() python

If we want to create a list of even numbers from 1 to 100 then we can do it using the range() function in the following way:

                    

for seq in range(0,100,2):
    print(seq)

The above code prints a sequence of numbers like this:

Since we have given the step value as 2 and the starting value as 0, it returned a sequence of numbers with the difference between them as 2 making a list of even numbers from 1 to 100.

Example 3: How range() python works with negative steps?

In Python range  function works for the negative numbers in the same way as it works for the positive numbers. you just need to provide the negative start value and the negative stop value and it returns the sequence of negative numbers in the given range.

                    

for seq in range(-10, -5):
    print(seq)

The above code prints a sequence of numbers like this:

Since we have given -10 as the starting value and -5 as the upper bound value it returned a sequence of negative numbers in the range -10 to -5.

FAQs on python range()

Here are some frequently asked questions with answers to give you a quick brief on how the range() function works in Python.

Q1. How do you find the range in Python?

A. In Python range() function is used to find the numbers in the given range of numbers. You can use the range() function by giving in the start and stop values for which you need to find the range in the following way

range(start_value, stop_value)

For instance. if you want to find the range of numbers between -23 and 76, you can find it by using the range function in the following way:

This code returns the values in the range -23 to 76.

                    

for seq in range(-23,76):
    print(seq)

Q2. How do you range a number in Python?

A. You can write a range of numbers in Python by using the range() function. You must give the numbers for which you need to find the range as parameters to the range() function.

For instance, if you want to find the range of numbers from 1 to 100 with a minimum difference of 5, you can do it by writing the following code

                    

for seq in range(1,100,5):
    print(seq)

Q3. How does Range () work in Python?

A. The range() function in Python works by taking three inputs as parameters which are start_value, stop_value, and step value. The range() function returns an immutable sequence of numbers starting from the start_value, and increments the values by the value given in the step_value, and stops at the stop_value.

Syntax:

range(start_value, stop_value, step_value)

Q4. Does the range in Python start at 0 or 1?

A. Range() in Python starts at 0 by default.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.