Running Python: REPL, Scripts, and Notebooks Explained

Python code can run in three common ways: the interactive REPL, a saved script file, or a notebook. The right choice depends on whether you want quick experiments, repeatable programs, or mixed code-and-notes work. This article shows how each one works, how to start it, and when to use it.

Difficulty: Beginner

Helpful to know first: basic command line use, how to save a file, and the idea that Python reads code one line at a time.

How Python Runs Code in the REPL, a Script, or a Notebook

Python always does the same core job: it reads your code, checks it, runs it, and shows results. The difference is where the code comes from and how long Python stays open. In the REPL, you type one statement at a time and Python keeps state between lines. In a script, Python reads a whole .py file and then exits when it is done. In a notebook, code runs in cells, and the same Python session can keep variables from one cell to the next.

The shortest useful example is the same in all three places:

Python
print("Hello, Python!")  # Output: Hello, Python!

That line can be typed in the REPL, saved in a script, or placed in a notebook cell. What changes is the workflow around it.

The REPL for quick experiments

The REPL is the interactive prompt you get after starting Python without a file. You type code, press Enter, and Python answers immediately. It is good for checking small ideas, trying expressions, and learning syntax.

Python
>>> print("hello")
hello
>>> 2 + 3
5

In the REPL, the result of an expression is shown automatically, but print() still matters when you want text output from code that will later live in a script.

Scripts for repeatable programs

A script is a text file that contains Python code, usually with the .py extension. You run it from the command line, and Python executes the file from top to bottom. This is the normal choice for programs you want to save, share, rerun, or test.

Terminal
# hello.py
print("Hello, script!")  # Output: Hello, script!

Running the file is separate from editing it, which makes scripts easier to reuse and debug than one-off terminal input.

Notebooks for cells, notes, and visible results

Notebooks let you mix code, output, and explanation in one document. Each cell can run separately, so you can explore ideas without rerunning the entire file every time. That is useful for data work, teaching, and experiments where seeing intermediate results matters.

Terminal
# In a notebook cell
name = "Ada"
print(f"Hello, {name}!")  # Output: Hello, Ada!

A notebook still runs Python code, but the notebook interface controls how cells are executed and displayed.

Starting the Python REPL from the Command Line

You can open the REPL by running Python with no file name. On many systems, the command is python3. Some environments use python, but on modern systems python3 is often the safer choice.

Once the prompt appears, you can type code line by line:

Terminal
$ python3
Python 3.12.x (main, ...)
>>> print("Ready")
Ready
>>> exit()

Some terminals also accept Ctrl-D on macOS and Linux or Ctrl-Z then Enter on Windows to leave the REPL. The exact prompt text may differ, but the idea is the same.

If you are learning Python, the REPL is one of the fastest ways to test expressions like 1 + 2, len("abc"), or type(3.14) before you put them in a file.

Running a Python Script File

To run a script, save your code in a .py file and pass that file to Python. This is the standard way to run programs you want to keep.

Terminal
# greet.py
name = "Mina"
print("Hello,", name)  # Output: Hello, Mina

Then run it from your terminal:

Terminal
$ python3 greet.py
Hello, Mina

Python reads the file from top to bottom. If the file contains a syntax error, the program stops before any real work begins. If the file runs successfully, it ends when the last line finishes.

Why if __name__ == "__main__" matters

Real scripts often define functions and then call them only when the file is run directly. That pattern keeps code reusable when the file is imported from somewhere else.

Python
def main() -> None:
    print("Running as a script")

if __name__ == "__main__":
    main()

If you run this file directly, it prints the message. If another file imports it, main() does not run automatically. That separation is one of the main reasons scripts scale better than loose REPL commands.

Using a Notebook Cell by Cell

A notebook runs code in cells instead of one whole file. You can execute a cell, inspect the result, change the next cell, and continue without restarting. The state stays alive until you restart the kernel or session.

Terminal
# Cell 1
count = 3

# Cell 2
print(count * 2)  # Output: 6

This can make notebooks feel convenient, but it also means execution order matters. If you run cell 2 before cell 1, count is not defined yet.

A notebook is not a different programming language. It is a different interface for running Python code.

How REPL State, Script State, and Notebook State Differ

In the REPL, variables you create stay available until you exit. In a script, variables disappear when the program ends. In a notebook, variables stay around between cells, which is helpful but can also hide mistakes if you run cells out of order.

Terminal
# REPL example
>>> x = 10
>>> x
10

That same idea in a script is different because the process ends immediately after the file runs. In a notebook, the variable can remain defined across many cells, which is why restarting the kernel is sometimes the cleanest way to debug a confusing result.

Another difference is input and output style. The REPL prints expression results automatically, while scripts usually rely on print(). Notebooks show the value of the last expression in a cell, but they still run ordinary Python underneath.

Common Mistakes When Running Python

Using the wrong command name for your system

Many beginners type python and get a shell error, or they type python3 where only python is configured. The command name depends on the operating system and installation.

