Running Code and Understanding Output

faq
notebooks
beginner
Diagnose missing output, stale notebook state, and confusion between display, print, and return.
TipWant to try the code?

You can read this FAQ as a standalone article. Use the Open in Colab button above if you want an optional notebook for running each snippet, changing one line, and comparing the result with your prediction.

Code can finish without showing a value, and a notebook can use state that is not obvious from the order of cells on the page. Both situations become easier to diagnose when you separate calculation, output, and runtime state.

Short answer

Python does not automatically print every value it calculates. Use print(...) when a script should deliberately show something. A notebook may display its final bare expression, but that behavior is supplied by the notebook interface and is not the same as printing.

When a notebook shows an old or surprising result, check which definition or assignment cell last ran. Editing a cell does not update the runtime until that cell executes again.

Why did nothing print?

Symptom: Python finishes without an error, but the terminal or notebook does not show the result you expected.

Likely reason: the code created or changed a value without performing an output action. Python does not print every intermediate value in a script.

Compare these two scripts:

message = "Course ready"
message = "Course ready"
print(message)

Both scripts store text under message. Only the second script asks print to send the text to standard output, which is normally visible in the terminal or notebook output area.

Inspect before changing code

Ask these questions in order:

  1. Did the line run, or did execution stop earlier with an error?
  2. Does the code contain print(...), a display call, or another output action?
  3. Is that action inside a condition or loop that never runs?
  4. Is the output going somewhere else, such as a file?

Temporary markers can show how far execution gets:

print("before calculation")
total = 4 * 5
print("after calculation")
print(total)

Use specific markers while debugging, then remove them when they are no longer helpful. A row of identical print("here") calls does not tell you which point the program reached.

NoteTry it

Remove print(total) but leave the two markers. Predict the visible lines, run the code, and explain why the calculation still occurs.

Why does a notebook display some values without print?

An interactive notebook normally displays the value of the last bare expression in a cell.

subtotal = 8 + 2
subtotal

The last line asks Python to evaluate the name subtotal. Colab or Jupyter then displays the resulting value, 10.

Move another expression below it:

subtotal = 8 + 2
subtotal
"cell finished"

The notebook displays only:

'cell finished'

The value subtotal was still read, but it was not the cell’s final expression. Use print when each value needs deliberate visible output:

subtotal = 8 + 2
print(subtotal)
print("cell finished")

Expected output:

10
cell finished
TipScript or notebook?

A bare expression such as subtotal can be displayed by a notebook interface. The same line in a .py script normally produces no visible output. A print(subtotal) call is explicit in both environments.

Quick check: visible output

What is the difference between printing and returning?

A function can calculate a value, print a value, return a value, or combine those actions. Printing and returning solve different problems.

This function prints a total:

def show_total(price, quantity):
    total = price * quantity
    print(total)

result = show_total(3, 2)
print("Stored result:", result)

Expected output:

6
Stored result: None

Line by line:

  1. def show_total(price, quantity): defines a function with two parameter names.
  2. total = price * quantity calculates a local value when the function runs.
  3. print(total) makes that value visible, but does not send it back to the caller.
  4. The function reaches its end without return, so Python returns None.
  5. result therefore stores None, even though 6 appeared earlier.

Return the value when later code needs to use it:

def calculate_total(price, quantity):
    total = price * quantity
    return total

result = calculate_total(3, 2)
print("Stored result:", result)

Now the output is Stored result: 6. The return statement ends the function call and sends the value of total back to the assignment.

NoteTry it

Predict the result of calculate_total(3, 4) + 1, then run it. Replace return total with print(total) and run the expression again. Read the resulting error as evidence about which version provides a usable value.

For a full introduction to function results, continue with Function Basics.

Why did a notebook use an old value?

Notebook cells share a running Python process called a runtime or kernel. That process remembers names created by every cell that has run. The visual order on the page does not guarantee the execution order.

Imagine these two cells:

# Cell A
price = 5
# Cell B
print(price * 2)

If you run Cell A and then Cell B, the output is 10. Now edit Cell A so it says price = 8 but do not run it. Running Cell B still prints 10, because the runtime remembers the last executed assignment, price = 5. Editing text does not execute it.

Inspect execution state

  • Look at each cell’s execution indicator or number.
  • Run the cell that assigns the suspicious name.
  • Run its dependent cell again.
  • If the history is unclear, restart and run all cells from top to bottom.

A notebook uses execution order, not simply the order of cells on the page.

flowchart LR
  edit["Edit Cell A<br/>price = 8"] --> text["Cell text changes"]
  text -. "not run yet" .-> old["Runtime still stores<br/>price = 5"]
  run["Run Cell A"] --> new["Runtime stores<br/>price = 8"]
  new --> output["Run Cell B<br/>prints 16"]

When should I restart the runtime?

Restart when you need a clean test of whether the notebook works from the beginning. Common clues include:

  • a name exists even though its assignment cell no longer exists;
  • output depends on a cell you cannot identify;
  • cells were run repeatedly and out of order;
  • an import or installed package behaves differently from a fresh session; or
  • you want to confirm that another learner can run the notebook top to bottom.

A restart deletes values stored in memory. It does not repair code, save unsaved edits, or automatically rerun setup cells.

Safe restart routine

  1. Save the notebook.
  2. Restart the runtime or kernel.
  3. Run all cells from the top.
  4. Stop at the first error or surprising result.
  5. Inspect that cell and the cells it depends on.
WarningCommon mistake

Restarting repeatedly without running the setup cells can create more NameError messages. After a restart, imports, assignments, file uploads, and function definitions must run again before later cells can use them.

For a fuller introduction to cells and runtimes, continue with Google Colab. To learn how to interpret a traceback encountered during this routine, use Error Messages.

Try the state changes in Colab

Run these cells in order:

tax_rate = 0.10
price = 20
final_price = price + price * tax_rate
final_price

Then complete three controlled experiments:

  1. Change tax_rate to 0.20 without running its cell. Predict what the second cell will display.
  2. Run the tax cell and then the price cell. Explain why the output changes.
  3. Restart the runtime and run only the price cell. Use the error to identify which state is missing.
Show one possible explanation
  1. The second cell still displays 22.0 because editing the first cell did not change the runtime’s stored tax_rate.
  2. After the first cell runs, the stored rate becomes 0.20, so the second cell displays 24.0.
  3. After a restart, tax_rate does not exist. The price cell raises NameError until the setup cell runs again.

Quick check: notebook state

A compact checklist

TipKey points
  • Calculation and visible output are separate actions.
  • A notebook may display its last expression, while a script normally will not.
  • print communicates a value; return gives a value back to the caller.
  • Editing a notebook cell does not update runtime state until that cell runs.
  • Restart and run all is a controlled test, not a substitute for reading the first clue.
Back to top