Could someone explain me something!

Peter Hansen peter at engcorp.com
Sat Dec 28 14:44:24 EST 2002


Ole Jensen wrote:
> 
> when typed this into the python shell:
> print "5+5 =";5+5
> 
> the result becomes:
> 5+5 =
> 10
> 
> so I am thinking that if you use a [,] you get a spacing and if you use a
> [;] you get a line break

Not quite.  You get a line break whenever you end a print statement 
without a comma.  The semicolon is not treated as part of the
print here, so you are basically doing:

print "5+5"
5+5

> but when you write it into a .py file the result differs to just this:
> 5+5
> 
> why is this, are there a logical explanation for it?

The semicolon is a statement separator.  That means the second 5+5 
gets treated just as though you had typed it separately, which means
the output you see comes from the interactive prompt's feature of
displaying the results of expressions for you automatically, not
because of the previous print statement.

In a script, this feature is not present of course, so you don't
see the 5+5.  

You might want to run through the tutorial or, if you already have,
spend some time reviewing the language reference.  You're probably
remembering something from another language and applying it improperly
to Python, using a semicolon in a print statement like that...

-Peter



More information about the Python-list mailing list