A computer's processor only understands machine code: long sequences of binary instructions specific to that processor. People do not write that directly. Instead you write source code in a high-level programming language such as Python, and a program translates it into something the machine can execute. The PCEP (Certified Entry-Level Python Programmer) exam expects you to know the vocabulary for how that translation happens and for the rules any language imposes, because these words appear in questions long before you write a line of code.
There are two classic translation strategies. Compilation translates the whole program ahead of time into a separate executable file of machine code, which can then be run many times without the compiler. Interpretation reads the source code and executes it statement by statement each time the program runs, so the interpreter must always be present. Compiled programs tend to run faster and can be shipped without their source; interpreted programs are quicker to test and easier to move between platforms, because the same source runs anywhere an interpreter exists. Python is treated as an interpreted language: you run python3 script.py and the interpreter executes it. Internally, CPython first converts source into bytecode for its own virtual machine, but for the exam, Python is interpreted.
Every language, human or computer, has layers of rules. An alphabet is the set of symbols you may use. Lexis is the vocabulary: the set of valid words and symbols, such as keywords, operators, names and literals. Syntax is the grammar: the rules for how those words may be combined into valid statements. Semantics is meaning: whether a statement that is well formed actually makes sense. For example, print("hi" is a syntax error because a parenthesis is missing, while print(10 / 0) is grammatically fine but fails when run because dividing by zero has no meaning.
The interpreter finds problems at different times, and the exam tests the difference. Before running anything, Python reads (parses) the whole file; a syntax error anywhere stops the program before even the first line executes, and the message shows the line with a caret under the problem. Run-time errors, called exceptions, appear only when the faulty line is actually reached, so earlier lines may already have produced output. Logic errors are the hardest: the program runs without complaint but gives the wrong answer, and no tool will flag them for you.
CPython is the reference implementation of Python, written in the C language, and it is what you download from the official Python site. You can use it in two modes. Interactive mode, also called the REPL (read-eval-print loop), shows the >>> prompt and evaluates each line immediately, which is ideal for experiments. Script mode runs a saved .py file from start to finish. Source files are plain text, so any editor works, and IDLE (Integrated Development and Learning Environment), which ships with Python, gives you both an editor and a shell.
# save as demo.py and run: python3 demo.py
print("start") # runs first
print(len("abc")) # 3
print(10 / 0) # ZeroDivisionError here, at run time
print("never shown")
Consider a worked example. You save a file with three print() calls, but the third is missing its closing parenthesis. When you run it, nothing is printed at all: the interpreter reports a SyntaxError before executing anything, because parsing the whole file comes first. You fix the bracket, but now the second line is print(1 / 0). This time the first line prints, then a ZeroDivisionError stops the program, so the third line never runs. Finally you fix that, but you wrote area = width + height where you meant multiplication; everything runs and prints a number, yet it is wrong. That is a logic error.
Common mistakes: thinking Python is compiled because it creates .pyc bytecode files; assuming a syntax error on line 10 lets lines 1 to 9 run first (it does not); and confusing lexis with syntax. A misspelled name such as prnt is lexically valid (it is a legal name), parses fine, and only fails at run time with NameError. Exam questions use clue words: "translated once into an executable" means compiler; "executed line by line each time" means interpreter; "vocabulary" means lexis; "grammar" or "structure" means syntax; "meaning" means semantics; "found before execution" means a syntax error.
Key terms
- Source code
- The human-readable program text you write, stored for Python in a plain-text .py file.
- Compiler
- A program that translates the whole source code into machine code ahead of time, producing a separate executable.
- Interpreter
- A program that reads and executes source code each time it runs; CPython is Python's reference interpreter.
- Lexis
- The vocabulary of a language: the valid words and symbols it recognises.
- Syntax
- The grammar rules for combining lexical elements into valid statements.
- Semantics
- The rules that decide whether a well-formed statement makes sense and what it means.
- REPL
- The interactive read-eval-print loop, shown by the >>> prompt, that runs each line as you type it.
You save a file with three print() calls, but the third is missing its closing parenthesis. When you run it, nothing is printed at all: the interpreter reports a SyntaxError before executing anything. Fix that, but make the second line print(1 / 0), and now the first line prints, then a ZeroDivisionError stops the program before the third line.
Check yourself
What is the key practical difference between compiling and interpreting?
A compiler translates the whole program once into an executable that runs without the compiler; an interpreter translates and runs the source every time, so the interpreter must be present.
Is print("a" + 5) a syntax error or a run-time error?
A run-time error (TypeError). The line is grammatically valid, but adding a string and an integer has no meaning, so it fails only when executed.
Which is violated by writing prnt instead of print: lexis, syntax or semantics?
Not lexis or syntax: prnt is a valid name and the line parses. It fails at run time with NameError because the name is not defined, which is a semantic problem.
A file has a syntax error on its last line. How many earlier lines run?
None. Python parses the whole file before executing it, so the SyntaxError is reported before any line runs.