# Return Keyword in Python - comparison

**URL:** <https://discuss.python.org/t/return-keyword-in-python-comparison/41826>\
**Category:** Python Help\
**Created:** [December 24, 2023, 4:20pm UTC](https://discuss.python.org/t/return-keyword-in-python-comparison/41826 "2023-12-24T16:20:10Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Moonlight001](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Moonlight001](https://discuss.python.org/u/Moonlight001)\
**Post date:** [December 24, 2023, 4:20pm UTC](https://discuss.python.org/t/return-keyword-in-python-comparison/41826/1 "2023-12-24T16:20:10Z")

</div>

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:

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

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

```

Second Code:

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

cube(2)

```

Thank you!

---

<div class="post-metadata">

**Author:** ![olekskrav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/olekskrav/32/14493_2.png) [@olekskrav](https://discuss.python.org/u/olekskrav)\
**Post date:** [December 24, 2023, 4:26pm UTC](https://discuss.python.org/t/return-keyword-in-python-comparison/41826/2 "2023-12-24T16:26:07Z")

</div>

> [@Moonlight001](#):
>
> Why does it not display a result (123) in the first code, but shows the result in the second code?

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.

---

<div class="post-metadata">

**Author:** ![Moonlight001](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Moonlight001](https://discuss.python.org/u/Moonlight001)\
**Post date:** [December 24, 2023, 4:27pm UTC](https://discuss.python.org/t/return-keyword-in-python-comparison/41826/3 "2023-12-24T16:27:41Z")

</div>

![Screenshot_57](https://us1.discourse-cdn.com/flex002/uploads/python1/original/3X/a/9/a94ad5b7d71b29a6385c668505c7b50a8c4bdcb2.jpeg)

---

<div class="post-metadata">

**Author:** ![Moonlight001](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Moonlight001](https://discuss.python.org/u/Moonlight001)\
**Post date:** [December 24, 2023, 4:28pm UTC](https://discuss.python.org/t/return-keyword-in-python-comparison/41826/4 "2023-12-24T16:28:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Moonlight001](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Moonlight001](https://discuss.python.org/u/Moonlight001)\
**Post date:** [December 24, 2023, 4:29pm UTC](https://discuss.python.org/t/return-keyword-in-python-comparison/41826/5 "2023-12-24T16:29:09Z")

</div>

![Screenshot_58](https://us1.discourse-cdn.com/flex002/uploads/python1/original/3X/3/b/3bb1cb4fc922a7788e840fce104aab8dc9c172e8.jpeg)

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [December 24, 2023, 5:01pm UTC](https://discuss.python.org/t/return-keyword-in-python-comparison/41826/6 "2023-12-24T17:01:35Z")

</div>

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

Like this:

````auto
```
print('Hello')
```

````

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.

```auto
def my_func():
    return 123

print(my_func())

```

---

<div class="post-metadata">

**Author:** ![Moonlight001](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Moonlight001](https://discuss.python.org/u/Moonlight001)\
**Post date:** [December 24, 2023, 5:23pm UTC](https://discuss.python.org/t/return-keyword-in-python-comparison/41826/7 "2023-12-24T17:23:28Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [December 24, 2023, 5:52pm UTC](https://discuss.python.org/t/return-keyword-in-python-comparison/41826/8 "2023-12-24T17:52:28Z")

</div>

> [@barry-scott](#):
>
> Please not share screen shots of code

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…

---

<div class="post-metadata">

**Author:** ![d\_n](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/d_n/32/7498_2.png) [@d\_n](https://discuss.python.org/u/d_n)\
**Post date:** [December 24, 2023, 7:39pm UTC](https://discuss.python.org/t/return-keyword-in-python-comparison/41826/9 "2023-12-24T19:39:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [December 24, 2023, 8:17pm UTC](https://discuss.python.org/t/return-keyword-in-python-comparison/41826/10 "2023-12-24T20:17:59Z")

</div>

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

> <https://stackoverflow.com/questions/73368843/why-do-i-get-extra-output-from-code-using-print-or-output-at-all-using-retu>

---

<div class="post-metadata">

**Author:** ![olekskrav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/olekskrav/32/14493_2.png) [@olekskrav](https://discuss.python.org/u/olekskrav)\
**Post date:** [December 24, 2023, 9:49pm UTC](https://discuss.python.org/t/return-keyword-in-python-comparison/41826/11 "2023-12-24T21:49:20Z")

</div>

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:

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

print("This lesson is interesting!")
boring_function()

```

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.

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [December 24, 2023, 10:44pm UTC](https://discuss.python.org/t/return-keyword-in-python-comparison/41826/12 "2023-12-24T22:44:00Z")

</div>

> [@Stefan2](#):
>
> screenshots can show that context

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.

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [December 24, 2023, 10:59pm UTC](https://discuss.python.org/t/return-keyword-in-python-comparison/41826/13 "2023-12-24T22:59:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Moonlight001](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Moonlight001](https://discuss.python.org/u/Moonlight001)\
**Post date:** [December 25, 2023, 4:24am UTC](https://discuss.python.org/t/return-keyword-in-python-comparison/41826/14 "2023-12-25T04:24:38Z")

</div>

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.

Thanks!

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [December 25, 2023, 2:13pm UTC](https://discuss.python.org/t/return-keyword-in-python-comparison/41826/15 "2023-12-25T14:13:10Z")

</div>

The differences with Anaconda shouldn’t matter here.

[REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) 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.
