4 ms·
> Incidentally, Fortran, for all its other ugliness, has the best for loop I know: How is that better than Python? for i in range( 1, 100 ): print
by nuclear_eclipse 18y ago
> Incidentally, Fortran, for all its other ugliness, has the best for loop I know:
How is that better than Python?
for i in range( 1, 100 ):
print "Python!"
- davidw 18y agoRuby's nice and compact too: (1..100).each do puts "Ruby" end
- qwph 18y agoMaybe it's just me, but I like doing it this way in Ruby: 100.times { puts "Ruby" } Not that there's anything wrong with your approach, but I just like the way that my code reads almost like English.
- hugh 18y agoEleven characters! Beats Fortran by four. We have a winner! (Counting only the loop-related characters, leaving out whitespace.) Unless anyone has a shorter way of doing something one hundred times?
- davidw 18y agoI think if you want to talk about advantages though, with Fortran you're going to want to talk about the speed, not the brevity. Ruby is quite poky by comparison.
- hugh 18y agoWell, it's shorter by five characters, and you can type it without touching the shift key. Look, I'm only too familiar with the annoyances of Fortran, but let's allow it this one thing, okay?
- abstractbill 18y agoThat's not a good idiom, because it only allows you to loop over small ranges: >>> for i in range(1000000000): ... print i ... python2.4(1261) malloc: *** vm_allocate(size=4000002048) failed (error code=3) python2.4(1261) malloc: *** error: can't allocate region python2.4(1261) malloc: *** set a breakpoint in szone_error to debug Traceback (most recent call last): File "<stdin>", line 1, in ? MemoryError Why attempt to allocate such a large list when you just want to do something several times?
- oofoe 18y agofor i in xrange(1000000000): print i xrange() addresses the problem you describe and has been in Python since, like, forever. However, range() is nicer for learning the language, since it's easy to see how it composes into a list.
- abstractbill 18y agoYes, I know. It's just bad that people are taught one way of iterating, and then may or may not learn the right way at some later time.