I see why you are facing this issue. Its because you are using the larger value as the first argument and smaller value at the second argument in the range (This is happening due to the negative sign).

For such cases following code will work :

a = 5
b = -5
step = 1   
if b < 0:
    step = -1
range (a, b + step, step)
Answer from Vishvajit Pathak on Stack Overflow
Discussions

why can't for print something when the range is negative
You can run range several ways depending on how many parameter you give it range(start, stop, step): range(start, stop): range(stop): โ€”โ€”โ€”โ€”โ€”โ€”- for i in range(3): print(i) When you pass just a single parameter to range, it treats that parameter as the stop. So it will stop at 3. Since no start parameter was give it will start at 0 by default. Since no step parameter was given, it will step by +1 as a default. The range above will print 0, 1, 2. for i in range(-5): print(i) This range stops at -5. Since no start was supplied the start will default to 0. Since no step was provided it will step by +1 by default. So where starting at 0, we need to end at -5, and we are counting up by 1. Itโ€™s impossible for the range to reach -5 from that starting point so range will give no numbers. for i in range(0, -5, -1): print(i) Prints the numbers 0, -1, -2, -3, -4. Can you figure out why? https://realpython.com/python-range/#lets-loop More on reddit.com
๐ŸŒ r/learnpython
7
4
February 27, 2021
What's the logic behind stop value of range function with negative step value in python - Stack Overflow
1 Seeking clarification on the in range function in Python where the stop value is smaller than the start value ยท 0 Why can't we get the range of a particular negative number ยท 1 Range() including its bounds for positive and negative steps More on stackoverflow.com
๐ŸŒ stackoverflow.com
What does step do in randrange?
It's probably easiest to just show an example. random.randrange(0,10) gives one randomly selected number from the following numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 random.randrange(0,10,2) gives one randomly selected number from the following numbers: 0, 2, 4, 6, 8 More on reddit.com
๐ŸŒ r/learnpython
11
6
April 16, 2024
Question about Pythons step option in Range function?
I'm not totally sure what your confusion is. But it's pretty simple: the three parameters are start, stop, and step. start is where the range starts, stop is where the range ends (exclusively, so it won't be included in the result), and step is the size of each step. So, when you say range(2*2, 10, 2), you're saying "generate a range from 4 to 10, by taking steps of 2". Thus, the output is [4, 6, 8]. It won't include 10, because that's the stop parameter, and the range ends at that point. So what's actually going on is, internally, it says "let me track what the start is. start is 4. Add it to the output, then add step to that value. Is that new value >= than the stop value? If so, then end and return the results, otherwise add the new value to the output and repeat the previous step." More on reddit.com
๐ŸŒ r/learnprogramming
14
3
July 14, 2022
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-use-negative-step-in-range-418732
Python - How to use negative step in range()
While most developers are familiar with positive steps, negative steps offer a unique way to iterate through sequences in reverse order. The range() function can take three arguments: range(start, stop, step).
๐ŸŒ
Real Python
realpython.com โ€บ python-range
Python range(): Represent Numerical Ranges โ€“ Real Python
November 24, 2024 - You can also reverse the sequence with reversed(). If you need to count backwards, then you can use a negative step, like range(start, stop, -1), which counts down from start to stop.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-range-function
Python range() function - GeeksforGeeks
Example 2: This example generates even numbers by skipping values using a custom step size. ... Example 3: This example demonstrates how range() can be used to generate numbers in reverse order by providing a negative step value.
Published ย  March 10, 2026
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-range-with-negative-step
Python Range with Negative Step Value
For this specific scenario we are discussing, where step is a negative value, start must be greater than or equal to stop. In this example, we shall iterate over a range that starts at 9 and goes until 4 in steps of -2. For example, in the following program, we define a range from 9 to 4 with ...
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-range.html
Python range() Function
>>> list(range(0, 10, 2)) [0, 2, 4, 6, 8] >>> list(range(0, 10, 6)) [0, 6] >>> list(range(200, 800, 100)) [200, 300, 400, 500, 600, 700] If the step is negative, the range decreases from start down to stop.
Find elsewhere
๐ŸŒ
Enki
enki.com โ€บ post โ€บ how-to-use-python-range-function
Enki | Blog - How to use Python Range Function
Using the range() with a step helps you manage how iterations happen. Starting at 1, this loop increments by 2, demonstrating how stepping works. You can count backward by setting a negative step.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-range-function-explained-with-code-examples
Python range() Function โ€“ Explained with Code Examples
October 6, 2021 - There's no default value for negative step โ€“ you must set negative_step = -1 to count down covering each number. โ–ถ In this example, you'd like to count down from 20 in steps of -2. So the sequence is 20, 18, 16, all the way down to 2. If ...
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ python โ€บ python-range โ€บ python-range-reverse-negative-step
Python range() - Reverse or Negative Steps
January 17, 2021 - If we are taking steps in the backward direction, start argument should be greater than stop to generate any elements in the range object. And step should be a negative number. In the following example, we shall generate a Python Range with ...
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ python range() explained with examples
Python range() Function Explained with Examples
March 17, 2022 - When we set the negative value to step, In each iteration, the number will go down until it reaches to stop number. # Decrement range() using step # start = 30, stop = 20 # step = -2 for i in range(30, 20, -2): print(i, end=' ') # Output 30 ...
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-range
range() Built-in Function -
Usually, when step is negative, start is greater than the stop, and r[i] should be greater than stop. For example, if start=8, stop=2 and step=-2, then the contents of range are calculated as given below.
๐ŸŒ
Learn By Example
learnbyexample.org โ€บ python-range-function
Python range() Function - Learn By Example
April 20, 2020 - You can generate a range of negative numbers as well. for x in range(-5,0): print(x) # Prints -5 -4 -3 -2 -1 ยท The range increments by 1 by default. However, you can specify a different increment by adding a step parameter.
๐ŸŒ
YouTube
youtube.com โ€บ watch
How Do You Use A Negative Step In Python Range? - Next LVL Programming - YouTube
How Do You Use A Negative Step In Python Range? In this informative video, weโ€™ll cover how to use a negative step in the Python range function to create coun...
Published ย  August 22, 2025
๐ŸŒ
Real Python
realpython.com โ€บ lessons โ€บ range-decrementing
Decrementing With range() (Video) โ€“ Real Python
00:00 We can supply a negative step value to generate a list of numbers that is decreasing. If we do this, then we have to make sure our stopping value is less than our starting value, or else weโ€™ll get a list with no elements.
Published ย  October 15, 2019
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-program-to-print-all-negative-numbers-in-a-range
Print all Negative Numbers in a Range - Python - GeeksforGeeks
February 1, 2025 - The most basic method for printing negative numbers is to use a for loop to iterate through the list and check each element.Pythona = ... In this article, we are going to learn how to print numbers within a given interval in Python using simple ...
๐ŸŒ
Server Academy
serveracademy.com โ€บ blog โ€บ python-range-tutorial-for-beginners
Python Range() Tutorial for Beginners - Server Academy
November 11, 2024 - ... Here, range(5) generates numbers ... The stop value is not included in the sequence. Negative Step: Use a negative step to create a descending sequence....
๐ŸŒ
BitDegree
bitdegree.org โ€บ learn โ€บ best-code-editor โ€บ python-range-example-7
How to Use Python Range to Iterate Backwards
Python range step can be a negative integer to generate a sequence that counts down. Check this code to learn how to reverse iteration in a Python range.