user9730627
user9730627

Reputation:

Range() including its bounds for positive and negative steps

I want to do a simple program that prints all the numbers in a range A to B, including B.

For ranges having bounds in increasing order, I know that I have to add 1 to the upper bound, like:

range(A, B+1)

But adding 1 to B won't work when the bounds are in decreasing order, like range(17, 15, -1).

How can I code it to work for both increasing and decreasing ranges?

Upvotes: 1

Views: 1887

Answers (5)

Thierry Lathuille
Thierry Lathuille

Reputation: 24232

You could create a function that turns a normal range into one that includes both bounds, like:

def inclusive(r):
    return range(r.start, r.stop + r.step, r.step)

You should pass it a range, and it will return a range:

increasing_range = range(2, 5)
print(list(inclusive(increasing_range)))
# [2, 3, 4, 5]

decreasing_range = range(5, -5, -1)
print(list(inclusive(decreasing_range)))
# [5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5]

even = range(2, 10, 2)
print(list(inclusive(even)))
# [2, 4, 6, 8, 10]

for odd in inclusive(range(1, 5)):
    print(odd)
# 1 2 3 4 5 

Upvotes: 0

BoarGules
BoarGules

Reputation: 16952

I think I don't understand the question properly. There are 3 cases:

  1. A, B both positive
  2. A negative, B positive
  3. A, B both negative

Now if I do this (in Python 2, to avoid having to do list(range(...)): this makes the explanation cleaner):

>>> A = 10; B = 20     # case 1
>>> range(A,B+1)
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
>>> A = -10; B = 2     # case 2
>>> range(A,B+1)
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2]
>>> A = -10; B = -2    # case 3
>>> range(A,B+1)
[-10, -9, -8, -7, -6, -5, -4, -3, -2]

So your remark the last number in the range won't be included doesn't seem to fit with what I can see.

If you are receiving input data where A > B, then the problem is not the negative number, but the fact that range() expects the values to be in ascending order.

To cover that:

>>> A = 2; B = -2     # case 4
>>> A,B = sorted((A,B))
>>> range(A,B+1)
[-2, -1, 0, 1, 2]

This also works for cases 1, 2, and 3.

If I have misunderstood the question please edit it to clarify.

Upvotes: 1

Vishvajit Pathak
Vishvajit Pathak

Reputation: 3711

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)

Upvotes: 1

Tejas Sarade
Tejas Sarade

Reputation: 1212

Last number is never included in python range. You need to adjust the code accordingly.
e.g. To print values form -5 to -1(included) use,

>>> print(list(range(-5, 0)))
[-5, -4, -3, -2, -1]

In reverse order

>>> print(list(range(-1, -6, -1)))
[-1, -2, -3, -4, -5]

Upvotes: -1

Adrish
Adrish

Reputation: 52

Please check if this works. Thank you.

if A>B and A < 0 and B < 0:
    print(list(range(A,B,-1)))
elif A<B:
    print(list(range(A,B)))
else:
    print(list(range(A,B,-1)))

Upvotes: 0

Related Questions