Assignment Operations

Basic Assignment

  • Simple Assignment (=): Assigns a value to a variable.

    x = 10
    

Compound Assignment Operators

  1. Addition (+=): Adds and assigns.

    x = 5
    x += 3  # x = x + 3
    
  2. Subtraction (-=): Subtracts and assigns.

    x = 10
    x -= 4  # x = x - 4
    
  3. Multiplication (*=): Multiplies and assigns.

    x = 7
    x *= 2  # x = x * 2
    
  4. Division (/=): Divides and assigns.

    x = 20
    x /= 4  # x = x / 4
    
  5. Floor Division (//=): Floor divides and assigns.

    x = 17
    x //= 3  # x = x // 3
    
  6. Modulus (%=): Applies modulus and assigns.

    x = 14
    x %= 5  # x = x % 5
    
  7. Exponentiation (**=): Exponentiates and assigns.

    x = 2
    x **= 3  # x = x ** 3
    
  8. 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
    

Conclusion

Assignment operations simplify updating variables through arithmetic, bitwise, and compound operations, improving code conciseness and readability.