Interpreters: Translating Code Line by Line
A compiler translates an entire program from high-level source code to machine language in one complete pass, then saves the result to an executable file for later execution.
Two Translation Workflows
When you write a program, the computer cannot directly execute the high-level instructions intended for human programmers. A translation process must connect your source code to machine language. A compiler translates the entire program in one complete pass and saves the result for later execution. An interpreter instead translates and executes source code line by line, allowing a Python programmer to see results as execution proceeds.
The key difference is when execution begins. With a compiler, you first provide the complete source file. The compiler reads the entire file, checks for errors, translates its statements into machine instructions, and produces a new executable file. With an interpreter, source code is processed sequentially: a line is translated and executed before the interpreter moves to the next line.
What a Compiler Produces
- 1Write the source code in a text editor and save it as a file.
- 2Run the compiler on the source file.
- 3The compiler reads the entire file, checks for errors, and translates every statement into machine instructions.
- 4The compiler creates a new file containing machine language.
- 5The operating system can later run the executable file.
The result of compilation contains low-level byte sequences designed for CPU execution rather than human reading. Loops, conditionals, and function calls from the original high-level program have already been translated into these machine-language sequences. Opening such a file in a text editor therefore produces symbols and control characters that are not meaningful as ordinary human-readable source code.
| Operating system | How an executable is identified | What the file contains |
|---|---|---|
| Windows | Typically a .exe or .dll suffix | Machine language instructions |
| Linux | File permissions mark it as runnable rather than a special suffix | Machine language instructions |
| macOS | File permissions mark it as runnable rather than a special suffix | Machine language instructions |
Executable identification differs by operating system, while the executable content is machine language.
Python Behind the Scenes
Python is used as an interpreted language: the Python interpreter reads Python source code and translates and executes it line by line. However, the interpreter itself is not written in Python. The Python interpreter is a compiled program written in C. Python developers used a C compiler to create that compiled program, and that program then reads and executes Python code.
This creates two layers of translation. First, a C compiler creates the Python interpreter as a compiled program. Later, when you launch Python, that compiled interpreter reads your Python source code and interprets it. Therefore, saying that Python is interpreted does not mean that every program involved was created without compilation.
Tracing Values Through Lines
A variable is a symbolic name that stores a value in memory, allowing a program to refer to data by name rather than by its literal value. During sequential execution, a line can use the results of earlier lines, but it cannot reference lines that come later.
total = 4
bonus = 3
total = total + bonus
print(total)- 1After line 1, total refers to 4.
- 2After line 2, total refers to 4 and bonus refers to 3.
- 3On line 3, the old value of total and the value of bonus are used to calculate 7; total then refers to 7.
- 4Line 4 displays the current value of total, so the output is 7.
- Line 1 assigns 4 to the symbolic name total.
- Line 2 assigns 3 to the symbolic name bonus.
- Line 3 retrieves both values, adds them, and assigns the result 7 to total.
- Line 4 retrieves total and displays its value.
7What do you think happens?
What does this program display?
points = 5
extra = 2
points = points + extra
print(points)Reveal answer
7
The interpreter processes the assignments in order. The third line retrieves 5 from points and 2 from extra, adds them, and stores 7 under points. The final line displays the current value of points.
Interactive Python Sessions
Python's interpreted execution model supports interactive programming. You can provide a command, have the interpreter translate and execute it, and see the result immediately. Each later command can build on values established by earlier commands. This is the practical effect of processing code sequentially: the interpreter works with the current state created by the lines that have already run.
12The final output is 12 because quantity is assigned 3, price is assigned 4, and cost is then assigned the result of using those earlier values. The print statement retrieves the current value of cost. If a later line attempted to use a name before an earlier line had assigned it, the sequential trace would not have that earlier value available.
Mistakes in Translation Models
Common mistake Assuming an interpreter translates the entire program before anything runs.
What it looks likeThinking that every line must be translated and completed before the first result can appear.
Why it failsAn interpreter translates and executes source code line by line, which makes immediate interactive results possible.
Do this insteadTrack the program sequentially: each line is processed using the results available from earlier lines.
Common mistake Assuming that Python has no compiled component.
What it looks likeSaying that Python source code is interpreted by a program that was also written in Python.
Why it failsThe Python interpreter itself is a compiled program written in C.
Do this insteadSeparate the two stages: a C compiler created the Python interpreter, and that interpreter later reads Python source code.
Common mistake Treating a variable name as if it were the value itself.
What it looks likeAfter total = total + bonus, continuing to think that total must still refer to its old value.
Why it failsThe assignment stores the calculated result under the name total, replacing the value that total referred to before.
Do this insteadUpdate your trace after every assignment and record the current value associated with each name.
Common mistake Expecting executable files to be identified the same way on every operating system.
What it looks likeAssuming that every runnable file must end with .exe.
Why it failsWindows typically uses .exe or .dll suffixes, while Linux and macOS use file permissions to mark files as runnable.
Do this insteadIdentify the operating system before applying a rule about executable files.
Practice the Trace
Trace this program from top to bottom. Write the value associated with each variable after every assignment, then predict the output.
first = 6
second = 1
result = first + second
first = result + 2
print(first)Hints
- Begin with first = 6 and second = 1.
- The third line uses the values already associated with first and second.
- The fourth line changes the value associated with first.
- The final line displays the current value of first, not its original value.
Tracing the practice program
Determine the output of the program and the variable values immediately before the print statement.
- 1First assignment
first refers to 6.
- 2Second assignment
second refers to 1.
- 3Calculate result
The interpreter retrieves 6 and 1, adds them, and assigns 7 to result.
- 4Update first
The interpreter retrieves result, adds 2, and assigns 9 to first. The earlier value 6 is no longer the current value associated with first.
- 5Display the value
print(first)retrieves the current value associated with first.
The program displays 9. Immediately before printing, first refers to 9, second refers to 1, and result refers to 7.
Key Takeaways
- 1A compiler translates an entire high-level program into machine language and saves the result in an executable file for later execution.
- 2Windows typically identifies executable files with .exe or .dll suffixes, while Linux and macOS use file permissions to mark files as runnable.
- 3The Python interpreter is itself a compiled program written in C.
- 4An interpreter translates and executes source code line by line, which supports Python's interactive execution model.
- 5Variables are symbolic names whose current values can be retrieved and updated as the interpreter processes a program sequentially.
At a glance
- Compilers process a complete program and produce machine-language executables before execution.
- Interpreters process source code line by line and execute each line as it is translated.
- Python source code is interpreted by a compiled interpreter written in C.
- Variables provide symbolic names for values, and assignments can update those names during sequential execution.
- To predict Python output, trace each line in order and update the current value of every variable.