An assignment connects a name to the value produced by an expression. It is not the same thing as a permanent mathematical equation.
Understanding the Concept
Python evaluates the right-hand side first, then binds the name on the left. If the name already exists, the assignment rebinds it. The old object is not necessarily destroyed immediately because another reference may still point to it. Assignment is how you give meaningful names to results and reuse them on later lines. Python uses = for assignment and == for equality comparison.
When and Why You Would Use This
Trace an example where the new value depends on the old one. The right side uses the previous counter value, then Python updates the binding. This is valid programming syntax even though it would not describe an ordinary algebraic identity.
Practical Example
counter = 1
counter = counter + 1
counter = counter + 1
print(counter)
Understanding the Example
The counter becomes 2 and then 3. Each statement evaluates its right side using the current binding, then assigns the new result. No line changes the already executed earlier statements.
Example Output
3
Try It Yourself
Start counter at 10 and subtract 2 three times using separate assignments. Predict the final result.
Common Beginner Mistakes
- Reading counter = counter + 1 as an impossible equation rather than an assignment.
- Confusing equality checks with assignments.
- Trying to use a name on the right side before it has any value.
Key Takeaways
- Python evaluates the right-hand expression before rebinding the name.
- An assignment can replace a name’s previous binding.
- Use == when asking whether two values are equal.