Python range() Function

Introduction

Before we begin talking about the basics of Python range() function, let's briefly understand – What is Python range() function?

Python range() function is a built-in function used to generate a sequence of numbers. It is widely used in loops and iterations. The range() function takes up to three arguments: start, stop, and step. By specifying these arguments, you can create a range of numbers starting from the 'start' value, up to but not including the 'stop' value, with an increment of 'step'.

This versatile function is commonly used to iterate over a specific range of values, making it essential for tasks like looping, list comprehension, and indexing. Understanding the range() function is crucial for efficient programming in Python.

In this tutorial, we will explain the fundamentals of Python range() Function. We will also address a few FAQs on Python range() function.

Advantages of Python range() function

  1. Simplicity: Python's range() function provides a straightforward way to generate number sequences without complicated logic.
  2. Memory Efficiency: It generates numbers on-the-fly, making it memory-efficient for large ranges.
  3. Loop Control: It simplifies loop control by specifying the start, stop, and step values.
  4. Versatility: It is used in various contexts like list comprehension and indexing.
  5. Performance: It improves performance by avoiding unnecessary memory allocation and iteration overheads.

Python range() syntax

There are several variations of the range constructor:

range(stop)
range(start, stop[, step])

The range constructor accepts only integer arguments. The use of floating numbers and other types is prohibited.

One mandatory and two optional arguments are accepted by range. It generates the numbers on demand and returns a range object that represents the provided range.

Python range(stop)

range returns a sequence of numbers, incremented by 1, starting from 0 till stop - 1, when only one argument is specified.

Here is an example of the range type in use:

for i in range(5):
    print(i)

The generated sequence of numbers begins with 0 and ends with 4 (5-1):

Output

0
1
2
3
4

range returns an empty sequence if the argument is 0 or a negative integer:

print(list(range(-5)))

We are transforming the range object to a list because the range performs a slow evaluation of the integer sequence. An empty list appears as an output:

Output

[]

When two arguments are given, the range returns a sequence of numbers, each one increased by 1, ranging from start to stop – 1.

An example is shown below:

for i in range(3, 5):
    print(i)
Output

3
4

The stop argument must be greater than the start argument. The sequence is empty otherwise:

print(list(range(5, 3)))
Output

[]

Positive, negative, and 0 digits can be used as arguments:

print(list(range(-5, -3)))
Output

[-5, -4]
print(list(range(-3, 0)))
Output

[-3, -2, -1]

Python range(start, stop, step)

range returns a series of numbers that are incremented or decremented by step, starting from start to stop - 1, when three arguments are provided.

If step is positive, range returns an incrementing sequence:

for i in range(0, 26, 5):
    print(i)
Output

0
5
10
15
20
25

The stop argument must be greater than the start argument while incrementing. The sequence is empty if not.

range returns a sequence that decrements if step is negative:

for i in range(20, 4, -5):
    print(i)
Output

20
15
10
5

The stop argument must be lesser than the start argument while decrementing. The sequence is empty if not.

A ValueError exception is raised if step is 0:

Output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: range() arg 3 must not be zero

FAQs About Python range() Function

What is the default value for the start argument?

The default value for the start argument is 0.

Can the range() function generate a sequence in reverse?

Yes, by providing a negative step value, the range() function can generate numbers in reverse order.

How can I use the range() function in a loop?

You can use the range() function as an iterator in a for loop to iterate over a specific range of values.

Can I use floating-point values with the range() function?

No, the range() function only accepts integers as arguments.

Is the stop value included in the generated sequence?

No, the stop value is not included. The sequence generated by the range() function stops one step before the stop value.

Can I create an empty sequence using the range() function?

No, the range() function requires at least one argument to generate a sequence.

How can I convert the range() function output into a list?

You can use the list() function to convert the output of the range() function into a list.

Conclusion

We hope this tutorial, helped you understand the fundamentals of Python range() Function.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them.