Skip to content

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.

Beginner 12 min read·Lesson 30 of 306 in Python Programming

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

Python
name = expression
name += expression
a = b = expression
if (name := expression) > limit:
pass
  • 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

Python
count = 7
count = count * 2 + 1
print(count)
Output
15

Augmented assignment

Python
total = 100
total += 50
total -= 30
total *= 2
print(total)
Output
240

More examples

Counters and the walrus operator

Python
readings = [12, 0, 31, 7, 45]
high = low = 0
for r in readings:
if r >= 30:
high += 1
else:
low += 1
print("high:", high, "low:", low)
if (count := len(readings)) > 3:
print(f"{count} readings collected")
Output
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:

TextOutput
high: 2 low: 3
5 readings collected

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

if total = 100:

Prefer

if total == 100:

= assigns, == compares. Using = in a condition is a SyntaxError.

Writing =+ instead of +=

Avoid

score =+ 5

Prefer

score += 5

=+ 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

  1. 1
    Start a variable at 1 and double it five times with *=. Print the result.
  2. 2
    Count 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?
Practise this topic