Problem: The shell cannot find the command, so Python never starts. This is usually a PATH or installation issue, not a Python syntax problem.

Terminal
$ python3
bash: python3: command not found

Fix: Install Python 3.12+ and use the command that your system provides. On some Windows setups, the launcher is py; on many Unix-like systems, python3 is correct.

Terminal
$ py -3.12
Python 3.12.x (main, ...)
>>>

The corrected version works because the launcher points to an installed Python interpreter.

Saving code in a file but not running the file

It is easy to write code in an editor, save it, and forget to run the script from the terminal. Python does nothing until you start the interpreter or launch the file.

Problem: The file exists, but no output appears because the code has only been saved, not executed.

Terminal
# greet.py
print("Hello")

Fix: Run the file with Python after saving it.

Terminal
$ python3 greet.py
Hello

The corrected version works because Python actually reads and executes the file contents.

Expecting notebook cell order to reset automatically

In a notebook, old variables can stay in memory even after you edit earlier cells. That can make a later cell seem to work for the wrong reason.

Problem: The notebook keeps earlier state, so a variable may come from a previous run instead of the current cell order.

Terminal
# Cell 1
total = 5

# Cell 2
print(total)  # Output: 5

Fix: Restart the kernel and run cells from top to bottom when you want a clean result.

Terminal
# Cell 1 after restart
total = 5

# Cell 2
print(total)  # Output: 5

The corrected version works because the notebook state now matches the code you can see.

Predict the Output

These short examples check the most common misunderstandings when code runs interactively or from a file.

What happens when you reuse a variable name?

Python
value = 10
value = value + 5
print(value)

What does this print?

Show answer

It prints 15. The first line stores 10 in value. The second line reads the current value, adds 5, and stores the result back in the same name. The last line prints the updated value.

Python
15

What does integer division do?

Python
print(5 / 2)
print(5 // 2)

What does this print?

Show answer

The first line prints 2.5 because / does true division and returns a float. The second line prints 2 because // does floor division and removes the fractional part for positive numbers.

Python
2.5
2

Find the Bug

This code looks fine in a notebook or script, but it hides a common state bug. Find the problem before looking at the answer.

Python
def add_item(item, items=[]):
    items.append(item)
    return items

print(add_item("a"))
print(add_item("b"))
Show answer

The bug is the mutable default argument. The same list object is reused every time the function is called without an explicit items argument, so the second call keeps the first call’s data.

Terminal
# Output:
# ['a']
# ['a', 'b']

Fix: Use None as the default and create a new list inside the function when needed.

Python
def add_item(item, items=None):
    if items is None:
        items = []
    items.append(item)
    return items

print(add_item("a"))
print(add_item("b"))

The fixed version works because each call gets a fresh list unless you pass one in yourself.

When to Use the REPL, a Script, or a Notebook

Use the REPL when you want instant feedback on small ideas. Use a script when you want code you can save, rerun, test, and import. Use a notebook when you want to explore data or teach ideas in a document that mixes text and results.

ToolBest forStrengthWeakness
REPLSmall experimentsFast feedbackHard to keep organized
ScriptPrograms and automationRepeatable and shareableLess interactive
NotebookExploration and teachingVisible steps and outputState can get confusing

For a class assignment, a script is usually the safest choice. For checking a single expression, the REPL is faster. For a lab or data exploration, a notebook can be the most convenient.

Should you use notebooks for everything?

No. Notebooks are useful, but they are not the best format for every Python task. Scripts are easier to version control, rerun from scratch, and automate. That is why many teams prototype in notebooks and then move the final logic into script files or modules.

Should you use the REPL instead of writing files?

No. The REPL is great for learning and debugging, but a saved file is better once the code matters. Files preserve the exact program, make it easier to repeat results, and help you avoid losing work when you close the interpreter.

Try It Yourself

Write a small program that asks for a name and then prints a greeting. This practice helps you see how input, variables, and output work in a normal script.

  • Create a file named greeting.py.
  • Use input() to read a name from the user.
  • Store the name in a variable.
  • Print Hello, NAME! with the user’s name filled in.

Expected output:

Python
What is your name? Ada
Hello, Ada!

Hint: input() returns a string, so you can place the result directly into an f-string.

Show solution
Python
name = input("What is your name? ")
print(f"Hello, {name}!")

Summary

  • The REPL is for interactive, line-by-line experiments.
  • A script is a saved .py file that Python runs from top to bottom.
  • A notebook runs code in cells and keeps state between cells.
  • python3, python, or py may start Python depending on your system.
  • Scripts are best for repeatable programs; notebooks are best for exploration; the REPL is best for quick checks.
  • Notebook state can confuse you if you run cells out of order, so restart the kernel when results look wrong.

Next, learn how Python stores values in variables and how names, objects, and assignment work together. That will make the behavior of scripts, notebooks, and the REPL much easier to understand.