Timeline for for loop in Python
Current License: CC BY-SA 2.5
5 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Aug 8, 2020 at 20:04 | comment | added | bukzor |
Just for fun: print("\n".join(["Hello, world!"] * 5))
|
|
| Aug 8, 2020 at 20:02 | comment | added | bukzor |
That's true, but I never said "never use range". My two suggestions start with "if". Iterating "directly" rather than via an integer variable is one of the things everyone has to un-learn when coming to Python. At least 90% of the time I see a junior programmer write for i in range, the correct refactoring is to get rid of range. The accepted answer makes me believe that was true here, as well. It's also interesting to note that the idiomatic way to do "just counting" in python (for i in range(x):) is actually iterating through a list of integers, rather than incrementing a counter.
|
|
| May 19, 2020 at 1:21 | comment | added | Synetech | Nothing in the question (right back to the first revision) says anything about iterating through array or even mentions an array at all. For-loops aren't just for iterating through arrays, they're also used for counting. What if you wanted to simply print "Hello World" five times? How would you do that without a counter? π€¨ | |
| Dec 4, 2010 at 21:48 | vote | accept | newprint | ||
| Nov 13, 2010 at 6:58 | history | answered | bukzor | CC BY-SA 2.5 |