Running Python Code in Jupyter
# 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.
Shift + Enter: Runs the current cell and moves your cursor to the next cell. (This is the most common shortcut).
-
2.
Ctrl + Enter: Runs the current cell and keeps your cursor on the same cell. (Great for running the same test repeatedly).
-
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:
*(Run Cell 1. The Kernel now remembers x = 100)*
Cell 2:
*(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. You write this in Cell 1 and run it:
- 2. You write this in Cell 2 and run it:
- 3. You go *back up* to Cell 1, change the code, and run it again:
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.
*Output:*
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:
*(Run this. Now tweak the numbers and run it again to update the Kernel)*
Cell 2:
*(Run this. You instantly see 20000)*
Cell 3:
*(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 = 5STILL 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 pdstatements in the very first cell of the notebook, so they are loaded before any other code runs.
9. MCQs
What is the keyboard shortcut to run a cell and automatically move to the next cell?
What does Ctrl + Enter do?
What does the number in In [5]: indicate?
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?
How does Jupyter handle output on the last line of a cell?
What is the "Hidden State Problem" in Jupyter?
What is the best way to ensure your notebook is reproducible before sharing it?
What happens when you Restart the Kernel?
If you type x on line 1 and y on line 2 of a cell without print(), what is displayed?
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 theIn [ ]: execution numbers. If your notebook state becomes confusing, rely on "Kernel -> Restart & Run All" to reset the environment and test reproducibility.