Correct range function coding

While working on python, I saw two different ways to use the range function when we want to create a list starting at 0.

list=range(10) list=range(0,10) 

I know that if we change 0 for another number, the result will be different, but in this case the output will be exactly the same.

I want to know if this is only a personal solution or if there is something more meaningful for using this or that syntax (memory, debugging, etc.).

+4
source share
5 answers

There is nothing special in doing this in a second way. One of the reasons I can think of is if you want 0 be the place for something else, i.e. If you intend to change it to something even later, and you specify 0 to indicate this. Other than that, it's just a preference. In my personal opinion, if you have no good reason to include 0 , I would stick with range(10) .

As a note, you should never use list as the name of a variable, as it is a built-in function.

+5
source

The second example is a more explicit range class call. It clearly defines the starting point. In the same way, 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: shows: class range(object) | range([start,] stop[, step]) where [start,] and [, step] are optional. As you have shown, range (x) starts at 0 and defaults to default.

Starting with python 3, the range function is now the same as xrange (), since it iterates at every step, as opposed to creating a list.

+1
source

Programming is communication. Not just communicating with the computer what you want, but communicating with people - including yourself - is what you intend. The code must be expressive. For me

 for i in range(10): 

says: "Do this thing 10 times, the actual index number does not matter."

 for i in range(0,10): 

says: “Do this thing for each of the numbers 0 to 9, and these numbers actually represent something,” which is another message. If I reviewed this piece of code again, I would look more closely to see what I am doing with this index number, what functions I can pass to it, and so on. In the first case, I could recode with a different loop structure without such reservations.

+1
source

This range behavior is not very Pythonic - i.e. changing the value 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 annoying to read range(0, stop) . When I see range with several parameters, I expect something a bit unusual because the shape of a single parameter is much more common.

+1
source

In regular python code, a function definition cannot make the first argument optional (with a later argument is not optional). Thus, in this function, where the first argument seems optional, it must run some additional code to switch the arguments.

This would mean that range(0,10) slightly more efficient than range(10) , since it does not need to run this extra code to switch arguments.

0
source

All Articles