Irongrave Irongrave - 23 days ago 10
Python Question

Python range - No iteration when start == end?

As the title says.

Tested using:

for x in range (0,0):
print(str(x))


I am expecting to see "0", but nothing prints.

I'm using Python 3 and was hoping:

for x in range(x,y)


Would at least get me one iteration when x == y, but it seems that isn't the case... is there a better way I could be doing this rather than manually handling the x == y scenario?

Answer Source

range is half-inclusive

range(x, y) includes x and excludes y. Use range(x, y + 1) instead, so for your example you'd like to use range(0, 1) in your first codeblock.