Python range() Function Explained With Examples | Tutorial
Have any questions?
Free: +1 888 502 4472

Python range() is one of the built-in function which is used for performing operations with a sequence of numbers. In this tutorial, we will take a look into range() function with simple examples and also some advantages of using this particular function in your Python development.

Let’s get started!

What is range() function in Python?

The range() method in Python web development  is a built-in function that returns the object which is a sequence of integers. Python range() generates the numbers between the given start integer and the stop integer, which can be used in iterating over the loop.

The range() function takes three arguments. Start and Step are the optional arguments. The first argument is a lower limit denoting at which position to start. The default value is 0 if not specified. The second argument is an upper limit which denotes numbers generated up to this value. This function doesn’t include this value in the result. The last argument is a difference between each number in the result. If not specified, the default value of the step parameter is 1.

#### Example

Only one argument in range() function.

#### Example

Two arguments in range() function.

#### Example

Three arguments in range() function.

## Rules for range() function arguments

The range() function only works with integers i.e., whole numbers. All arguments must be integers. You can not pass a string or float number or any other type in a start, stop and step argument of a range(). Ensure that step value is not zero. Python range() function raises a ValueError exception, if a step value is 0.

#### Example

Decrementing with range() using a -ve step.

#### Example

Decrementing with range() from –ve to +ve number.

#### Example

Generating values with range() from +ve to -ve number.

## Reverse range()

One of the most interesting features about using python range() method is, we can generate a sequence of integers in a descending or reverse order by using a negative step value. To enable this feature, we need to set the step argument of the range() method to value -1.

#### Example

Creating values with reverse order.

#### Output

Using range() function, we can create a list in python. Python V3’s range uses the generator, by using python V3’s range() we can generate value as when for loop iteration asked for it. i.e., the range() doesn’t produce all numbers at once.

We can convert the range() output into the list since the range() function returns an immutable sequence of integers. Use the list method to convert range output to list.

## Python range() Vs xrange() functions

When compared to range() function, xrange() also returns the generator object which can be used to iterate numbers only by looping. The main disadvantage of xrange() is that only a particular range is displayed on demand and hence it is “lazy evaluation“.

#### Example

Using xrange() function

Hope you find this helpful. If you are a Python beginner, by now you would have a clear idea of Python range() method. I’ve covered all the basic usage of this function with sufficient and simple examples. If you have any further doubts or suggestions comment it below. We will get back to you soon.

Stuck on starting your Python Project? Get started immediately with our team of Python experts at Agira. Hire your Python developer now!

Turn your vision to magnificent reality With
Our Web and Mobile Solutions
The following two tabs change content below.

#### Deepeka Rani

A Junior Software Developer, specializing in JavaScript, Jquery, Python, and NodeJS. This vibrant techie loves to look for updates and try out something new in Technology. When she isn't programming, she will be either exploring places that hold great history or gaming with her friends.

#### Latest posts by Deepeka Rani (see all)

Join our 30,000+ subscribers, never miss out anything on our latest blogs, tips, tutorials, updates & more.