Learntofish's Blog

A blog about math, physics and computer science

Python: Difference between print and return

Posted by Ed on December 9, 2011

When I began programming I was confused by the difference between print and return. Here, I want shed some light on the difference.

Consider the following code:

def square1(x):
    print(x*x)

def square2(x):
    return x*x

Save the code in a file square.py and execute the file, e.g. in IDLE press F5.
Then, type the following in the Python-Shell:

>>> square1(5)
25
>>> square2(5)
25

In line 1 we have called the function square1() and passed the value 5 to it. As a result the Python-Shell printed 25.
In line 3 we have called the function square2() and passed the value 5 to it. Here, the Python-Shell also printed 25.

What is the difference?
In both cases we seem to get the same result. But while square1() only prints the square, square2() returns it. The difference becomes obvious if we try to save the squared value:

Consider square2():

>>> r2 = square2(5)
>>> r2
25
>>> print(r2)
25
>>> type(r2)
<class 'int'>

– In line 1 we call square2(5) and assign the returned value to r2.
– In line 2 and 5 we both check the value that is stored in r2.
– In line 6 we check the type of the variable r2.
We have been successful with storing the square number in r2.

Consider square1():

>>> r1 = square1(5)
25
>>> r1
>>> print(r1)
None
>>> type(r1)
<class 'NoneType'>

– In line 1 we call square1(5) and try to assign a value to r1.
– In line 2 we note that the value 25 is printed, which is exactly what the function should do.
– In line 3 we try to check what is stored in r1. We note that nothing happens!
– In line 4 we try to check again what is stored in r1. This time the Python-Shell responds with ‘None’.
– In line 6 we check the type of r1 whereupon the Python-Shell tells us r1 is a ‘NonType’.
This shows that we cannot use square1() to save the squared result in a variable.
[Hint: For those who know Java or C++, None corresponds to null]

The confusion with print and return comes from the behavior of the Python-Shell. When we call a function that returns a value, e.g. the square number, the function also prints it.

Advertisements

3 Responses to “Python: Difference between print and return”

  1. Samo said

    Thankss, Now i know the difference. It’s a bit tricky.

  2. Cristian said

    Interesting post! A simple thing that I had ever thought. ;)

  3. Mike said

    Awesome write-up. I am new to Python (learning) and you answered my question perfectly. Thanks for posting this article!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: