User Controls
For loop or while loop for small number of iterations?
-
2017-01-11 at 7:50 PM UTCConsider this code:
pages = 5
i = 0
while i < pages:
do_stuff()
i += 1
pages = 5
for i in pages:
do_stuff()
Which would be better?
-
2017-01-11 at 11:10 PM UTCWhich one do you prefer? Simply pragmatics
If you want to discuss performance, how is your assembly? -
2017-01-12 at 12:48 AM UTCI prefer the form of for(x;x<y;x++)
generally for if you're able to know the number of iterations before starting, while if you aren't -
2017-01-12 at 1:14 AM UTC
Originally posted by iamlight Which one do you prefer? Simply pragmatics
If you want to discuss performance, how is your assembly?
Not that good. As for the subject of loops, i prefer a `for` loop, it looks and feels more elegant. -
2017-01-12 at 4:15 AM UTCSecond piece of code won't run. "for ... in" iterates over a collection type (list, dictionary, stream, etc) while `i` is an int. You can do `for i in range(5):` but not `for i in 5:`. Both `for index in range(N)` and `for item in collection` are prefered to a while loop if you want to iterate over an item or do something finite N times. This is more convention/readability than anything.
If you want to do something N times for large values of N the while construct is more space efficient since `range(n)` takes roughly linear space while the index approach takes constant space. There is, however, `xrange()` which returns a generator and requires constant space to operate. -
2017-01-12 at 4:19 AM UTC
Originally posted by Lanny Second piece of code won't run. "for … in" iterates over a collection type (list, dictionary, stream, etc) while `i` is an int. You can do `for i in range(5):` but not `for i in 5:`. Both `for index in range(N)` and `for item in collection` are prefered to a while loop if you want to iterate over an item or do something finite N times. This is more convention/readability than anything.
If you want to do something N times for large values of N the while construct is more space efficient since `range(n)` takes roughly linear space while the index approach takes constant space. There is, however, `xrange()` which returns a generator and requires constant space to operate.
Ya ya, sorry i forgot to put `range` there. I was about to say REEEEE LIAR
>>> page = 5
>>> for i in range(page):
... print i
...
0
1
2
3
4
Then i saw i did range in the interpreter and not in my code example, lel. -
2017-01-13 at 2:20 AM UTC
Originally posted by aldra I prefer the form of for(x;x<y;x++)
Same pisses me off that python doesn't have this. I usually only use while if it's an infinite while(true) {...} -
2017-01-13 at 2:48 AM UTC^ `for x in range(y)` is the exact same behavior, the only difference is syntax
-
2017-01-13 at 3:43 AM UTC
Originally posted by Lanny ^ `for x in range(y)` is the exact same behavior, the only difference is syntax
Yeah I know it just doesn't feel right and I can never remember the syntax, if it will start at 0 or 1, will it use the final number or just stop there, etc. I'm sure once you know it that it is cleaner and more readable. -
2017-01-13 at 3:46 AM UTC
Originally posted by Merlin Yeah I know it just doesn't feel right and I can never remember the syntax, if it will start at 0 or 1, will it use the final number or just stop there, etc. I'm sure once you know it that it is cleaner and more readable.
"It is not the Python's fault that it occasionally bites"
- Cyber Sun Tzu