[Courses] [python] Lesson 1: Hello world
akkana at shallowsky.com
Sat Jun 18 20:18:52 UTC 2011
About that homework question, print "Hello,", name
Everybody gets a point for the first comma -- it's part of the string
you're printing. Good job! I mean it -- a lot of people don't get that
Pretty much everybody knew that the second comma, being outside the
string, was telling Python to do something. But what? A lot of you
pretty much figured it out -- that it's to separate the objects
you're passing to the print statement -- but here are the details.
Python's print statement is a bit magical, with a special syntax of
its own that's different from most of the language.
(Python 3 changes that.)
You can give print a long list of things separated by commas, like:
print a, b, c, d, e
and it will print each of them with spaces
between them. So in my Hello example, the comma outside the quotes
just means that you're printing two things -- the string "Hello,"
and the name -- with a space in between them.
You need the comma. You can't give print a list of things without
commas, i.e. you can't say
print a b c d e # Wrong: Python will give you a syntax error
Sharon asked if there's any way to print multiple things without a
space between them. There is, but it's a little bit harder. Instead of
print "Hello,", name
you could say
print "Hello, " + name
A plus + will stick two strings together without any space between
them. (Notice that I added the space inside the string.) In fact, Kay
print "Hello, " + name + "!"
which adds an exclamation mark at the end, with no space before it.
You can mix commas and + as much as you want, but using + gets a little
more complicated if your objects aren't all strings. For instance, you
print "Today is lesson number", 1
but you can't say
print "Today is lesson number " + 1
or Python will complain: cannot concatenate 'str' and 'int' objects
In other words, + means something different for a string than for an
integer number, and Python doesn't know what to do.
So most of the time, using commas is easiest if you don't mind a space.
Larry asked if the second comma was to prevent printing a newline.
Not in this case, but commas can do that if you put them at the *end*
of the print statement. Like this:
print "Hello,", name,
Peggy asked about double quotes vs. single quotes. In Python, you can
use either type -- there's no difference. So use whichever type suits
your own aesthetics. Some programmers prefer single quotes because
they're easier to type (you don't need the shift key); some prefer
double quotes because then you don't need to worry about apostrophes.
For instance, "Python doesn't care which type you use" will work fine,
whereas if you said 'Python doesn't care which type you use', you'd
end up with a syntax error because the string would be 'Python doesn'
and Python wouldn't know what to do with the rest of it.
Of course, you have to end the string with the same type you used to
start it. 'string" won't work.
And on why the language is called Python: lots of people knew the
answer, that it's named after Monty Python and not the snake. Though
I love the "So O'Reilly could have a cool book cover" answer. :-)
Darn, I think my followup was longer than the lesson! I hope I
didn't miss anybody -- if you asked a question and I didn't answer
it, or I wasn't clear enough, let me know.
More information about the Courses