Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pythonic way to use range with excluded last number?

Tags:

python

range

If I wanted a list from 0 to 100 in steps of five I could use range(0,105,5), but I could also use range(0,101,5).

Honestly, neither of these makes sense to me because excluding the last number seems non-intuitive.

That aside, what is the "correct" way to create a list from 0 to 100 in steps of five? And if anyone has the time, in what instance would excluding the last number make code easier to read?

like image 886
bfletch Avatar asked Jan 10 '16 19:01

bfletch


People also ask

Why is the range () function inclusive on the left of its range and exclusive on the right?

Python range is inclusive because it starts with the first argument of the range() method, but it does not end with the second argument of the range() method; it ends with the end – 1 index. The reason is zero-based indexing.

Does range include last value?

By default, The range(n) is exclusive, so it doesn't include the last number in the result. It creates the sequence of numbers from start to stop -1 . For example, range(5) will produce [0, 1, 2, 3, 4] .

How do you exclude numbers in a range in Python?

If you want to exclude 0 then change your range to (1,11). The way range works is the lower limit is inclusive where as upper limit is exclusive. On an unrelated note, if your lower limit is 0, you need not include the lower limit and write it as range(11) which is same as range(0,11).

How do you restrict input range in Python?

You'll also want to have a try-except block around the int() call, in case you get a ValueError (if they type a for example). Note if you use Python 2. x, you'll want to use raw_input() instead of input() . The latter will attempt to interpret the input as Python code - that can potentially be very bad.


Video Answer


2 Answers

The two choices you have listed are not similar. One is range(start, stop+step, step) and the other is range(start, stop+1, step). They don't necessary return the same thing. The only case they do is when stop - start is divisible by step.

>>> start, stop, step = 0, 42, 5
>>> range(start, stop+step, step)
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45]
>>> range(start, stop+1, step)
[0, 5, 10, 15, 20, 25, 30, 35, 40]

So, which one should you use? If you want [start, stop] (inclusive), then use stop+1 for the end of the range. Anything more than stop+1 will have side effects like the above. range(0, 42+5, 5) includes 45 as a result, which is not in the [0, 42] range. How do you feel about that?

like image 59
Reti43 Avatar answered Nov 12 '22 12:11

Reti43


The correct way to create a list from 0 to 100 in steps of five is

range(0, 100, 5)

this list has 100 / 5 == 20 elements and does not include the number 100.

Dijkstra explained why counting should start at zero and intervals should be half-open a (very?) long time ago: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

Since Python's range follows Dijkstra and uses half-open intervals, it follows that to create one past the half open interval you must add one:

range(0, 100+1, 5)  # closed interval [0..100] by five

and, yes, I would expect to see it written exactly like that in production code since it is unusual.

like image 39
thebjorn Avatar answered Nov 12 '22 11:11

thebjorn