Concepts / Interpreters: Translating Code Line by Line

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.

  • Programming

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.

Source codeSource codeCompilerInterpreterExecutable fileMachine languageExecutionLine by lineExecutionentire programtranslateslater runsnext linetranslates and executes
What happens to source code, and when does execution begin, when code is given to a compiler versus an interpreter?

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

  1. 1
    Write the source code in a text editor and save it as a file.
  2. 2
    Run the compiler on the source file.
  3. 3
    The compiler reads the entire file, checks for errors, and translates every statement into machine instructions.
  4. 4
    The compiler creates a new file containing machine language.
  5. 5
    The 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 systemHow an executable is identifiedWhat the file contains
WindowsTypically a .exe or .dll suffixMachine language instructions
LinuxFile permissions mark it as runnable rather than a special suffixMachine language instructions
macOSFile permissions mark it as runnable rather than a special suffixMachine 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.

C source codePython source codeC compilerPython executionPython interpreterCompiled programtranslated bycreatesread bytranslates and executes
How is Python source code connected to the interpreter program, and where does the compiled C implementation fit?

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

Definition

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.

Code walkthrough
total = 4
bonus = 3
total = total + bonus
print(total)
  1. 1
    After line 1, total refers to 4.
  2. 2
    After line 2, total refers to 4 and bonus refers to 3.
  3. 3
    On line 3, the old value of total and the value of bonus are used to calculate 7; total then refers to 7.
  4. 4
    Line 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.
Output
7
total = 4total: 4bonus = 3bonus: 3total = total + bonustotal: 7print(total)output: 7next linenext linenext line
Which line executes next, what data changes after each line, and when is output produced?

What do you think happens?

What does this program display?

points = 5
extra = 2
points = points + extra
print(points)
  • 5

  • 2

  • 7

  • Nothing

Reveal answer
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.

points77extra22refers torefers to
What value does each variable name refer to, and how does an assignment change that mapping?

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.

python
Output
12

The 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 like

    Thinking that every line must be translated and completed before the first result can appear.

    Why it fails

    An interpreter translates and executes source code line by line, which makes immediate interactive results possible.

    Do this instead

    Track 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 like

    Saying that Python source code is interpreted by a program that was also written in Python.

    Why it fails

    The Python interpreter itself is a compiled program written in C.

    Do this instead

    Separate 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 like

    After total = total + bonus, continuing to think that total must still refer to its old value.

    Why it fails

    The assignment stores the calculated result under the name total, replacing the value that total referred to before.

    Do this instead

    Update 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 like

    Assuming that every runnable file must end with .exe.

    Why it fails

    Windows typically uses .exe or .dll suffixes, while Linux and macOS use file permissions to mark files as runnable.

    Do this instead

    Identify the operating system before applying a rule about executable files.

Practice the Trace

EASY

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.
Worked example

Tracing the practice program

Determine the output of the program and the variable values immediately before the print statement.

  1. 1
    First assignment

    first refers to 6.

  2. 2
    Second assignment

    second refers to 1.

  3. 3
    Calculate result

    The interpreter retrieves 6 and 1, adds them, and assigns 7 to result.

  4. 4
    Update 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.

  5. 5
    Display the value

    print(first) retrieves the current value associated with first.

Result

The program displays 9. Immediately before printing, first refers to 9, second refers to 1, and result refers to 7.

Key Takeaways

  1. 1
    A compiler translates an entire high-level program into machine language and saves the result in an executable file for later execution.
  2. 2
    Windows typically identifies executable files with .exe or .dll suffixes, while Linux and macOS use file permissions to mark files as runnable.
  3. 3
    The Python interpreter is itself a compiled program written in C.
  4. 4
    An interpreter translates and executes source code line by line, which supports Python's interactive execution model.
  5. 5
    Variables 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.

Related concepts

More concepts starting with I