Assignment operators in Python are used to assign values to variables. They can also modify the value of a variable by combining assignment with arithmetic operations.
| Operator | Example | Meaning |
|---|---|---|
= |
x = 5 |
Assign value |
+= |
x += 3 |
x = x + 3 |
-= |
x -= 2 |
x = x - 2 |
*= |
x *= 2 |
x = x * 2 |
/= |
x /= 2 |
x = x / 2 |
//= |
x //= 2 |
x = x // 2 |
%= |
x %= 2 |
x = x % 2 |
**= |
x **= 2 |
x = x ** 2 |
=)Assigns a value to a variable.
x = 10
print(x)
Output
10
+=)x = 5
x += 3
print(x)
Output
8
-=)x = 10
x -= 4
print(x)
Output
6
*=)x = 6
x *= 2
print(x)
Output
12
/=)x = 10
x /= 2
print(x)
Output
5.0
//=)x = 10
x //= 3
print(x)
Output
3
%=)x = 10
x %= 3
print(x)
Output
1
**=)x = 2
x **= 3
print(x)
Output
8
Python allows assigning multiple variables at once.
a, b, c = 1, 2, 3
print(a, b, c)
x = y = z = 5
print(x, y, z)
total = 0
total += 10
total += 20
total += 30
print("Total:", total)
Output
Total: 60
+=, -=, etc.) make code short and efficient/= always returns a float valueAssignment operators are used to assign and update values
Types include:
=, +=, -=, *=, /=, //=, %= , **=Help in writing clean and concise code
Support multiple assignments