The Fundamental Assignment Operator: =
The most basic assignment operator is the equals sign (=
). It assigns a value to a variable.
= 10 # Assigns the integer value 10 to the variable x
x = "Python" # Assigns the string "Python" to the variable name name
Compound Assignment Operators: Efficiency and Readability
Python offers compound assignment operators that combine an arithmetic operation with an assignment. These operators shorten your code and make it more readable.
+=
, -=
, *=
, /=
, //=
, %=
These operators perform the specified arithmetic operation and then assign the result back to the original variable.
= 5
x += 3 # Equivalent to x = x + 3. x now holds 8
x = 10
y -= 2 # Equivalent to y = y - 2. y now holds 8
y = 4
z *= 2 # Equivalent to z = z * 2. z now holds 8
z = 16
a /= 4 # Equivalent to a = a / 4. a now holds 4.0 (float division)
a = 15
b //= 4 # Equivalent to b = b // 4. b now holds 3 (integer division)
b = 10
c %= 3 # Equivalent to c = c % 3. c now holds 1 (modulo operation) c
**=
This operator performs exponentiation and assigns the result.
= 2
x **= 3 # Equivalent to x = x ** 3. x now holds 8 x
&=
, |=
, ^=
These bitwise operators perform a bitwise AND, OR, or XOR operation, respectively, and assign the result.
= 10 #Binary: 1010
x = 4 #Binary: 0100
y
&= y # Bitwise AND. x now holds 0 (Binary: 0000)
x = 10
x |= y # Bitwise OR. x now holds 14 (Binary: 1110)
x = 10
x ^= y # Bitwise XOR. x now holds 14 (Binary: 1110) x
<<=
and >>=
These operators perform left and right bitwise shifts, respectively, and assign the result.
= 10 #Binary: 1010
x <<= 2 # Left shift by 2 bits. x now holds 40 (Binary: 101000)
x = 40
y >>= 2 # Right shift by 2 bits. y now holds 10 (Binary: 1010) y