[Courses] [python] Lesson 2: Loops, if, and beer

Akkana Peck akkana at shallowsky.com
Tue Jun 28 16:47:38 UTC 2011

Good job on lesson 2, everyone! I think I'm making the homework
assignments a bit too easy. :-)

I liked how people who were more comfortable with programming took it a
step farther, doing things like distinguishing "1 bottle" from "2 bottles".
Everybody who did that gets extra credit!

Some people used extra Python features that I haven't covered yet, so
if you saw stuff in other people's solutions that you don't recognize
or didn't understand, don't worry: I'll be getting to all of that in
lessons 3 and 4.

======== Sending spaces in email =========

I noticed a lot of the posted solutions didn't have any indenting at all.
In particular, I noticed that from several gmail and yahoo accounts.

I can't figure out what's causing it, though. I have a gmail account,
so I tried sending myself some Python code from gmail's web interface,
some using spaces for indentation and some with tabs, and both types
of indentation came through just fine. So it's not gmail's web
interface that's making the indentation disappear. Anybody have any
idea what's going wrong? 

Python code is really hard to read if there's no indentation, and it
won't work at all that way, so since this seems to be a common
problem, I hope we can figure out what's causing it. I'd be curious
to hear from the folks whose posts came through with no spaces: did
you use a webmail interface, or a mail program? Did you copy from
a text editor or other program then paste into the mail window?
Did the code use spaces or tabs?

======== Answers to questions ===============

GcX asked about range(0, 5) versus range(1, 6).  For the purpose of
printing something five times, they're the same, but most of the time
you'll care about whether the variable starts at 0, 1, 42 or whatever,
so it'll be clear where to start your range(). I started with 1 in the
example because the lesson was getting long and I didn't want to make it
longer by explaining that programmers usually start counting things at
zero rather than 1.

Peggy asked three questions. First, about spaces, in particular
"range(1, 6) :" vs. "range(1, 6):".  They're both fine. Although
Python cares a lot about indentation (spaces at the beginnings of
lines), it doesn't care much about spaces after that. With colons,
parentheses, commas and brackets you can add spaces or not, whatever
you find easiest to read.  "range ( 1 , 6 ):" and "range(1,6):"
are equally valid.

Second, is the .py extension necessary? No, and I'll talk more about
that (and shebangs) in lesson 3.

Third, pydoc. I'm not planning to cover pydoc in this course, though
I'll probably mention it along with docstrings when I get to functions.

I'll answer Tino's question about else in a separate message.


More information about the Courses mailing list