A line of Python is not meaningful until it changes the state of a running program or produces some result.
Understanding the Concept
Think of program execution as a sequence of evaluations. An expression such as 4 + 6 produces a value. An assignment binds that result to a name. A later expression can read it and calculate something new. Calls such as print() create observable output. The interpreter keeps track of objects and names while the program runs; it does not replace every variable in your source text with a number.
When and Why You Would Use This
Trace a small example line by line. After the first assignment, subtotal refers to 40. After computing the fee, the new value is 4. The final value includes both. This mental tracing technique will become essential for loops and functions later.
Practical Example
subtotal = 40
fee = subtotal * 0.10
total = subtotal + fee
print("Total:", total)
Understanding the Example
The program displays Total: 44.0. Python evaluates the right side of each assignment and then associates the result with the name on the left. No user interaction is needed because all input values were defined in the file.
Example Output
Total: 44.0
Try It Yourself
Write the value of each variable after every line without running the script. Then confirm it in Python.
Common Beginner Mistakes
- Assuming the result of an expression is automatically displayed when a .py script runs. Use print() for console output.
- Reading an assignment as mathematical equality. It gives a name a value; the value may change later.
- Ignoring execution order. A name must be assigned before it is read in this straightforward example.
Key Takeaways
- The interpreter evaluates expressions and executes statements in order.
- Assignments bind names to values for later use.
- Tracing intermediate values helps explain and debug program behavior.