[Tutor] formatting strings

Steven D'Aprano steve at pearwood.info
Sat May 9 05:12:28 CEST 2015


On Fri, May 08, 2015 at 05:11:49PM -0700, Danny Yoo wrote:

> Also, you can write a loop that goes from 1 to N by using range().  For example:
> 
> ########################
> for n in range(1, N+1):
>     print(n, 2*n)
> ########################
> 
> The while loop that you have does work, but the for loop here is more
> idiomatic in expressing the idea of "Do the following for this
> collection of values ..."

Why do so many beginners turn to while loops when they want to iterate 
over a fixed sequence?

I know that Learn Python The Hard Way teaches while loops first. I think 
that is a terrible idea. That's like going to a cookery class where 
for the first three weeks they teach you to chop vegetables with a 
spoon, and only in the fourth week say "Guess what? There's an easier 
way! Introducing the knife!!!!"


While-loops should be taught after for-loops. The fact that from a 
computer-science theoretical perspective while-loops are more 
fundamental is irrelevant. We don't teach people bitwise operators 
before teaching them arithmetic. Loops are no different.



-- 
Steve


More information about the Tutor mailing list