Learn Python
>>> operators.op_assignment

Assignment Operators in Python

#concept

The = operator assigns a value to a name. Python also provides compound assignment operators that combine an operation with assignment in one step — += adds and reassigns, -= subtracts and reassigns, and the same pattern applies to every arithmetic operator. These aren't just shorthand; they make the intent "update this variable based on its own value" explicit.

#how it works
1

Plain assignment

name = value binds a name to a value.

2

+= is shorthand

name += value means name = name + value.

3

The same pattern covers every operator

-=, *=, /=, //=, %=, and **= all follow the identical shorthand pattern.

4

Works on any supporting type

Including string concatenation with += , since + already works on strings.

5

Multiple variables in one line

a, b = 1, 2 assigns both at once, matching values to names in order.

#example
score = 10
score += 5
print(score)
 
message = "Hello"
message += ", World!"
print(message)
 
a, b = 1, 2
print(a, b)
Output
15
Hello, World!
1 2
1score += 5 is shorthand for score = score + 5, updating score from 10 to 15.
2message += ", World!" does the same thing with string concatenation, since + works on strings too.
3a, b = 1, 2 assigns both variables in a single line — Python matches values to names in order.
#variants

Compound Arithmetic (+=, -=, *=, /=)

Shorthand for "update this variable based on its current value," using any arithmetic operator.

total = 100
total -= 25
print(total)
Output
75

Multiple Assignment

Assign several variables at once on one line, or the same value to several names.

x = y = z = 0
print(x, y, z)
Output
0 0 0
#use cases
Incrementing a counter inside a loop with count += 1, one of the most common patterns in any language
Building up a string or list across multiple steps, like a running log message
Resetting several related variables to the same starting value in one line
Making an update's intent explicit — total -= discount reads more clearly than total = total - discount

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.