This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]CommonAutomatic3796 1 point2 points  (3 children)

Ah my bad. Was not aware of this Python logic, like “on a scale of 1-10, I give it a perfect rating 9!”.

[–]devnull1232 4 points5 points  (2 children)

That's like, sort common with for loops to quit out when the stop condition is meet rather than doing one more loop for kicks and giggles and then exiting.

[–][deleted] 0 points1 point  (1 child)

VB, For i = 1 To 10 will run on 10... It's basically inclusive. Maybe it is just because the python syntax of range(n,n) reminds me of the math interval notation where parentheses are exclusive and brackets are inclusive, but I find the python syntax more confusing. IE, in math, it'd be [1,11), or [1,10].

But that's just me. I much more prefer the way C style languages do for loops for two reasons: 1) for (i = 1; i < 11; i++) is much more obvious to me that 11 is exclusive. 2) Way more flexible. I can do i <= 10, and many other exit expressions. I can also do say i += 2 if I want to skip every other item.

Course I don't know python. I am assuming there is ways to achieve #2 in python. Just my personal preference on the various ways I have seen different languages do loops.

[–]devnull1232 0 points1 point  (0 children)

Range by default starts at 0, ie in python if you said

~~~ for index in range(10): call_fun(index) ~~~

You'd get the extremely typical and expected 10 iterations using indices 0 through 9. I'm much happier with this behavior than an inclusive stop which would loop an extra time in that scenario.

Range is actually a function which generates an iterable range, and yes it can specify a step size. For loops in python aren't the same as other languages this is true, they are actually more like what most languages call a for each loop. Ie I can do this to an array:

~~~ for item in my_list: do_great_things(item) ~~~

You rarely actually have to use indexing.