Compilers: Translating Code Before Execution
The key distinction in this topic is when translation and execution occur.
From Source to Result
When you write a Python statement, Python does not wait for an entire long program before showing every result. An interpreter translates and executes source code line by line. This makes Python suitable for interactive programming, where you can enter a statement and see its result immediately.
A Program in Motion
Consider a program as a sequence of instructions. The interpreter processes the first line, then the second line, and continues in order. Each line can use results produced by earlier lines. It cannot use a result from a line that comes later, because that later line has not yet been processed.
total = 4
bonus = 3
grand_total = total + bonus
print(grand_total)- 1The interpreter processes total = 4. The name total is associated with the value 4.
- 2It processes bonus = 3. The name bonus is associated with the value 3.
- 3It processes
grand_total= total + bonus. The earlier values are retrieved and combined, producing 7. - 4It processes
print(grand_total), which displays 7.
- Line 1 creates a value associated with total.
- Line 2 creates a value associated with bonus.
- Line 3 uses the results of the first two lines.
- Line 4 displays the resulting value.
7Interpreters and Compilers
The key distinction in this topic is when translation and execution occur. An interpreter translates and executes source code line by line. A compiler translates code before execution. Python is described here as an interpreter, so its execution model lets you work with statements interactively and see results immediately.
| Interpreter | Compiler |
|---|---|
| Translates and executes source code line by line | Translates code before execution |
| Supports interactive work with immediate results | The provided material does not describe an interactive execution model |
| Python is described as an interpreter | The source pack provides the before-execution translation distinction |
The comparison is limited to the translation and execution distinction described in the source material.
Names and Stored Values
A variable is a symbolic name that stores a value in memory. It lets a program refer to data by name rather than by repeating the literal value. When a later line uses the variable name, the program retrieves the value associated with that name.
The name score allows the later expression to retrieve 10 without writing the literal value again. This pattern is fundamental because programs can build new computations from values established by earlier lines.
Interactive Python
Python's interactive execution model follows the same line-by-line idea. You enter a Python statement, the interpreter translates and executes that statement, and you can see the result immediately. The result can then help you decide what statement to enter next.
Mistakes in Execution Order
Common mistake Trying to use a variable before its line has been processed
What it looks likeprint(final_value) final_value = 8Why it failsThe first line comes before the line that establishes
final_value, so the earlier line cannot use that later result.Do this insteadPlace final_value = 8 before print(final_value).Common mistake Treating a variable name as if it were the literal value itself
What it looks likepoints = 6 extra = points + 2Why it failsThe name points must be understood as a reference to the stored value 6. It is not the text of the value; it is the symbolic name used to retrieve it.
Do this insteadTrace the name to its stored value before evaluating the expression.
Common mistake Skipping earlier lines when predicting output
What it looks likebase = 5 increase = 4 total = base + increase print(total)Why it failsThe final line depends on values produced by the earlier assignment lines.
Do this insteadRecord each variable and its value in execution order.
When tracing a program, read from the first line downward. After each assignment, write down the name and the value it now represents. When a later expression uses that name, substitute the stored value mentally before determining the next result.
Practice the Trace
What do you think happens?
What output does this program produce?
first = 2
second = 5
combined = first + second
print(combined)Reveal answer
7
The interpreter processes the assignments in order. first represents 2, second represents 5, and combined is calculated from those earlier values, producing 7. The final line displays that result.
Trace this program without running it. Write the value associated with each name after every assignment, then predict the displayed output.
starting = 3
change = 6
ending = starting + change
print(ending)Hints
- Process the assignments from top to bottom.
- The expression for ending uses the values associated with starting and change.
- The final line displays ending.
Key Takeaways
- 1An interpreter translates and executes source code line by line.
- 2Python's interactive model makes it possible to see results immediately after entering statements.
- 3Variables are symbolic names that store values and let later code retrieve those values by name.
- 4Sequential execution means a line can use earlier results but cannot use results from later lines.
- 5To predict output, trace every line in order and track the values associated with each variable.
At a glance
- An interpreter translates and executes code one line at a time.
- Python supports interactive programming because results can be seen immediately.
- Variables provide symbolic names for stored values.
- Execution is sequential: later lines can use earlier results, but earlier lines cannot use later results.
- A reliable output trace records each variable's value as the interpreter moves through the program.