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 = 10print(x)Output
10+=)x = 5x += 3print(x)Output
8-=)x = 10x -= 4print(x)Output
6*=)x = 6x *= 2print(x)Output
12/=)x = 10x /= 2print(x)Output
5.0//=)x = 10x //= 3print(x)Output
3%=)x = 10x %= 3print(x)Output
1**=)x = 2x **= 3print(x)Output
8Python allows assigning multiple variables at once.
a, b, c = 1, 2, 3print(a, b, c)x = y = z = 5print(x, y, z)total = 0 total += 10total += 20total += 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