The difference between return and print in Python

Share
Copied to clipboard.
Series: Functions
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
4 min. read Watch as video Python 3.8—3.12

In Python, when should you use print and when should you use return? And what's the difference between print and return?

Both print and return output something

Here's a function called nth_fibonacci that calculates the nth Fibonacci number and prints out the result:

from math import sqrt

root5 = sqrt(5)
phi = (1 + root5)/2  # The golden ratio

def nth_fibonacci(n):
    print(round(phi**n / root5))

If we give it the number 20, we'll see the 20th Fibonacci number:

>>> nth_fibonacci(20)
6765

Here's a function that does nearly the same thing, except it uses return instead of print:

from math import sqrt

root5 = sqrt(5)
phi = (1 + root5)/2  # The golden ratio

def nth_fibonacci(n):
    print(round(phi**n / root5))

def nth_fibonacci2(n):
    return round(phi**n / root5)

When we call this second function, it seems like it does exactly the same thing:

>>> nth_fibonacci2(20)
6765
>>> nth_fibonacci(20)
6765

What's going on here?

The difference between print and return

One of these functions prints and the other returns, but they seem like they do the same thing.

Both of our functions are outputting in some way. But one of them outputs to the terminal screen by printing, and the other outputs to the code that called it by returning.

If we assign a variable to the output of these functions, we'll see what's going on a little bit more clearly.

If we point the variable x to the output of the second function, we'll see that x captures its return value:

>>> x = nth_fibonacci2(20)
>>> x
6765

But if we point the variable x to the output of the first function, we'll see that something is printed out, but x doesn't seem to have a value:

>>> x = nth_fibonacci(20)
6765
>>> x
>>> print(x)
None

The value of x is None, which is the default function return value.

What's going on here?

The Python REPL prints automatically

The place where you're able to type code and then immediately see a result is called the interactive Python interpreter, or the Python REPL.

REPL stands for: Read, Evaluate, Print, and Loop, and that's how the REPL works.

The Python REPL:

  1. Reads the statement you've typed
  2. Evaluates the statement when you hit the Enter key
  3. Prints out the result
  4. Loops (it starts again from step 1)

Python first reads what we've typed in as we type code, then it evaluates the code, meaning it runs it. It then prints any value that's returned, and then it loops, meaning it waits for more input.

The third step, the printing, is what we're seeing. The REPL will automatically print any result that isn't None.

So when running code at the REPL, it's sometimes hard to tell whether a value was returned from a function, or whether it was explicitly printed.

Python scripts don't print automatically

Let's define a Python script that uses our second function:

from math import sqrt
import sys

root5 = sqrt(5)
phi = (1 + root5)/2  # The golden ratio

def nth_fibonacci(n):
    """Return the n-th Fibonacci number."""
    return round(phi**n / root5)

n = int(sys.argv[1])
nth_fibonacci(n)

What do you think will happen when we call this function from within a Python script, instead of from the Python REPL?

$ python3 fibonacci.py 20

When we run this script, we see nothing!

$ python3 fibonacci.py 20
$

We don't see anything because we haven't asked Python to print anything.

Let's change the last line in our function to print the return value of nth Fibonacci:

print(nth_fibonacci(n))

Now when we run our program again, we'll see some output:

$ python3 fibonacci.py 20
6765

When running our code from the Python REPL, Python will print automatically. When running our code from within a Python script, Python doesn't print unless we ask it to.

When to print and when to return?

When should you return from a function and when should print?

Most functions return a value. And most functions do not print.

Typically, the functions that do print instead of returning are the surface-level functions in a program. The sole purpose of these functions is to call other functions and print things to the screen.

For example, the main function in this quadratic.py script calls print but the quadratic function doesn't:

import math
import sys


def quadratic(a, b, c):
    x1 = -1 * b / (2 * a)
    x2 = math.sqrt(b ** 2 - 4 * a * c) / (2 * a)
    return (x1 + x2), (x1 - x2)


def main(arguments):
    a, b, c = (float(x) for x in arguments)
    solution1, solution2 = quadratic(a, b, c)
    print(f"x = {solution1} or {solution2}")


if __name__ == "__main__":
    main(sys.argv[1:])
def main(arguments):
    a, b, c = (float(x) for x in arguments)
    solution1, solution2 = quadratic(a, b, c)
    print(f"x = {solution1} or {solution2}")

That quadratic function is meant to return a value back to the line of code that called it. But the main function is meant to display output to the user of this program.

Most Python functions return, but some print

Functions have inputs and outputs: their arguments are their inputs and their return value is their output. Since return is used to pass a value from one bit of code to another, most functions need a return value in order to be useful.

But what about when you're manually testing a function that returns? How can you see the values you're working with?

You could wrap your function in a print call. Or you could test your function from the Python REPL. When running code at REPL, calling a function will print its return value automatically.

Series: Functions

Python, like many programming languages, has functions. A function is a block of code you can call to run that code.

Python's functions have a lot of "wait I didn't know that" features. Functions can define default argument values, functions can be called with keyword arguments, and functions can be written to accept any number of arguments.

To track your progress on this Python Morsels topic trail, sign in or sign up.

0%
Concepts Beyond Intro to Python

Intro to Python courses often skip over some fundamental Python concepts.

Sign up below and I'll share ideas new Pythonistas often overlook.