Assignment Operators

basic
Published

July 2, 2024

The Fundamental Assignment Operator: =

The most basic assignment operator is the equals sign (=). It assigns a value to a variable.

x = 10  # Assigns the integer value 10 to the variable x
name = "Python"  # Assigns the string "Python" to the variable 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.

x = 5
x += 3  # Equivalent to x = x + 3.  x now holds 8
y = 10
y -= 2  # Equivalent to y = y - 2. y now holds 8
z = 4
z *= 2  # Equivalent to z = z * 2. z now holds 8
a = 16
a /= 4 # Equivalent to a = a / 4. a now holds 4.0 (float division)
b = 15
b //= 4 # Equivalent to b = b // 4. b now holds 3 (integer division)
c = 10
c %= 3 # Equivalent to c = c % 3. c now holds 1 (modulo operation)

**=

This operator performs exponentiation and assigns the result.

x = 2
x **= 3  # Equivalent to x = x ** 3. x now holds 8

&=, |=, ^=

These bitwise operators perform a bitwise AND, OR, or XOR operation, respectively, and assign the result.

x = 10 #Binary: 1010
y = 4  #Binary: 0100

x &= 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)

<<= and >>=

These operators perform left and right bitwise shifts, respectively, and assign the result.

x = 10 #Binary: 1010
x <<= 2 # Left shift by 2 bits. x now holds 40 (Binary: 101000)
y = 40
y >>= 2 # Right shift by 2 bits. y now holds 10 (Binary: 1010)