Computer Programming 1 (Python) • Python Fundamentals

Running Python (REPL vs. Script) — I can run Python code in a REPL and as a script and explain the difference.

Master the two fundamental ways to execute Python code and know when to use each one.

A Python coding terminal with a program flow and laptop.
Inside this lesson 10 sections 5 questions 6 figures

SECTION 1

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.

1964 • Dartmouth BASIC & Interactive Computing

The BASIC programming language introduced interactive, line-by-line computing to students, allowing them to type code and see results immediately on a terminal — an early form of the REPL experience.

1991 • Python 0.9 Released

Guido van Rossum released the first version of Python, which included both an interactive interpreter (REPL) and the ability to run script files. This dual approach was built into Python from day one.

2001 • IPython Enhances the REPL

Fernando Pérez created IPython, a more powerful interactive Python shell with features like tab completion, syntax highlighting, and history. It made the REPL experience significantly more user-friendly.

2014 • Jupyter Notebooks Emerge

Jupyter Notebooks combined the REPL concept with a web-based interface, allowing users to mix interactive code, text, and visualizations. This tool became hugely popular in data science and education.

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.

SECTION 2

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.

1

REPL (Read-Eval-Print Loop)

An interactive environment where Python reads your input, evaluates (executes) it, prints the result, and loops back to wait for your next input. You launch it by typing python in a terminal.
2

Script (File-Based Execution)

