Historical Context & Motivation
Long before Python existed, programmers faced a basic choice: should a computer execute instructions one at a time while you watch, or should it run an entire file of instructions all at once? This question goes back to the earliest days of computing. In the 1950s and 1960s, computers were enormous machines shared by many users, and programmers submitted their code on punch cards — physical cards with holes representing instructions. The computer ran the whole stack of cards as a batch job, which is the ancestor of running a script. Later, as computers became more interactive, developers created systems that let you type a command and instantly see the result. This style of instant feedback eventually became what we call a REPL — short for Read-Eval-Print Loop.
So why does Python give you two ways to run code? The answer comes down to different needs. Sometimes you want to quickly test a small idea — like checking what 2 ** 10 equals — and a REPL lets you do that instantly. Other times, you need to write a full program with many lines of logic, save it, and run it repeatedly. That is what scripts are for. Understanding both approaches, and knowing when to use each, is one of the first skills every Python programmer needs.
Core Principles & Definitions
Before diving deeper, let's establish the key ideas behind running Python code. Every time you write Python, the code must be interpreted — that is, translated from human-readable text into instructions the computer can execute. Python is an interpreted language, which means it reads and executes your code line by line rather than compiling the entire program first. This interpretation can happen in two modes: interactively through a REPL, or by running a saved file called a script.
REPL (Read-Eval-Print Loop)
python in a terminal.Script (File-Based Execution)
.py) containing multiple lines of code. You run it with a command like python my_script.py, and Python executes every line from top to bottom without pausing for input.The Python Interpreter
Implicit vs. Explicit Output
3 + 4 automatically displays 7. In a script, nothing appears unless you explicitly use print(). This is a critical difference.Visual Explanation — The REPL Cycle
>>> prompt — this is how you know you are inside the Python REPL.The diagram above captures the heart of how the REPL works. Each time you type something at the >>> prompt, Python goes through the full cycle. The key detail to notice is that expressions like 3 + 4 automatically display their result without needing print(). This automatic display is unique to the REPL — it exists because the REPL is designed for exploration and quick testing. In a script file, you would see nothing on screen unless you explicitly tell Python to print something.
How It Works — Script Execution Flow
When you run a Python script, the process is fundamentally different from the REPL. Instead of waiting for you to type each line, the interpreter opens your .py file and executes every line sequentially — from the very first line to the very last. There is no interactive prompt, no automatic printing of expression values, and no pausing between lines. The interpreter reads the entire file, processes it, and then returns control to your terminal.
Step-by-Step: What Happens When You Run a Script
- You type a command like
python my_program.pyin your terminal or click a "Run" button in your editor. - The interpreter opens the file and reads all the code into memory.
- Python compiles the code into an intermediate form called bytecode (you don't see this step — it happens automatically behind the scenes).
- The bytecode is executed line by line from top to bottom.
- Output appears only when your code explicitly calls
print()or writes to a file. - When the last line finishes, control returns to the terminal and the program ends.
greet.py) is executed line by line. Notice that Line 6 contains the expression age + 1, which computes 17 — but produces no output because there is no print() call. In the REPL, that same line would have displayed 17 automatically.age + 1 shows 17 on screen. In a script, the same expression computes 17 internally but shows nothing. This is the most common point of confusion for beginners. When writing scripts, always use print() to display values.REPL vs. Script — Side-by-Side Comparison
Now that you understand how each mode works individually, let's place them side by side. The table below summarizes every important difference between the REPL and script execution. Keep this comparison in mind as you develop your programming habits — experienced developers switch between both modes regularly, using each one for the task it handles best.
| Feature | REPL | Script |
|---|---|---|
| How you start it | Type python in terminal (no filename) | Type python filename.py in terminal |
| Prompt | Shows >>> and waits | No prompt — runs immediately |
| Expression output | Automatically displays result | Must use print() |
| Code persistence | Lost when you close the session | Saved in a .py file permanently |
| Best for | Quick tests, calculations, exploring | Full programs, reusable code, projects |
| Editing mistakes | Must retype the line (no easy editing) | Edit the file and re-run |
| Execution order | One line at a time, on demand | All lines, top to bottom, at once |
Worked Example — Building a Greeting Program
Let's walk through creating a simple Python program in both the REPL and as a script. We want a program that asks for a user's name and then prints a personalized greeting. This example will highlight the practical differences between the two approaches.
python to enter the REPL. You will see the >>> prompt. Now type the following lines one at a time, pressing Enter after each:
>>> name = input("What is your name? ")
Python will pause and wait for you to type a name. Type Alice and press Enter.
>>> print("Hello, " + name + "!")Hello, Alice! — Each line was executed immediately as you typed it.name at the prompt, the REPL automatically prints 'Alice' (with quotes, showing it's a string). This automatic display is a REPL feature that helps you inspect variables quickly. You did not need to write print(name) — though that would also work.print() shows the str() version (without quotes).greet.py. Type all of the following lines into the file:
name = input("What is your name? ")
print("Hello, " + name + "!")
print("Welcome to Python.")
Save the file.greet.py now contains three lines of Python code, saved permanently on your computer.greet.py, and type:
python greet.py
Python will execute Line 1 (which pauses to ask for your name), then Line 2 (which prints the greeting), then Line 3 (which prints the welcome message). After the last line runs, the program ends and you are returned to the terminal prompt.What is your name? Alice
Hello, Alice!
Welcome to Python.python greet.py again and again — each time the program starts fresh and asks for a new name. You can also share the file with a friend, and they can run it on their computer. In the REPL, your work would have been lost the moment you closed the terminal. Scripts give you persistence and reusability.When to Use the REPL vs. a Script
Knowing the difference is great, but knowing when to use each approach is what makes you a more efficient programmer. Neither the REPL nor script execution is inherently "better" — they each have strengths that make them ideal for different situations. The table below breaks down common scenarios you will encounter as a student learning Python.
| Scenario | Best Choice | Why |
|---|---|---|
| Checking what a function does | REPL | Instant feedback lets you experiment without creating a file |
| Writing a homework assignment | Script | You need to save your work, edit it, and submit the file |
| Using Python as a calculator | REPL | Quick math with auto-printed results — no file needed |
| Building a game or app | Script | Programs with many lines need a persistent, editable file |
| Testing a small code snippet before adding it to your project | REPL | Quick verification prevents bugs from entering your main program |
| Automating a repetitive task | Script | You want to run the same code repeatedly without retyping it |
Connection to Advanced Tools
The REPL and script concepts you have learned here are foundational, but as you grow as a programmer, you will encounter more advanced tools that build on these same ideas. Understanding where the REPL and scripts fit in the broader ecosystem will help you appreciate what comes next.
| What You Know Now | What Comes Next | How They Connect |
|---|---|---|
Python REPL (>>> prompt) | IPython & Jupyter Notebooks | Enhanced REPLs with syntax highlighting, inline plots, and shareable "cells" of code |
Running scripts with python file.py | IDE Run Buttons | Tools like VS Code and PyCharm let you run scripts with a single click and show errors inline |
| Single-file scripts | Multi-file projects & modules | Large programs split code across many .py files that import each other |
| Manual script execution | Automated testing & CI/CD | Scripts are run automatically by servers every time code changes, checking for bugs |
As you progress in Computer Science, you will likely use Jupyter Notebooks in data science or science classes. Notebooks are essentially a hybrid: they combine the interactivity of the REPL with the persistence of a script. You write code in "cells," execute them one at a time (like the REPL), and save the whole notebook as a file (like a script). Understanding the REPL vs. script distinction makes learning notebooks much more intuitive.
if __name__ == "__main__": pattern, which controls whether code runs when a script is executed directly versus when it is imported by another file. This powerful pattern only exists in scripts — not in the REPL — and it becomes essential as your programs grow in complexity.Practice Problems
7 * 8 in both the REPL and as a line in a script called math_test.py. What output does each environment produce? Explain why.demo.py:
a = 10
b = a + 5
a * b
print(b)
What is the exact terminal output when you run python demo.py? What would the output be if you typed these same four lines one at a time in the REPL?5 + 3 on line 1 and "hello".upper() on line 2 of my script, but when I run it, nothing shows up!" Explain what is happening and how to fix their script.print() in a script to see everything. Why would anyone ever use the REPL?" Write a response that presents at least three concrete advantages of the REPL over scripts. Then describe one scenario where a script is clearly superior to the REPL.Lesson Summary
Python gives you two primary ways to run code. The REPL (Read-Eval-Print Loop) is an interactive mode launched by typing python in your terminal, identified by the >>> prompt. It executes code one line at a time and automatically displays expression results without requiring print(). The REPL is ideal for quick tests, calculations, and exploration, but your work is not saved when you close the session.
A Python script is a .py file executed with python filename.py. The interpreter runs every line from top to bottom without pausing, and output appears only when you explicitly use print(). Scripts are essential for building real programs because they offer persistence, editability, and reusability. The key takeaway: skilled programmers use the REPL for exploration and scripts for production, switching between both as needed.