Assignment Operators in Python: Complete Guide with Examples
All 8 Python assignment operators with code examples: = and the 7 augmented forms (+=, -=, *=, /=, %=, **=, //=), walrus :=, and placement test traps.
Python has 8 core assignment operators, and knowing when to reach for += versus //= catches more output-tracing MCQs than any amount of memorisation.
What assignment operators in Python do
Assignment operators store a value in a variable. The simplest form is =:
x = 10 # stores 10 in x
Python evaluates the right-hand side first, then binds the result to the left-hand variable. One distinction worth settling early: = is not equality. The double-equals == compares two values and returns True or False. Using = where you meant == inside an if statement raises a SyntaxError in Python (unlike C, where it compiles silently). That distinction appears repeatedly in aptitude MCQs.
Python also has 7 augmented assignment operators. Each combines a binary operation with assignment. Instead of x = x + 5, you write x += 5. According to the Python language reference, an augmented assignment evaluates the right-hand operand, applies the binary operation, and stores the result back into the left-hand target in a single step.
The 8 core assignment operators
| Operator | Operation | Equivalent to |
|---|---|---|
= | Simple assign | x = value |
+= | Add and assign | x = x + n |
-= | Subtract and assign | x = x - n |
*= | Multiply and assign | x = x * n |
/= | Divide (float result) and assign | x = x / n |
%= | Modulo and assign | x = x % n |
**= | Power and assign | x = x ** n |
//= | Floor-divide and assign | x = x // n |
Each operator applied in sequence, with the value carried forward from one line to the next:
x = 20 # x = 20
x += 5 # x = 25
x -= 3 # x = 22
x *= 4 # x = 88
x /= 8 # x = 11.0 (/ always returns float in Python 3)
x %= 4 # x = 3.0 (11.0 mod 4 = 3.0)
x = 3 # reset to integer
x **= 3 # x = 27 (3 to the power of 3)
x //= 5 # x = 5 (27 floor-divided by 5)
print(x) # 5
One detail that catches students on //=: floor division rounds towards negative infinity, not towards zero. So for x = -7 followed by x //= 2, the result is -4 (not -3). Plain upward division rounds toward zero; floor division does not.
+= also works on non-numeric types. For strings, it concatenates; for lists, it extends in place:
label = "FACE"
label += " Prep"
print(label) # FACE Prep
scores = [85, 90]
scores += [78, 92]
print(scores) # [85, 90, 78, 92]
The **= operator is central to the Armstrong number check, where each digit is raised to the power of the total digit count of the number.
Chained assignment and right-to-left associativity
Python evaluates chained assignment from right to left:
x = y = z = 10
print(x, y, z) # 10 10 10
Python first binds 10 to z, then z’s value to y, then y’s value to x. All three names end up pointing to the same integer object.
The same right-to-left evaluation logic powers the two-variable swap idiom:
a, b = b, a
Python evaluates the right-hand side (b, a) as a complete tuple before any assignment happens. No temporary variable is required. This is a clean Python pattern, not a special syntax rule.
One constraint: chaining augmented operators is not valid Python syntax. x += y -= z raises a SyntaxError. Each augmented operator must stand on its own statement.
The += pattern inside a loop is the most common form in placement coding rounds. A sum of array elements problem reduces to:
total = 0
for num in arr:
total += num
That two-line accumulator is the idiom. Knowing it cold saves time in timed assessments.
The walrus operator (:=)
Python 3.8 introduced the walrus operator := via PEP 572. Unlike =, which is a statement, := is an expression: it assigns a value and also returns it. This matters when you want to test a value and use it in the same line without calling a function twice:
import random
while (n := random.randint(1, 10)) != 7:
print(f"Got {n}, trying again")
print("Reached 7")
Without :=, the standard pattern requires either calling random.randint twice or pulling the call into a separate line before the while test. The walrus form is more concise for streaming data and while loops.
Two practical notes for placement preparation:
:=requires Python 3.8 or later. Some online judge environments at older firms still default to Python 3.6, where:=will raise aSyntaxError.- In placement MCQs as of 2026,
:=appears mainly in two question types: “spot the syntax error” problems and output-tracing questions for product-company assessments targeting students who claim Python proficiency.
Where these appear in placement tests
Output-tracing MCQs
These questions show a short code block and ask for the final printed value. The recurring traps:
//=with a negative operand:x = -7thenx //= 2gives-4, not-3(floor rounds toward negative infinity)%=with a float operand:x = 5.0thenx %= 4gives1.0, not1(the type is preserved)- Chained
=on mutable objects: two names pointing at the same list can produce unexpected side effects when one is modified
Coding-round accumulation patterns
TCS NQT, AMCAT, and eLitmus coding sections include problems that sum, count, or multiply elements across a sequence. The += and *= operators are the workhorses for these. A simple calculator in Python is one of the canonical drills that exercises all four arithmetic assignment operators under a timed constraint. Python practice programs in the FACE Prep library are grouped by pattern type, so you can target += and -= accumulation problems specifically.
The += accumulator loop that computes a running total is also the structural seed of gradient-descent weight updates in machine learning: weight -= learning_rate * gradient is the same update pattern, applied millions of times. TinkerLLM at ₹299 lets you trace that connection from the loop variable in today’s placement prep to the model parameter in a working LLM fine-tune, in one session.
Primary sources
Frequently asked questions
What is the difference between = and == in Python?
= is the assignment operator that stores a value in a variable (x = 10). == is the equality comparison operator that returns True or False. Using = inside an if-condition is a SyntaxError in Python — a frequent trap in output-tracing MCQs.
Can += be used with strings and lists in Python?
Yes. s += ' World' concatenates strings, and lst += [4, 5] extends a list in place. The behaviour mirrors the __iadd__ method of each object type, so the result is identical to the + operator followed by reassignment.
What is the walrus operator in Python?
The walrus operator := (introduced in Python 3.8 via PEP 572) assigns a value to a variable inside an expression. It is useful in while loops and list comprehensions to avoid calling the same function twice on the same data.
Is x += 1 exactly the same as x = x + 1?
For integers and floats, the result is identical. For mutable objects like lists, += calls __iadd__ and modifies the object in place, while x = x + [item] creates a new list object. The difference matters when two variables point to the same list.
What does //= do in Python, and how does it handle negative numbers?
//= performs floor division and assigns the result. Floor division rounds towards negative infinity, not towards zero. So x = -7 followed by x //= 2 gives -4, not -3. This direction-of-rounding distinction is the most common //= trap in placement aptitude MCQs.
Which assignment operators appear in placement aptitude tests?
Output-tracing questions for +=, -=, *=, and //= appear in TCS NQT, AMCAT, and eLitmus aptitude sections. Common traps are the rounding direction of //= and the float remainder produced by %= when one operand is a decimal.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹299 launch)