Assignment Operations
Basic Assignment
-
Simple Assignment (
=
): Assigns a value to a variable.x = 10
Compound Assignment Operators
-
Addition (
+=
): Adds and assigns.x = 5 x += 3 # x = x + 3
-
Subtraction (
-=
): Subtracts and assigns.x = 10 x -= 4 # x = x - 4
-
Multiplication (
*=
): Multiplies and assigns.x = 7 x *= 2 # x = x * 2
-
Division (
/=
): Divides and assigns.x = 20 x /= 4 # x = x / 4
-
Floor Division (
//=
): Floor divides and assigns.x = 17 x //= 3 # x = x // 3
-
Modulus (
%=
): Applies modulus and assigns.x = 14 x %= 5 # x = x % 5
-
Exponentiation (
**=
): Exponentiates and assigns.x = 2 x **= 3 # x = x ** 3
-
Bitwise Operations:
- AND (
&=
): Applies bitwise AND. - OR (
|=
): Applies bitwise OR. - XOR (
^=
): Applies bitwise XOR. - Left Shift (
<<=
): Shifts bits left. - Right Shift (
>>=
): Shifts bits right.
x = 8 x &= 3 # x = x & 3
- AND (
Conclusion
Assignment operations simplify updating variables through arithmetic, bitwise, and compound operations, improving code conciseness and readability.