print
(or print()
if you're using Python 3) does exactly that—print anything that follows the keyword. It will also do nice things like automatically join multiple values with a space:
print 1, '2', 'three'
# 1 2 three
Otherwise print
(print()
) will do nothing from your program's point of view. It will not affect the control flow in any way and execution will resume with the very next instruction in your code block:
def foo():
print 'hello'
print 'again'
print 'and again'
On the other hand return
(not return()
) is designed to immediately break the control flow and exit the current function and return a specified value to the caller that called your function. It will always do this and just this. return
itself will not cause anything to get printed to the screen. Even if you don't specify a return value an implicit None
will get returned. If you skip a return
altogether, an implicit return None
will still happen at the end of your function:
def foo(y):
print 'hello'
return y + 1
print 'this place in code will never get reached :('
print foo(5)
# hello
# 6
def bar():
return # implicit return None
print bar() is None
# True
def baz(y):
x = y * 2
# implicit return None
z = baz()
print z is None
# True
The reason you see return
ed values printed to the screen is because you are probably working in the interactive Python shell that automatically print
s any result for your own convenience.