Return Keyword in Python - comparison

Hi. I have a question about the result of the “return” keyword in these two cases:
Why does it not display a result (123) in the first code, but shows the result (8) in the second code?
Do those print lines stop the return line from displaying results? (in first code)

First Code:

def boring_function():
    print("'Boredom Mode' ON.")
    return 123

print("This lesson is interesting!")
print("This lesson is boring...")

Second Code:

def cube(num):
    return num*num*num


Thank you!

Shows where? If you’re using Jupyter notebook, then the result of only the last expression is shown by default. If it’s None, then nothing is shown.

The first block would show nothing, because the result of the last print function is None. The second block would show whatever the function returns.


I tried removing the print lines in the first code, and 123 appears.


To share code here use the </> button in thre :gear: menu.
Please not share screen shots of code, copy-n-paste the text.

Like this:


When you are in the python REPL prompt each thing you type that has a value has that value printed. But when you run a python script that does not happen.

If you want to print the value return by a function you need to use the print function to do that.

def my_func():
    return 123


hi barry… I was asked where I got the results, or where these were shown. Hence, I posted a screenshot. I would normally post the code in the prescribed format.

hmm…as to the printed return result, as in my screenshots, I got a result printed even without the “print” line… (2nd code). why…

Why not? In this case, it’s the context that matters, i.e., where they run that code, and screenshots can show that context and let us see exactly what they see. And they had posted the code as text already. Which also makes it strange for you to explain to them how to do it after they had already successfully done it…


Why copy-paste code? Because that way helpers, and other learners, can easily reproduce the situation.

Why copy-paste the results (or exception-reporting)? So, that the full story becomes visible, rather than relying upon a person’s interpretation - particularly an interpretation in a situation (s)he doesn’t fully-understand.

Also, some of us are ‘lazy’ and copy-paste is quicker than messing with taking, shaping, editing, whatever, screen-dumps.

As we see in the conversation, there’s plenty of room for misunderstanding: is the OP using Jupyter? Is the OP using an editor and executing from the command line? Is the OP doing both inside an IDE? Is the OP not using an editor but working entirely in the Python-REPL?

BTW, the latter might explain the last question, but the outlines on the screen-dumps do lead one to suspect Jupyter.

I wrote a thorough explanation of this at Stack Overflow already:

1 Like

Also, when you run a Jupyter-notebook cell, it actually has two different outputs:

  • “regular” outputs, which would appear if you would run commands non-interactively, like when loading or just running the script in a regular way. (This includes all print-functions.)
  • and the value of the last statement/expression, like in the interactive mode. This one is also saved as the result of the cell execution inside the notebook.
How to access the cell value later

The value produced by each cell execution is saved inside the notebook. After each cell execution, the cell displays a new number on the left, like [1], [2], [3] etc. The corresponding values are saved in the variables named _1, _2, _3 etc.

Try running the following:

def boring_function():
    print("'Boredom Mode' ON.")
    return 123

print("This lesson is interesting!")

You should see that the print-outputs are separated from 123.
123 is the actual cell execution value, because it’s the value of the last statement/expression, and you can think of the output produced by the print statements as some debug-values.

1 Like

They may show the context, but in these examples there is no clue to the app that the screenshot is from. I guessed a REPL.

I wouldn’t say “no” clue. One might recognize the UI. But yes, better screenshots would’ve been good.

1 Like

Oh sorry for confusion. Honestly I do not have any idea what REPL and other terms are, as I am a beginner. (just been at python for few months and first time to hear these terms and to know that there are differences in outputs. coming from a literature background, i try jumping into python to expand my skills bec i want to explore data analysis, so pls pardon if the questions are so beginner) Also, thanks for sharing these answers, bec the material I am using has nothing about these differences in outputs.

Anyway, the platform I used is anaconda python.


The differences with Anaconda shouldn’t matter here.

REPL is “read-eval-print loop”. We use it to mean the Python console that you get by running python without arguments, where code is executed a statement at a time.