[Tutor] print in py3

Steven D'Aprano steve at pearwood.info
Sun Dec 22 11:14:42 CET 2013


On Sun, Dec 22, 2013 at 12:43:46AM -0500, Keith Winston wrote:
> I've been playing with afterhoursprogramming python tutorial, and I was
> going to write a question about
> 
> myExample = {'someItem': 2, 'otherItem': 20}
> for a in myExample:
>     print (a, myExample[a])
>     print (a)
> 
> returning

Not returning, printing. Remember, they are two different things. 
Return can only occur inside a function, and once you return, the 
function is exited immediately.

> ('someItem', 2)
> someItem
> ('otherItem', 20)
> otherItem
> 
> Which is to say, why would the output formatting be completely different
> (parens, quotes) between the two print statements, 

You imply, but don't actually say, that the above is in some version of 
Python other than Python 3.3. I expect that you are using Python 2.7.

That's the answer to your question: in Python 2, print is a statement, 
not a function. That has many consequences, but the relevant one is that 
statements don't require brackets (parentheses for Americans reading) 
around the arguments. For example, in Python 2:

    print fee, fi, fo, fum

prints the four variables fee, fi, fo and fum with a space between them. 
In Python 3, the above gives a syntax error, and you have to use 
brackets like any other function:

    print(fee, fi, fo, fum)

You can also leave a space between the function and the opening bracket, 
like "print (...", without changing the meaning.

So, in Python 3.3, the two lines:

    print (a, myExample[a])
    print (a)

prints the variable "a" and the value "myExample[a]" with a space in 
between, then on the next line prints the variable "a" alone.

But in Python 2, the parentheses aren't part of the function call, 
because print isn't a function. So what do the brackets do? They are 
used for *grouping* terms together.

In the first line, the brackets group variable a, comma, myExample[a] 
together. What does the comma do? It creates a tuple. A tuple is a group 
of multiple values lumped together. So the first line doesn't print two 
things, a followed by myExample[a]. Instead it prints a single thing, a 
tuple of two items. How do tuples print? They print with round brackets 
around them, and commas between items.

The second line is less complicated: the brackets group a single value, 
a, which is just a. Since there is no comma, you don't get a tuple.

(It is commas, not parentheses, which create tuples.)


> but then when I run it
> in  Python 3.3, it's the more reasonable
> 
> otherItem 20
> otherItem
> someItem 2
> someItem
> 
> Which I'm much happier with. I assume this just reflects changes in the
> print command default formatting: is there some reasonably efficient way to
> see where/when/how this changed? I guess it would be buried in a PEP
> somewhere?

I don't think it was in a PEP... no, I was wrong, there is a PEP for it:

http://www.python.org/dev/peps/pep-3105/


-- 
Steven


More information about the Tutor mailing list