The interpreter and every way you launch it

Prompt, file, python -m, a double-click, a shebang on Linux, IDLE. Same language, different doors. Plus: UTF-8 in the source file, and argv as the words after the command.

School notice board

print() is how your program speaks.

You will be able to

  • Tell REPL, saved file, python -c, python -m and double-click apart
  • Know source files are UTF-8 and may start with a shebang
  • Treat argv as the words after the launch command

Doors into the same interpreter

The interactive prompt (REPL) is a scratchpad: try one expression, throw it away. A saved .py file is what a technician double-clicks or a cron job runs at 06:00. python -c "print(1)" is one shot from the shell. python -m pip is ‘run this module as a program’. IDLE and other editors are just nicer doors into the same engine.

On Linux a file may start with #!/usr/bin/env python3 so the OS knows which interpreter to use. The next line can declare an encoding; the default you should assume is UTF-8. Type quit() or an end-of-file key to leave the prompt. Continuation lines in the REPL show ... when a block is unfinished.

argv is a list of strings

Whatever you type after python app.py becomes sys.argv on a laptop — the script name, then options. Here we model that as a list you pass into a function. Missing arguments are a policy: print a usage line, do not crash with a mystery index error.

python -i script.py runs the file then leaves you in the prompt — useful for poking at names after a failure. It is not how you ship a nightly job.

Laptop: encoding + shebang + argv

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys

def title(argv):
    if len(argv) < 2:
        return "usage: report.py LABEL"
    return argv[1]

if __name__ == "__main__":
    print(title(sys.argv))

Words that matter

REPL
Read-eval-print loop — the >>> prompt.
shebang
First line #!... telling Unix which program runs this file.
argv
Argument vector — words from the command line.

Common mistakes

Avoid: Shipping a job that only works if someone is sitting in IDLE.

Do this: A .py file with a door, launched by the scheduler.

Run it step by step

Each box is a real program. Press Run, change a number, Run again — the output must follow your code.

1. Step 1 — a launch banner

This is what a file prints when someone actually launched it.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

2. Step 2 — argv as a list you pass

Missing label is a usage line, not a crash.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Example program — Config for how this PC launches

Folder and label would come from argv or env on a laptop.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Your turn — Need a label

If label is "", print usage. Otherwise print the label. Use label = "" so usage appears.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Self-assessment

Check your understanding before you mark the lesson complete.

1. python -m pip means…
2. Campus source files should usually be saved as…

Progress is stored in a browser cookie on this device.