Skip to main content
Jupyter Notebooks
CHAPTER 04 Beginner

Running Python Code in Jupyter

Updated: May 18, 2026
5 min read

# CHAPTER 4

Running Python Code in Jupyter

1. Chapter Introduction

Writing code in a Jupyter Notebook is different from writing a standard Python script. In a standard script, the computer reads and executes the code from top to bottom, linearly. In Jupyter, you can run cell 5, then cell 1, then cell 10. This flexibility is brilliant for experimentation, but it introduces the most common pitfall for data scientists: Out-of-Order Execution. This chapter explains how the Kernel executes code and remembers variables.

2. Executing Code Cells

To run the code inside a cell, you have three options:

  1. 1. Shift + Enter: Runs the current cell and moves your cursor to the next cell. (This is the most common shortcut).
  1. 2. Ctrl + Enter: Runs the current cell and keeps your cursor on the same cell. (Great for running the same test repeatedly).
  1. 3. Alt + Enter: Runs the current cell and inserts a brand new blank cell immediately below it.

3. The Kernel's Memory (Variable State)

Think of the Kernel as a continuously running brain. Once you define a variable in *any* cell and run it, the Kernel remembers that variable until you explicitly shut the Kernel down.

Cell 1:

python
1
x = 100

*(Run Cell 1. The Kernel now remembers x = 100)*

Cell 2:

python
12
y = 50
print(x + y)

*(Run Cell 2. Output: 150)*

Even though x is defined in Cell 1, Cell 2 knows about it because they share the same Kernel memory.

4. The Danger of Out-of-Order Execution

Because the Kernel remembers everything based on *when you ran it in time*, not *where it is on the page*, things can get confusing.

Look at the In [ ]: numbers. They tell you the chronological order in which cells were executed.

Scenario:

  1. 1. You write this in Cell 1 and run it:

python
12
# In [1]:
my_variable = 10
  1. 2. You write this in Cell 2 and run it:
python
123
# In [2]:
print(my_variable)
# Output: 10
  1. 3. You go *back up* to Cell 1, change the code, and run it again:
python
12
# In [3]:
my_variable = 999

If you look at the screen from top to bottom, it looks like Cell 2 should print 999. But because you haven't re-run Cell 2 yet, its visible output is still 10!

This is the Hidden State Problem. The code on the screen does not match the reality inside the Kernel's memory.

5. Fixing the Hidden State: Restarting the Kernel

When your notebook starts acting weird, or variables contain data they shouldn't, your Kernel memory has become a tangled mess of out-of-order executions.

The Solution: Click Kernel -> Restart & Clear Output in the top menu. This is the equivalent of turning the computer off and on again. It wipes the Kernel's memory clean. All variables are forgotten.

*Best Practice:* Before you share a notebook with a colleague or submit an assignment, ALWAYS click Kernel -> Restart & Run All. If the notebook runs from top to bottom without errors, it is reproducible.

6. Printing and Displaying Output

In standard Python, you must use print() to see a variable. In Jupyter, the last item in a cell is automatically displayed.

python
12345678
name = "Alice"
age = 30

# Using print
print(name)

# If we just type the variable name on the last line, Jupyter displays it automatically
age

*Output:*

text
12
Alice
30

Notice that if you put name and age on their own lines without print(), only age (the last one) will be displayed. If you want to see both, you must print() the first one.

7. Mini Project: Interactive Calculator

Let's use Jupyter's interactivity to build a step-by-step calculator.

Cell 1:

python
123
# Define our base values
revenue = 50000
expenses = 30000

*(Run this. Now tweak the numbers and run it again to update the Kernel)*

Cell 2:

python
123
# Calculate profit
profit = revenue - expenses
profit

*(Run this. You instantly see 20000)*

Cell 3:

python
123
# Calculate profit margin percentage
margin = (profit / revenue) * 100
print(f"Profit Margin is {margin}%")

*(Run this. You instantly see 40.0%)*

8. Common Mistakes

  • Deleting a cell and thinking the variable is gone: If you run x = 5, then delete that cell, x = 5 STILL exists in the Kernel's memory! Deleting a cell only deletes text on the screen, it does not delete memory in the Kernel. You must restart the Kernel to clear memory.
  • Importing libraries at the bottom: It is best practice to put all your import pandas as pd statements in the very first cell of the notebook, so they are loaded before any other code runs.

9. MCQs

Question 1

What is the keyboard shortcut to run a cell and automatically move to the next cell?

Question 2

What does Ctrl + Enter do?

Question 3

What does the number in In [5]: indicate?

Question 4

If you run a cell that defines x=10, and then delete that cell from the screen, what is the value of x in the Kernel?

Question 5

How does Jupyter handle output on the last line of a cell?

Question 6

What is the "Hidden State Problem" in Jupyter?

Question 7

What is the best way to ensure your notebook is reproducible before sharing it?

Question 8

What happens when you Restart the Kernel?

Question 9

If you type x on line 1 and y on line 2 of a cell without print(), what is displayed?

Question 10

Where is the best place to put import statements in a notebook?

10. Interview Questions

  • Q: A colleague sends you a Jupyter Notebook. You run it from top to bottom, but it crashes on cell 4 saying a variable is undefined. Your colleague swears it worked on their machine. What is the most likely cause?
  • Q: Explain how cell execution order In [x] relates to the Kernel's memory state.

11. Summary

Jupyter allows nonlinear code execution, making experimentation incredibly fast. However, the Kernel's memory is determined by *time* (the order you run cells), not *space* (top to bottom on the screen). Always keep an eye on the In [ ]: execution numbers. If your notebook state becomes confusing, rely on "Kernel -> Restart & Run All" to reset the environment and test reproducibility.

12. Next Chapter Recommendation

In Chapter 5: Markdown Cells and Documentation, we step away from Python code to learn how to write beautiful text, headers, bullet points, and math equations to document our thought processes.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·