Reputation: 2785
While working on python I've seen 2 different ways to use the range function when we want to create the list starting from 0.
list=range(10)
list=range(0,10)
I know that if we change the 0 for another number the output is different, but in this case, the output is exactly the same.
I want to know if it's only a personal decision or is there something more meaningful to use one or the other syntax (memory, debugging, etc).
Upvotes: 4
Views: 326
Reputation: 1
In regular python code, a function definition cannot make the first argument optional (with a later argument not optional). So in this function, where the first argument seems to be optional, it must run through some extra code to switch the arguments around.
This would mean that range(0,10)
is a little bit more efficient than range(10)
, since it doesn't have to run through that extra code to switch the arguments around.
Upvotes: 0
Reputation: 304473
This behaviour of range
is not very Pythonic - ie. changing the meaning of positional arguments depending on the number of arguments.
However everyone is so used to using range(stop)
that it will never be changed.
Personally I find it somewhat jarring to read range(0, stop)
. When I see range
with more than one parameter, I expect something slightly unusual to be happening since the one parameter form is so much more common.
Upvotes: 1
Reputation: 13196
Programming is communication. Not just communicating to the computer what you want it to do, but also communicating to people--including yourself--what you intend. Code should be expressive. To me,
for i in range(10):
says "Do this thing 10 times, the actual index number is not relevant".
for i in range(0,10):
says "Do this thing for each of the numbers 0 through 9, and those numbers will actually represent something", which is a different message. If I were revisiting this piece of code later, I would look more carefully to see what I was doing with that index number, what functions I might me passing it to, and so on. The first case I might recode with a different looping structure without such reservations.
Upvotes: 1
Reputation: 983
The second example is a more explicit call of the range class. It explicitly defines the starting point. Similarly, you can explicitly define the 'step' of each iteration:
range(0, 10, 2)
---> (0, 2, 4, 6, 8)
(see help(range)
in the python interpreter: which shows:
class range(object)
| range([start,] stop[, step])
where [start,] and [,step] are optional. As you have shown, range(x) will begin at 0, and step by 1 by default.
Since python 3, the range function is now the same as xrange() used to be, in that it iterates at each step, as opposed to creating a list.
Upvotes: 1
Reputation: 129572
There is nothing special about doing it the second way. One reason I can think of would be if you want the 0
to be a place-holder for something else - i.e. if you intend to change it to something else later on, and you include the 0
to indicate that. Other than that, it's simply preference. In my personal opinion, unless you have a good reason to include the 0
, I would stick with range(10)
.
Just as a note, you should never use list
as a variable name since it's a built-in function.
Upvotes: 5