Python Negative Range

Negative Range in Python

In Python, you can generate a sequence of negative integers using range() function. We can call this range as Negative range.

Since we are working with negative numbers in the range, make sure the start value of the range is less than the stop value of the range. Else the range would be empty.

For example, if my_range is the given range object with a start value of -9 and a stop value of -4, then the syntax to define this negative range would be as shown below.

my_range = range(-9, -4)

And the sequence of numbers in the range would be as shown in the following.

-9 -8 -7 -6 -5

Example

In the following program, we define a range with a start=-9, stop=-4, and default step value.

Python Program

my_range = range(-2, -4)

for i in my_range:
    print(i)
Run Code Copy

Output

Python Negative Range

Summary

In this tutorial of Python Ranges, we learned how to iterate over a range using While loop statement, with the syntax and examples.

Related Tutorials

Code copied to clipboard successfully 👍