Fundamentals 4 min read

Master Python Assignment Operators: From Simple = to Compound Forms

This tutorial explains Python's simple and compound assignment operators, showing how = assigns values and how operators like +=, -=, *=, /=, %=, **=, and //= combine arithmetic with assignment, illustrated with concrete code examples and their output, while noting Python lacks ++/--.

Lisa Notes
Lisa Notes
Lisa Notes
Master Python Assignment Operators: From Simple = to Compound Forms

Python uses the = sign to assign the result of the right‑hand expression to the variable on the left, which differs from the mathematical equality sign.

Simple assignment example:

num = 13
print(num)  # 13

Compound assignment operators combine an arithmetic operation with assignment. The following operators are supported: += – addition assignment (e.g., c += a is equivalent to c = c + a) -= – subtraction assignment ( c -= ac = c - a) *= – multiplication assignment ( c *= ac = c * a) /= – division assignment ( c /= ac = c / a) %= – modulo assignment ( c %= ac = c % a) **= – exponentiation assignment ( c **= ac = c ** a) //= – floor‑division assignment ( c //= ac = c // a)

Illustrative code snippets demonstrate each operator and the resulting values:

num = 13
num += 21   # equivalent to num = num + 21
print(num)  # 34

num1 = 23
num1 -= 22  # equivalent to num1 = num1 - 22
print(num1) # 1

num2 = 6
num2 *= 3   # equivalent to num2 = num2 * 3
print(num2) # 18

num3 = 12
num3 /= 4   # equivalent to num3 = num3 / 4
print(num3) # 3.0

num4 = 21
num4 **= 2  # equivalent to num4 = num4 ** 2
print(num4) # 441

num5 = 3
num5 **= 2  # equivalent to num5 = num5 ** 2
print(num5) # 9

num6 = 54
num6 //= 7  # equivalent to num6 = num6 // 7
print(num6) # 7

The output sequence is 34 1 18 3.0 441 9 7. Finally, the article notes that Python does not provide the increment/decrement operators ++ or --; instead, one must use a += 1 or a -= 1 to achieve the same effect.

Pythoncode-exampleprogramming basicsassignment-operatorcompound-assignment
Lisa Notes
Written by

Lisa Notes

Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.