What's the difference between print and return in Python?

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

elenni
Posts: 1
Joined: Sun Jul 15, 2012 9:18 pm UTC

What's the difference between print and return in Python?

Postby elenni » Sun Jul 15, 2012 9:26 pm UTC

I am finding this difficult to understand. I am following Google's tutorial with videos. The tutorial contains lots of 'print' and the exercises use 'return.' If I experiment and try to exchange 'print' for 'return' things stop working.
For example:
Tutorial--

Code: Select all

def Hello(name):
  if name == 'Alice' or name == 'Nick':
    name = name[:2] + '-tron'
    print 'Alert: Cake Mode'
  else:
    print 'Hey, you are not Alice!'
  name = name + '!!!'
  print 'Hello', name
  print 'I have lots of cake for %s' % (name)


Exercise--

Code: Select all

def verbing(s):
  if len(s) > 2 and s[-3:] == 'ing':
    return s + 'ly'
  elif len(s) > 2:
return s + 'ing'
  else:
    return s


I'm sort of a newbie with computer programming, having only had reasonable experience with MATLAB and Mathematica. Your explanation in the simplest 'dumbed-down' form is appreciated.

User avatar
phlip
Restorer of Worlds
Posts: 7556
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: What's the difference between print and return in Python

Postby phlip » Tue Jul 17, 2012 11:07 pm UTC

The "print" command outputs text directly to the screen, while the "return" command outputs the values back to whatever is calling the function.

For example:

Code: Select all

def example():
    print 1+1

example() # this will output the number 2 to the screen

Code: Select all

def example():
    return 1+1

x = example() # this will store the number 2 in the variable called x


I'm guessing the reason you get them confused is because of the way the Python command line works - if you had either of those functions set up and you just typed "example()" into the interpreter, it would display "2"... this is because if you type in an expression that returns some result, then the interpreter automatically displays that. So in the first case, typing "example()" makes it display "2" because that's what the example() function does, and in the second case, typing "example()" makes it display "2" because that's what the function evaluates to, and then the interpreter displays that result.

In short: you use "print" in functions that are interacting directly with the user, to display information, and you use "return" in functions that need to pass its values back to other code.

As for your specific case... note that when it hits a "return" statement, it stops processing the function immediately, and sends back that value:

Code: Select all

def example():
  print "This is the start of the function"
  return 5
  print "This is after the return"

x = example() # this will display "This is the start of the function", and then store 5 in the variable x, but will *not* display "This is after the return"
So in your first code snippet, if you changed all the "print"s to "return"s, then it would only go as far as the first one (ie either "Alert: Cake Mode" or "Hey, you are not Alice") and not go any further. A function can only return one value... if you need a function to return many values, you need to bundle them up into a something like tuple, or a list, so all the values you need are packaged into a single object that you can return. But in this case, your Hello function is trying to interact directly with the user, so "print" is appropriate. While your verbing function is processing on a string that you will presumably want to use as a part of other code, so "return"ing that value makes more sense.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

bizworldusa
Posts: 1
Joined: Fri Jul 27, 2012 6:51 am UTC

Re: What's the difference between print and return in Python

Postby bizworldusa » Fri Jul 27, 2012 7:16 am UTC

Code: Select all

>>> def ret():
    return 42

>>> def pri():
    print(42)


>>> answer = pri()
42
>>> print(answer)         # pri implicitly return None since it doesn't have return statement
None
>>> answer = ret()
>>> answer
42
Last edited by phlip on Fri Jul 27, 2012 11:55 am UTC, edited 1 time in total.
Reason: Added [code] tags

hoppypress
Posts: 9
Joined: Thu Aug 02, 2012 6:26 pm UTC

Re: What's the difference between print and return in Python

Postby hoppypress » Fri Aug 24, 2012 9:25 pm UTC

There's another fantastic resource for learning Python, if you're interested: search for "Learn Python The Hard Way" by Zed Shaw. It's very popular and comes highly recommended. It assumes no programming experience and the online version is free (dead tree versions are available).

Good luck!
Didn't you say you liked craft beer and homewbrewing? Or needed a gift for someone who does? Get some fine beer art!
(Just between us, there is a coupon code: "XKCDBEER")

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: What's the difference between print and return in Python

Postby You, sir, name? » Sun Oct 07, 2012 12:36 pm UTC

The [code]-tag is your friend.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

danieloc93
Posts: 2
Joined: Fri Nov 16, 2012 11:21 pm UTC

Re: What's the difference between print and return in Python

Postby danieloc93 » Sat Nov 17, 2012 3:18 am UTC

Print does what it says: Prints what you want.

Return does the same thing its name implies... returns something from a function to another... If you want to get the value of c from the main method, you gotta call a method that calculates that c and returns its value to the "__init__"


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 5 guests