Courses/Python Mastery/Module 1: What is Python? (Introduction)
Module 1 · Lesson 315 minBeginner

How Python Works: Interpreter & Scripts

Lesson goal
Understand what happens when you 'run' Python — interpreters vs compilers, .py files, and why Python feels like English.

How Python Works: Interpreter & Scripts

Computers only understand one language: machine code — billions of tiny on/off electrical signals represented as 1s and 0s. Everything else — Python, English, emoji — is for humans. So how does your Python code become electricity?

The translator metaphor

Imagine you speak only Hindi and need to give instructions to someone who speaks only Tamil. You have two options:

  • Translate everything first, then hand over the full document (compile)
  • Have a live interpreter translate line-by-line as you speak (interpret)
  • Languages like C use option 1 — a compiler translates the whole program into machine code *before* it runs. Python uses option 2 — the Python interpreter reads your code and executes it line-by-line, on the spot.

    code
    You write:        print("hello")
                           ↓
    Interpreter:      translates + runs that line, immediately
                           ↓
    Screen:           hello

    What this means for you (the good)

  • Instant feedback — save the file, run again, see changes immediately. No waiting for builds.
  • Errors are readable — the interpreter stops at the problem line and tells you what it hit. C compilers sometimes hand you cryptic hex dumps; Python says "you forgot a quotation mark on line 3."
  • Interactive mode — you can open a Python "REPL" (read-eval-print loop) and type code one line at a time, like chatting with the computer. Try it: open a terminal and type python, then 2 + 2 and press Enter.
  • What this means for you (the tradeoff)

    Interpreting line-by-line is slower than running pre-translated machine code. That's the "Python is slow" thing from the last lesson. For the apps in this course — and for most real-world software — the difference is invisible.

    Scripts: the files you'll live in

    A script is just a text file full of Python instructions, saved with the ending .py:

    code
    my_first_script.py

    That's genuinely it. A Python "program" is not a special binary file or an installer — it's a text file you could open in Notepad. The interpreter reads it top to bottom, executing each line in order:

    code
    # the interpreter runs these IN THIS ORDER
    print("first")     ← 1st
    print("second")    ← 2nd
    print("third")     ← 3rd

    Order matters. If line 1 uses a variable that line 5 creates, line 1 fails — the interpreter hasn't reached line 5 yet. This "top-to-bottom" thinking becomes very important later, especially in Streamlit where the whole script re-runs on every click.

    The two ways you'll run Python

  • Interactive mode — type python in a terminal, experiment line-by-line, press Ctrl+D (or exit()) to leave. Great for quick tests.
  • Script mode — write a .py file, run python my_file.py. This is how every project in this course works.
  • One more thing: Python is everywhere you'll look

    When you install Python, you get the interpreter plus the standard library — hundreds of built-in toolboxes (math, files, dates, random numbers...) that need no installation. That's why Python "batteries are included."


    ✅ Checkpoint

  • What's the difference between a compiler and an interpreter? *(Compiler translates everything before running; interpreter translates and runs line-by-line)*
  • What's a .py file, really? *(A plain text file of Python instructions)*
  • If line 1 uses a variable created on line 5, what happens? *(Error — the interpreter runs top-to-bottom and hasn't seen line 5 yet)*
  • What are the two modes of running Python? *(Interactive/REPL and script mode)*
  • Next: we install Python and VS Code, and all of this becomes real on your machine.