Assignment Operators in Python
Assignment operators are used to assign values to variables. In addition to simple assignment (=), Python provides compound assignment operators that combine arithmetic or bitwise operations with assignment — helping to write more concise and readable code.
List of Assignment Operators
| Operator | Symbol | Description |
|---|---|---|
| Simple Assignment | = | Assigns the right-hand value to the left-hand variable |
| Add and Assign | += | Adds right operand to left operand and assigns the result |
| Subtract and Assign | -= | Subtracts right operand from left operand and assigns the result |
| Multiply and Assign | *= | Multiplies left operand by right operand and assigns the result |
| Divide and Assign | /= | Divides left operand by right operand and assigns the result |
| Modulus and Assign | %= | Performs modulus operation and assigns the result |
| Floor Divide and Assign | //= | Floor division and assign |
| Exponent and Assign | **= | Raises left operand to power of right operand and assigns result |
1. Simple Assignment (=)
Assigns the value of the right-hand side to the left-hand variable.
assignment_equal.py
x = 10
y = 5
print("x =", x)
print("y =", y)output.txt
x = 10
y = 5Explanation:
- The
=operator assigns the value on the right to the variable on the left. - It does not compare; it stores the value.
2. Add and Assign (+=)
Adds a value to the variable and updates the variable.
assignment_add.py
count = 5
count += 3
print("count =", count)output.txt
count = 8Example 2: String Concatenation with +=
assignment_add_string.py
greeting = "Hello"
greeting += " World"
print(greeting)output.txt
Hello WorldExample 3: Using in Loops
assignment_add_loop.py
total = 0
for i in range(1, 6):
total += i
print(total)output.txt
153. Subtract and Assign (-=)
Subtracts a value and updates the variable.
assignment_subtract.py
balance = 100
balance -= 20
print("balance =", balance)output.txt
balance = 80Example 2: Countdown
assignment_subtract_loop.py
countdown = 10
while countdown > 0:
countdown -= 1
print("Finished!")output.txt
Finished!Example 3: Negative Result
assignment_subtract_negative.py
num = 5
num -= 10
print(num)output.txt
-54. Multiply and Assign (*=)
Multiplies the variable by a value and updates it.
assignment_multiply.py
points = 10
points *= 2
print("points =", points)output.txt
points = 20Example 2: Geometric Growth
assignment_multiply_loop.py
x = 2
for i in range(3):
x *= 2
print(x)output.txt
16Example 3: String Repetition
assignment_multiply_string.py
stars = "*"
stars *= 5
print(stars)output.txt
*****5. Divide and Assign (/=)
Divides the variable by a value and updates it (result is float).
assignment_divide.py
price = 100
price /= 4
print("price =", price)output.txt
price = 25.0Example 2: Division Resulting in Float
assignment_divide_float.py
num = 9
num /= 2
print(num)output.txt
4.5Example 3: Multiple Divisions
assignment_divide_loop.py
value = 64
for _ in range(3):
value /= 2
print(value)output.txt
8.06. Modulus and Assign (%=)
Performs modulus (remainder) operation and updates variable.
assignment_modulus.py
number = 10
number %= 3
print(number)output.txt
1Example 2: Even or Odd
assignment_modulus_even_odd.py
n = 7
n %= 2
print(n)output.txt
1Example 3: Loop with Modulus
assignment_modulus_loop.py
total = 20
while total > 0:
total %= 7
break
print(total)output.txt
67. Floor Divide and Assign (//=)
Performs floor division and updates variable (integer result).
assignment_floor_divide.py
value = 25
value //= 4
print(value)output.txt
6Example 2: Reducing Value
assignment_floor_loop.py
x = 100
x //= 3
print(x)output.txt
33Example 3: Countdown to Zero
assignment_floor_zero.py
num = 10
num //= 5
print(num)output.txt
28. Exponent and Assign (**=)
Raises variable to a power and updates it.
assignment_exponent.py
base = 2
base **= 3
print(base)output.txt
8Example 2: Squaring a Number
assignment_exponent_square.py
num = 5
num **= 2
print(num)output.txt
25Example 3: Power Series
assignment_exponent_loop.py
value = 3
for _ in range(2):
value **= 2
print(value)output.txt
81Summary
- Assignment operators assign values to variables.
- Compound operators like
+=,-=,*=, and others make code shorter and more expressive. - They are commonly used in loops, counters, and aggregations.
- Mastering these operators improves both performance and clarity of your Python code.