Two kinds of translator#
Imagine a book written in Spanish that needs to reach an English reader. There are two ways to do it.
- A book translator takes the whole manuscript away, translates every page, and hands back a finished English book. If one page is garbled, the book is not handed over at all until it is fixed. After that, the reader can read it as often as they like without the translator.
- A live interpreter at a meeting listens to one sentence, says it in English, then moves to the next. The audience hears the start of the speech before the end has even been translated. If one sentence is gibberish, the meeting has already heard everything before it.
Computers need the same service. Your computer’s processor understands only one language, and it is not Python. A compiler is the book translator. An interpreter is the live one. That is most of what an exam means by those two words.
Machine code, low-level and high-level#
The processor (the CPU, the chip that does the actual work) runs machine code: instructions written as binary numbers, specific to that kind of chip. Nobody writes programs in it by hand any more.
Programming languages are described by how close they sit to that machine code:
| Low-level language | High-level language | |
|---|---|---|
| Examples | machine code, assembly language | Python, Java, C++, JavaScript |
| Close to | the hardware | human language and maths |
| One line does | one tiny step, such as “copy this number into a register” | a lot: print(sum(scores)) |
| Runs on | one kind of processor | any computer that has the right translator (it is portable) |
| Easy to read, write and fix? | no | yes |
A register is a tiny storage slot inside the CPU. Assembly language is machine code with short word names (like ADD or MOV) in place of the binary numbers, one instruction per line. The trade-off is simple: high-level languages are far easier for people, at the cost of needing a translator before the processor can run them.
Compiler versus interpreter#
| Compiler | Interpreter | |
|---|---|---|
| Translates | the whole program, before it runs | one statement at a time, while it runs |
| Produces | a separate file you can run later (an executable) | no separate file: it runs the program directly |
| Errors reported | grammar (syntax) errors all at once, before anything runs; other errors can still happen while it runs | when the line with the problem is reached |
| Speed of the finished program | usually faster | usually slower |
| Classic examples | C, C++ | Python, JavaScript |
Python is described as an interpreted language, and for a first exam that is the right answer. The fuller truth is that the standard Python you install (called CPython) works in two steps. First it reads your whole file and translates it into bytecode, a simple intermediate code. Then the Python interpreter runs that bytecode one instruction at a time. You never see the bytecode; it happens in a blink each time you press Run. (Java works in a similar two-step way, but there the two steps are separate commands, javac and java.)
That two-step process produces two very different kinds of failure, and you can see both.
Two kinds of stop you can see#
A mistake in the grammar of the code, a syntax error, is found in the first step, while Python reads the whole file. So nothing runs, not even the lines above the mistake:
print("Line 1 runs")
print("Line 2 runs")
print("Line 3 has a missing bracket"
print("Line 4")
File "report.py", line 3
print("Line 3 has a missing bracket"
^
SyntaxError: '(' was never closed
Lines 1 and 2 are perfectly fine, yet they never printed. The bracket left open on line 3 meant Python could not finish reading the file, so it never started running it. This is the book translator refusing to hand over the book.
A mistake that only shows up while the program runs, a runtime error, waits until its line is reached:
print("Line 1 runs")
print("Line 2 runs")
print(scroe)
print("Line 4")
Line 1 runs
Line 2 runs
Traceback (most recent call last):
File "report.py", line 3, in <module>
print(scroe)
^^^^^
NameError: name 'scroe' is not defined
This time the first two lines ran and printed. Line 3 is grammatically fine, so the file was read without complaint. Only when the interpreter reached line 3 and looked for a variable called scroe did the program stop. That is the live interpreter: the audience already heard the first two sentences.
A third kind, a logic error (or semantic error), never stops anything: the program runs to the end and prints the wrong answer. The errors guide covers all three in depth.
The words that get tested#
| Word | Plain meaning |
|---|---|
| program | a set of instructions a computer follows |
| source code | the program as you typed it, in a language people read (your .py file) |
| algorithm | a step-by-step method for solving a problem, written in any form: words, a flowchart, or code |
| syntax | the grammar rules of a language: where brackets, colons and quotes go |
| semantics | what the code means, what it actually does when run |
| statement | one complete instruction, such as x = 5 or print(x) |
| expression | a piece of code that produces a value, such as x + 1 or len(name) |
| bug / debugging | a mistake in a program / finding and fixing it |
| shell (interactive mode) | the >>> prompt, where each line runs as soon as you press Enter and values are shown automatically |
| script (script mode) | a saved .py file run as a whole; only what you print appears |
| IDE | Integrated Development Environment: the editor you write and run code in, such as IDLE, Thonny or VS Code |
The difference between the shell and a script trips people up early. Typing 2 + 3 at the >>> prompt shows 5, because the shell displays every expression’s value. In a script, the same line calculates 5 and throws it away:
2 + 3
print(2 + 3)
5
Only one 5 appeared. The bare expression on line 1 was evaluated and discarded. In a script, if you want to see a value, print it.
Computational thinking#
Computational thinking is the name for the habits of mind behind writing a program, before any code exists. Courses usually list four:
- Decomposition: breaking a big problem into smaller ones. “Work out my grade” becomes: get the scores, add them up, divide, pick the letter.
- Pattern recognition: noticing what repeats. Every score is handled the same way, which is a hint that a loop will do the work.
- Abstraction: keeping the details that matter and ignoring the rest. The grade depends on the numbers, not on the course’s name or the colour of the paper.
- Algorithm design: writing the steps in order, precisely enough that someone (or something) with no common sense could follow them.
scores = [88, 92, 79, 95]
total = 0
for s in scores:
total = total + s
average = total / len(scores)
if average >= 90:
letter = "A"
elif average >= 80:
letter = "B"
else:
letter = "C or below"
print("Average:", average)
print("Grade:", letter)
Average: 88.5
Grade: B
Each of the four habits is visible in that short program: separate steps, one rule applied to every score, only the numbers kept, and a precise order. The flowcharts guide shows how to plan those steps on paper before typing anything.
A compiler translates the whole program before it runs; an interpreter runs it one statement at a time. Python reads the whole file first, so a syntax error stops everything before line 1, while a runtime error only stops the program when its line is reached.
Where these go wrong#
- Calling Python “compiled” on an exam. The expected answer is interpreted. The bytecode step is real, but it is not what the question is about.
- Mixing up high-level and low-level. High-level means close to human language and far from the hardware. Python is high-level.
- Expecting the lines above a syntax error to run. They do not: Python must read the whole file first.
- Expecting a bare expression to show its value in a script. Only the shell displays values automatically. Use
print. - Saying syntax and semantics are the same thing. Syntax is the grammar; semantics is the meaning. Code can be perfectly grammatical and still do the wrong thing.
- Defining algorithm as “code”. An algorithm is the method. Code is one way to write it down; so are a flowchart and pseudocode.
Practice#
Answer each one on paper before you look at the solutions. Every answer that involves running code was checked by running it.
- Put these in order from lowest level to highest level: Python, machine code, assembly language.
- Give two differences between a compiler and an interpreter.
- What does this program print, and why?
total = 10 print("Start") total = total + "5" print("Total is", total) - A file has a missing closing quote on its last line. Lines 1 to 9 are correct and each prints something. How many lines of output appear before the error message?
- Explain the difference between syntax and semantics, with an example of code whose syntax is fine but whose semantics are wrong.
- Name the four parts of computational thinking and give a one-line example of each for “plan a road trip”.
Solutions#
- Machine code (binary instructions the CPU runs), then assembly language (the same instructions with short word names), then Python (high-level, close to human language).
- Any two of: a compiler translates the whole program before it runs, an interpreter translates and runs one statement at a time; a compiler produces a separate executable file, an interpreter does not; a compiler reports errors before anything runs, an interpreter reports them when the line is reached; compiled programs usually run faster.
- It prints
Start, then stops with aTypeErroron line 3, because a number and a string (text) cannot be added. Line 3 is grammatically correct, so the file is read and runs normally until that line is reached; the last line never runs.total = 10 print("Start") total = total + "5" print("Total is", total)Start Traceback (most recent call last): File "quiz.py", line 3, in <module> total = total + "5" ~~~~~~^~~~~ TypeError: unsupported operand type(s) for +: 'int' and 'str' - None. A missing quote is a syntax error, found while Python reads the whole file, before it runs any of it. Only the
SyntaxErrormessage appears. - Syntax is the grammar of the code; semantics is what it means and does.
average = total / 2in a program that has three scores is perfectly grammatical, but it divides by the wrong number, so the meaning, and the answer, are wrong. That is a logic error. - Decomposition: split it into route, fuel, food and hotels. Pattern recognition: every fuel stop is the same task, so plan one and repeat it. Abstraction: keep distances and times, ignore the colour of the car. Algorithm design: write the steps in order, from leaving home to arriving.
Test-readiness checklist#
These are the skills a first programming course tests on this topic. Tick each one only when you can do it without looking.
- ☐ I can define machine code, low-level language and high-level language, and place Python on that scale.
- ☐ I can give the differences between a compiler and an interpreter, and say which one Python is usually called.
- ☐ I can predict whether lines above an error run, for a syntax error and for a runtime error.
- ☐ I can define program, source code, algorithm, syntax, semantics, statement, expression, bug and debugging.
- ☐ I can explain shell (interactive) mode versus script mode.
- ☐ I can name and explain the four parts of computational thinking.
Test yourself in the free Kestrel Exams app
12 practice questions on interpreters, compilers, high- and low-level languages, and when Python reports each kind of error. Free, offline, no account.
Practise how programs run →Frequently asked questions#
What is the difference between a compiler and an interpreter?
A compiler translates a whole program into machine code before it runs, producing a separate executable and reporting errors up front. An interpreter translates and runs a program one statement at a time, so errors appear only when their line is reached. Compiled programs usually run faster; interpreted ones are quicker to test and change.
Is Python compiled or interpreted?
Python is usually described as an interpreted language. The standard implementation, CPython, first compiles the whole file to bytecode and then interprets that bytecode one instruction at a time, which is why a syntax error anywhere stops the program before any line runs.
What is the difference between a high-level and a low-level language?
A low-level language, such as machine code or assembly, is close to the hardware: each instruction does one tiny step and works only on one kind of processor. A high-level language, such as Python or Java, is close to human language, does much more per line, and runs on any computer with the right translator.
What is machine code?
Machine code is the set of binary instructions a computer's processor can execute directly. A program in a high-level language cannot run on the processor as it is: either a compiler translates it into machine code first, or an interpreter (itself a machine-code program) reads it and carries out each step.
What is the difference between syntax and semantics?
Syntax is the set of grammar rules for writing code, such as where colons and brackets go. Semantics is the meaning of the code, what it actually does when it runs. A syntax error stops a program from running; a semantic or logic error lets it run but produce the wrong result.
What is computational thinking?
A problem-solving approach used before writing code, usually described as four skills: decomposition (breaking a problem into parts), pattern recognition (spotting what repeats), abstraction (keeping only the important details), and algorithm design (writing precise steps in order).
Something here not clear? A topic you wish we covered? Tell us. We read every message, and a request is the fastest way to get a guide written — several of these exist because somebody asked.