Module 3 · Variables and Data Types — topic 2 of 15
Assignment
The assignment statement in detail: how = evaluates the right side first, chained assignment, augmented assignment such as +=, and the walrus operator := for assigning inside expressions.
Learning objectives
- Explain the evaluation order of assignment
- Use chained and augmented assignment
- Recognise the walrus operator
Definition
The assignment statement in detail: how = evaluates the right side first, chained assignment, augmented assignment such as +=, and the walrus operator := for assigning inside expressions.
Introduction
Assignment is how values get names. The basic form, name = expression, is the most common statement in Python, but there are a few variations that make everyday code shorter and clearer.
Why it matters
Counting, totalling and updating state all use assignment. Knowing the shortcuts such as += makes such code concise, and knowing the exact evaluation order prevents subtle bugs when a variable appears on both sides.
Detailed explanation
Python always evaluates the entire right-hand side first, using the current values of any names, and only then binds the result to the left-hand name. That is why x = x + 1 works.
Augmented assignment combines an operation with assignment: x += 1 means x = x + 1. The same pattern works for -=, *=, /=, //=, %= and **=.
Chained assignment gives several names the same value: a = b = 0. Use it only with immutable values such as numbers, because both names refer to the same object.
The walrus operator := assigns inside an expression, for example while (line := input()) != "quit". It is useful but optional; ordinary assignment is clearer in most beginner code.
Assignment never prints anything and has no value of its own, so it can’t be used where Python expects a value (except with :=).
Syntax
- Plain assignment binds one name.
- Augmented assignment updates a name using its current value.
- Chained assignment binds several names to one value.
- The walrus operator assigns and returns the value inside an expression.
Code examples
Right side first
15
Augmented assignment
240
More examples
Counters and the walrus operator
high: 2 low: 3 5 readings collected
Step-by-step explanation
- high = low = 0 starts two counters at zero in one line.
- For each reading, exactly one counter is increased with +=.
- The walrus operator stores len(readings) in count while also comparing it with 3.
- count can then be reused inside the if block without calling len() again.
Expected output
Running “Counters and the walrus operator” prints exactly:
Practical applications
- Web analytics code increments page-view counters with +=.
- Loops that read data until a stop value use := to read and test in one line.
- Shopping carts update a running total with += as items are added.
Best practices
- Use augmented assignment for counters and totals.
- Use chained assignment only for simple starting values.
- Prefer plain assignment over := unless it clearly simplifies the code.
Common mistakes
Confusing = and ==
Avoid
Prefer
= assigns, == compares. Using = in a condition is a SyntaxError.
Writing =+ instead of +=
Avoid
Prefer
=+ 5 is read as = (+5), which sets score to 5 instead of adding 5.
Key takeaways
- The right side of = is evaluated before the name is bound.
- x += n is shorthand for x = x + n, and similar for other operators.
- := assigns inside an expression.
Try it yourself
- 1Start a variable at 1 and double it five times with *=. Print the result.
- 2Count how many words in a list are longer than 5 letters using a counter and +=.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?