range() function in Python

Video Tutorial
FREE
Range Function thumbnail
This video belongs to
Python Course for Beginners With Certification: Mastering the Essentials
16 modules
Certificate
Topics Covered

range() function in Python serves as a versatile tool for generating sequences of numbers, commonly employed in for loops. This function allows the definition of iterable objects with specific start (inclusive), stop (exclusive), and step values, offering flexibility in sequence creation.

Example:

In this example, the range function efficiently creates a sequence starting from 1, incrementing by 2 until reaching, but not including, 10. This showcases the utility of range in constructing tailored number sequences for various programming needs.

Syntax of range() function in Python

Parameters of range() function in python

The range() function in Python can take up to three parameters:

  • start – It indicates the beginning integer of the sequence you want to generate. It is optional and set to 0 by default.
  • stop – It indicates the integer at which you want to stop. It is mandatory to mention and is exclusive, i.e., it is not included in the sequence generated.
  • step – It indicates the difference between consecutive integers in the sequence. It specifies the value of the incrementation or decrementation step. By default, it is set to +1 and is optional.

Return Values of range() function in python

The range() function in Python does not return a list of numbers, but rather a sequence object that generates the numbers on the fly as you iterate over it. This makes it memory-efficient for working with large ranges of numbers.

When you use the range() function in a loop or pass it to a function that expects a sequence of numbers, the numbers are generated on the fly and returned one at a time. The range() function does not create a list or store all the numbers in memory at once.

What is the use of the range function in Python?

The range() function enables users to create a sequence of numbers within a specified range. By adjusting the number of arguments provided to the function, users can determine the starting and ending points of the number sequence, as well as the interval between consecutive numbers. The Python range() function can be initialized in three ways:

  • When using `range(stop)``, only one argument is required.
  • For `range(start, stop)``, two arguments are needed.
  • If using range(start, stop, step), three arguments must be provided.

range(stop)

It takes a single argument and starts at 0, increments by 1 and stops at one less than the value of “stop” argument given.

Code

Output

In this example, we called range(5) to generate a sequence of numbers from 0 to 4 (inclusive). Since we didn't provide a start argument, the sequence started at 0 by default. The for loop then iterated over the sequence and printed each number.

range(start, stop)

It takes two arguments: start and stop. The sequence will start from the value given at start, increment by 1 and stop at one less than the value given.

Code

Output

In this example, we called range(5, 10) to generate a sequence of numbers starting at 5 and ending at 9 (inclusive). The for loop then iterated over the sequence and printed each number. Note that when using range(start, stop) without providing the step argument, the sequence will have a step of 1 by default.

range(start, stop, step)

It takes three arguments and lets you decide the start and stop values and the difference between each integer in that sequence. The default value of the step is 1.

Code

Output

In this example, we called range(0, 10, 2) to generate a sequence of even numbers starting at 0 and ending at 8 (inclusive), with a step of 2. The for loop then iterated over the sequence and printed each number. Note that the step argument can be positive or negative, and can be used to generate sequences that count up or down. If the step argument is negative, the start argument should be greater than the stop argument.

Example: Using Positive Step If you need the values to increase in the given range, the step value must be a positive integer.

Output –

Example: Using Negative Step If you need the values to decrease in the given range, the step value must be a negative integer. Note that your start value must be greater than the stop value; otherwise, it returns an empty sequence.

Output –

More Examples

Concatenation of two range() functions

What if you want to join 2 range() results? Yes, we can do it using the chain method of the itertools module. It concatenates the results of both the range() functions.

Output –

Accessing range() with index value

The range() function returns a sequence of integers that can be accessed using indexing.

Output –

range() function with List in Python

The range() function can be used in conjunction with a list to generate a sequence of numbers and then convert it into a list. Here's an example:

In this example, the range() function creates a sequence starting from 2, incrementing by 2 until reaching, but not including, 20. The list() function is then applied to convert the range object into a list.

Output--

This demonstrates how the range() function can be used in combination with a list for generating and manipulating sequences of numbers.

Exceptions

In Python, the range() function raises a TypeError if any of its arguments are of an inappropriate type. Specifically:

  • If any argument is not an integer, a TypeError is raised.
  • If the step argument is zero, a ValueError is raised.
  • If the step argument is negative and the start argument is less than the stop argument, a ValueError is raised.
  • If the step argument is positive and the start argument is greater than the stop argument, a ValueError is raised.

Conclusion

  • Range function in Python generates a sequence of numbers, used in for loops to iterate over values or create number sequences.
  • Range function in Python takes one to three arguments: start, stop, and step.
  • The range function can be called in three ways: range(stop), range(start, stop), and range(start, stop, step).
  • range() in Python generates numbers on-the-fly while iterating, making it memory-efficient for large ranges.
  • TypeError is raised by range() function if the argument types are incorrect, while ValueError is raised if step is negative or zero and start is greater or equal to stop.