Data Types and Values in Python
When you write a Python program, the interpreter processes the source code sequentially.
From Source to Result
When you write a Python program, the interpreter processes the source code sequentially. It translates and executes one line at a time, so the result of an earlier line can become part of what a later line does. This makes Python suitable for interactive programming: you can enter an instruction and see a result immediately.
The interpreter moves forward through the program. A line can use results that already exist, but it cannot use the result of a line that has not yet been processed.
Tracing a Small Program
The most useful way to understand sequential execution is to trace the program's state after every line. In the generated example below, the first line gives a value to a variable, the second line uses that value to produce another value, and the final line retrieves the later value.
first_value = 4
second_value = first_value + 3
print(second_value)- 1Line 1 Python assigns the value 4 to the symbolic name
first_value. - 2Line 2 Python processes the line after line 1, so
first_valueis available. The calculation produces a later value, which is associated withsecond_value. - 3Line 3 Python retrieves the value associated with
second_valueand produces the program's output.
- A value is assigned to
first_value. - The current value of
first_valueis used to determinesecond_value. - The value retrieved through
second_valueis sent to the output.
7Names, Values, and Retrieval
A variable is a symbolic name associated with a value. The name lets a program refer to data without repeating the literal value every time. Assignment establishes the association, and a later line can retrieve the value by using the name.
This relationship is important when working with different values, including contrasting examples such as numbers and text. The variable name provides a way to refer to the value, while the value itself is the data being handled. The interpreter must process the assignment before a later line can retrieve that value through its name.
Interactive Execution
Python's interpreter can be used interactively. Instead of preparing a complete multi-line program before observing anything, you can enter an instruction and see its result immediately. This immediate feedback reflects the same sequential model: the interpreter processes the instruction, updates what is available for later work, and presents the result.
What do you think happens?
Suppose you enter an instruction that establishes a variable and then enter a second instruction that uses that variable. What should happen when the second instruction is processed?
Reveal answer
The second instruction can use the value established by the first instruction because the interpreter has already processed the earlier instruction.
Interactive execution still follows sequential processing. Earlier results are available to later instructions, while instructions entered afterward cannot be used by an earlier one.
When using an interactive interpreter, treat each entered instruction as part of an ongoing sequence. Before using a variable, check that an earlier instruction has already associated that name with a value.
Mistakes in Execution Order
Common mistake Assuming Python can use a later line before it is processed.
What it looks likeTrying to rely on a value that is established only by a line appearing later in the program.
Why it failsThe interpreter processes code sequentially. A line can use results from previous lines, not from lines that come afterward.
Do this insteadTrace the program from the first line forward and confirm that each required value already exists before it is used.
Common mistake Treating a variable name as if it were the value itself.
What it looks likeThinking that a symbolic name and the data associated with that name are identical concepts.
Why it failsA variable is a symbolic name that allows the program to refer to a value.
Do this insteadSeparate the two questions: What name is being used, and what value is available through that name?
Common mistake Reading only the final line when predicting output.
What it looks likeLooking at the output instruction without tracing the assignments that occurred before it.
Why it failsThe final instruction depends on the results established by earlier instructions.
Do this insteadWrite down the relevant variable values after each line, then determine what the output instruction retrieves.
A program may contain several lines that refer to related values, but the interpreter still handles them in order. The presence of a name in the source code does not by itself make a later value available earlier in the program.
Trace It Yourself
Trace the following generated program line by line. Identify the value associated with each variable after each assignment, then predict the output before checking your reasoning.
Hints
- Start with the first assignment.
- Use the value established on the first line when processing the second line.
- The final instruction retrieves the value associated with the second variable.
8Key Takeaways
- 1Python uses an interpreter that translates and executes source code line by line.
- 2Sequential processing means that a line can use earlier results but cannot use results from a later line.
- 3Variables are symbolic names that allow programs to store and retrieve values.
- 4Tracing each line and recording the available values makes it possible to predict a program's output.
- 5Interactive execution provides immediate results while following the same sequential model.
At a glance
- The Python interpreter processes source code sequentially, translating and executing one line at a time.
- Earlier lines establish results that later lines can use.
- Variables connect symbolic names with values so programs can retrieve data by name.
- A reliable output prediction comes from tracing assignments and retrievals in order.
- Python's interactive model makes results visible immediately after an instruction is processed.