A Python file (ending in .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.
3

The Python Interpreter

The program that translates and executes your Python code. It powers both the REPL and script execution. Think of it as the engine that understands the Python language, regardless of how you feed it code.
4

Implicit vs. Explicit Output

In the REPL, typing an expression like 3 + 4 automatically displays 7. In a script, nothing appears unless you explicitly use print(). This is a critical difference.
✦

KEY TAKEAWAY

Think of the REPL like a conversation with Python — you say something, it responds, and you say something else. A script is more like handing Python a complete letter — it reads the whole thing from start to finish and then gives you the result. You use conversations (REPL) when you want quick feedback, and letters (scripts) when you have a full message to deliver.

SECTION 3

Visual Explanation — The REPL Cycle

REPL: Read → Eval → Print → LoopREAD>>> 3 + 4EVALcompute 3+4PRINT7LOOP (wait for next input)Terminal Session:>>> 3 + 47>>> print("Hello!")Hello!>>> ▌ (waiting for input)
The REPL cycle shown above illustrates how Python reads your typed expression, evaluates it, prints the result, and loops back to await your next command. Notice the >>> 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.

SECTION 4

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

  1. You type a command like python my_program.py in your terminal or click a "Run" button in your editor.
  2. The interpreter opens the file and reads all the code into memory.
  3. Python compiles the code into an intermediate form called bytecode (you don't see this step — it happens automatically behind the scenes).
  4. The bytecode is executed line by line from top to bottom.
  5. Output appears only when your code explicitly calls print() or writes to a file.
  6. When the last line finishes, control returns to the terminal and the program ends.
Script Execution: Top-to-Bottom Flowgreet.py1name = "Alice"2age = 163greeting = "Hi, " + name4print(greeting)5print("Age:", age)6age + 17print("Done!")Line 1Line 2Line 3Line 4Line 5Line 6Line 7Interpreter ActionsStore name = "Alice"Store age = 16Build greeting stringTerminal OutputHi, AliceAge: 16Line 6: age + 1 → NO OUTPUT!Done!⚠ Line 6 computed 17 but discarded the result because there isno print() call and no variable assignment.
This diagram shows how a Python script file (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.
⚠

The Big Difference

In the REPL, typing 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.

SECTION 5

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.

FeatureREPLScript
How you start itType python in terminal (no filename)Type python filename.py in terminal
PromptShows >>> and waitsNo prompt — runs immediately
Expression outputAutomatically displays resultMust use print()
Code persistenceLost when you close the sessionSaved in a .py file permanently
Best forQuick tests, calculations, exploringFull programs, reusable code, projects
Editing mistakesMust retype the line (no easy editing)Edit the file and re-run
Execution orderOne line at a time, on demandAll lines, top to bottom, at once

Key differences between REPL and script execution

Same Code, Two Different Environments🖥 REPL Session>>> x = 5>>> x * 315auto-printed!>>> print(x * 3)15>>> type(x)<class 'int'>auto-printed!>>> Output lines shown: 44📄 Script (test.py)1x = 52x * 3computed, NOT shown3print(x * 3)4type(x)computed, NOT shownTerminal output:15Output lines shown: 11
Both sides contain the same four logical operations, but the REPL produces four lines of output while the script produces only one line. The difference comes from the REPL's automatic printing of expression results.

SECTION 6

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.

Creating a Greeting — REPL vs. Script

  1. 1

    Step 1 — Try It in the REPL First

    Open your terminal and type 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 + "!")
    Output: Hello, Alice! — Each line was executed immediately as you typed it.
  2. 2

    Step 2 — Notice the REPL Behavior

    If you now type just 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.
    Key insight: the REPL shows the repr() of the value (with quotes for strings), while print() shows the str() version (without quotes).
  3. 3

    Step 3 — Convert to a Script

    Now open a text editor (such as VS Code, IDLE, or even Notepad) and create a new file called 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.
    The file greet.py now contains three lines of Python code, saved permanently on your computer.
  4. 4

    Step 4 — Run the Script

    Open your terminal, navigate to the folder containing 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.
    Output: What is your name? Alice Hello, Alice! Welcome to Python.
  5. 5

    Step 5 — The Advantage of a Script

    You can now run 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.
    Scripts are ideal for any program you want to keep, edit, and run multiple times.

SECTION 7

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.

ScenarioBest ChoiceWhy
Checking what a function doesREPLInstant feedback lets you experiment without creating a file
Writing a homework assignmentScriptYou need to save your work, edit it, and submit the file
Using Python as a calculatorREPLQuick math with auto-printed results — no file needed
Building a game or appScriptPrograms with many lines need a persistent, editable file
Testing a small code snippet before adding it to your projectREPLQuick verification prevents bugs from entering your main program
Automating a repetitive taskScriptYou want to run the same code repeatedly without retyping it

Common scenarios and the recommended execution mode

✦

KEY TAKEAWAY

Think of the REPL as a scratch pad — great for trying out ideas quickly and throwing them away. Think of a script as a Google Doc — you write, save, revise, and share your work. Most professional Python developers use both every single day. They test ideas in the REPL and build real programs as scripts.

SECTION 8

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 NowWhat Comes NextHow They Connect
Python REPL (>>> prompt)IPython & Jupyter NotebooksEnhanced REPLs with syntax highlighting, inline plots, and shareable "cells" of code
Running scripts with python file.pyIDE Run ButtonsTools like VS Code and PyCharm let you run scripts with a single click and show errors inline
Single-file scriptsMulti-file projects & modulesLarge programs split code across many .py files that import each other
Manual script executionAutomated testing & CI/CDScripts are run automatically by servers every time code changes, checking for bugs

How today's concepts connect to advanced Python development

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.

ℹ

Looking Ahead

In future lessons, you will also learn about the 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.

SECTION 9

Practice Problems

Question 1: CONCEPTUAL

What does REPL stand for, and what does each letter represent in terms of what the Python interpreter does?
Show answer
REPL stands for Read-Eval-Print Loop. Read means Python takes in your typed input from the prompt. Eval (evaluate) means Python interprets and executes that input as Python code. Print means Python displays the result of the evaluation. Loop means after printing, Python returns to the prompt and waits for the next input, repeating the cycle. This loop continues until you exit by typing exit() or pressing Ctrl+D.

Question 2: BASIC CALCULATION

You type 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.
Show answer
In the REPL, typing 7 * 8 produces the output 56 because the REPL automatically prints the result of any expression you type. In the script, the line 7 * 8 produces no output at all. Python still computes 56 internally, but the result is immediately discarded because no print() call or variable assignment captures it. To see 56 in a script, you would need to write print(7 * 8).

Question 3: INTERMEDIATE

Consider the following four lines saved in a file called 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?
Show answer
When running as a script, the output is simply 15. Lines 1 and 2 assign values (no output). Line 3 computes 10 * 15 = 150 but discards the result because there is no print(). Line 4 prints b, which is 15. In the REPL, Lines 1 and 2 produce no output (assignments don't auto-print). Line 3 auto-prints 150. Line 4 prints 15. So the REPL shows both 150 and 15, while the script shows only 15.

Question 4: APPLIED

Your friend is writing their first Python program and complains: "I put 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.
Show answer
Your friend's code is syntactically correct, and Python does compute both values internally — 8 and "HELLO". However, scripts require explicit output. Unlike the REPL, a script does not automatically display the result of expressions. Both results are computed and immediately discarded. To fix this, your friend should wrap each expression in a print() call: print(5 + 3) on line 1 and print("hello".upper()) on line 2. Alternatively, they could store the values in variables and print those. This is one of the most common mistakes beginners make when transitioning from the REPL to scripts.

Question 5: CRITICAL THINKING

A student argues: "The REPL is pointless because you can just use 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.
Show answer
The REPL offers several advantages that scripts cannot easily replicate. First, speed of exploration: when you want to check how a function works or test a quick calculation, the REPL gives instant results without creating, saving, and running a file. Second, interactive debugging: you can import a module in the REPL and call its functions one at a time to isolate bugs, which is faster than adding print statements throughout a script. Third, learning and discovery: beginners can use help() and dir() in the REPL to explore Python's built-in tools without writing a full program. However, a script is clearly superior for any program that needs to be saved, shared, or run repeatedly — for example, building a text-based game with hundreds of lines of code. The REPL would be impractical because you cannot go back and edit earlier lines, and all your work would be lost when you close the terminal.

SUMMARY

Lesson Summary

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.

Varsity Tutors • Computer Programming 1 (Python) • Running Python (REPL vs. Script)