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 ++/--.
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) # 13Compound 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 -= a → c = c - a) *= – multiplication assignment ( c *= a → c = c * a) /= – division assignment ( c /= a → c = c / a) %= – modulo assignment ( c %= a → c = c % a) **= – exponentiation assignment ( c **= a → c = c ** a) //= – floor‑division assignment ( c //= a → c = 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) # 7The 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Lisa Notes
Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
