SwitchNRoutesPython (PCAP) lessonsAll interactive lessonsBlogGet a quote
PYTHON PCAP | INTERACTIVE LESSON

What a Program Is and How Python Runs It

A program is a recipe. Python is the cook that reads it one line at a time. Before any syntax, see the whole journey from file to result.

Where this fits: Pre-Python chapter 1 of 5. Leads into PCAP Module 1 (core language).
All Python (PCAP) lessons
Progress is saved in this browser

1. A program is a recipe

A computer does exactly what it is told, in the order it is told, and nothing else. A program is a list of instructions written in a language the computer's helper (an interpreter) understands. Cooking is a good picture: ingredients (data), steps (instructions), a cook who follows the steps without thinking for themselves.

From file to resultYou writehello.pyPython readsthe fileTranslates tobytecodePython virtualmachine runs itOutput onthe screen
You write text. Python translates it and runs it, line by line.

Python is called an interpreted language, which is a simplification: it first compiles your text to a compact bytecode (the .pyc files in __pycache__), then a virtual machine executes it. You never have to think about the bytecode; you only need to know that an error found while translating (a SyntaxError) stops the program before anything runs, but an error found while running (like dividing by zero) stops it at that exact line.

2. Two ways to run Python

The REPL (interactive)A script file
Startpython (or py on Windows)python hello.py
Good forTrying one idea, checking what something doesReal programs you keep and run again
Result of each linePrinted immediatelyOnly what you print()
Leaveexit() or Ctrl+Z then Enter (Windows)Ends by itself
>>> 2 + 3
5
>>> print("Hello")
Hello
>>> 7 / 2
3.5
RememberThe REPL is your laboratory. Whenever you are unsure what a piece of Python does, type it into the REPL and look. Guessing in your head is slower and less reliable.

Try it

3. Your first script

# hello.py
print("Hello, world")
print("Learning Python")
C:\> python hello.py
Hello, world
Learning Python

Three things happened: you saved text in a file; you asked Python to run it; Python executed the two lines in order, top to bottom. print() is a function that writes text to the screen (to stdout, the standard output stream).

Lines starting with # are comments: Python ignores them, humans read them.

Try it

4. Arithmetic: Python as a calculator

OperatorMeaningExampleResult
+ - *Add, subtract, multiply6 * 742
/Division (always a float)7 / 23.5
//Floor division (round down)7 // 23
%Remainder (modulo)7 % 21
**Power2 ** 101024

Order of operations is the same as school maths: brackets, then **, then * / // %, then + -. 2 + 3 * 4 is 14, not 20; (2 + 3) * 4 is 20.

Floor division and remainder always rebuild the number: (a // b) * b + (a % b) == a7 // 2 = 37 % 2 = 1together: 2 x 3 + 1 = 7
A pair you will use constantly: how many whole times, and what is left over.

Try it

5. What can go wrong in the first five minutes

SymptomCause
'python' is not recognizedPython is not on your PATH; on Windows try py
SyntaxError: ...Python cannot understand the line (missing bracket, quote or colon)
Nothing printedYou forgot print() (the REPL shows values, scripts do not)
Output from an old version of your fileYou did not save it
NameErrorYou used a word Python does not know (typo, or defined later)

Try it

Check yourself

Think first, type an answer if you like, then reveal.