Python Mastery – From Basics to Brilliance / Operators in Python

What Are Assignment Operators?

Assignment operators are used to store values in variables.

You can also use them to update the value of a variable in a shorter way — this is called compound assignment.


Operator

Example

What It Means

=

x = 5

Set x to 5

+=

x += 3

Add 3 to x → x = x + 3

-=

x -= 2

Subtract 2 from x → x = x - 2

*=

x *= 4

Multiply x by 4 → x = x * 4

/=

x /= 2

Divide x by 2 → x = x / 2

%=

x %= 3

Remainder of x ÷ 3 → x = x % 3

//=

x //= 3

Rounds the result down to the nearest whole number x = x // 3

**=

x **= 3

Raises a variable to the power of a specified value x = x ** 3

&=

x &= 3

Performs a bitwise AND and updates the variable.x = x & 3

|=

x |= 3

Performs a bitwise OR and updates the variable.x = x | 3

^=

x ^= 3

Performs a bitwise XOR and updates the variable.x = x ^ 3

>>=

x >>= 1

Shifts bits to the right and updates the variable.x = x >> 1

<<=

x <<= 1

Shifts bits to the left and updates the variable.x = x << 1

:=

if (n := len(a)) > 5:

Assigns a value to a variable inside an expression.Assigns n, then checks if n > 5


💼 Example

= Simple Assingment

x = 5

+= Addition Assingment

x = 3
x += 5 # Output: 8

-= Subtraction Assingment

x = 9
x -= 6 # Output: 3

*= Multiplication Assingment

x = 3
x *= 5 # Output: 15

/= Division Assingment

x = 10
x /= 5 # Output: 2

%= Modulus Assingment

x = 9
x %= 5 # Output: 4 

//= Floor Division Assignment

x = 3
x //= 2 # Output: 1

**= Exponentiation Assignment

x = 3
x **= 2 # Output: 9

&= Bitwise AND Assignment

x = 3
x &= 0 # Output: 3
  • Convert to binary:

    • 3 in binary is 0011

    • 0 in binary is 0000

  • Apply Bitwise AND (&): A bitwise AND outputs a 1 only if both corresponding bits are 1.

  0011  (Decimal 3)
& 0000  (Decimal 0)
------
  0000  (Decimal 0)

|= Bitwise OR Assignment

x = 3
x &= 0 # Output: 3
  • Convert to binary:

    • 3 in binary is 0011

    • 0 in binary is 0000

  • Apply Bitwise OR (|): A bitwise OR outputs a 1 only if any bit is 1.

  0011  (Decimal 3)
| 0000  (Decimal 0)
------
  0011  (Decimal 3)

^= Bitwise XOR Assignment

x = 3
x ^= 1 # Output: 2
  1. Initial Assignment: x = 3 stores the integer 3 in memory. In binary, 3 is represented as 011., 1 is represented as 001

  2. Binary Evaluation: XOR returns 1 only if the bits are different. XORing any number with 0 always leaves the original number completely unchanged.

  011  (binary for 3)
^ 001  (binary for 1)
------
  010  (binary for 2)

>>= Right Shift Assignment

x = 8
x >>= 1  # Output: 4
  1. Initial Assignment: x = 8 stores the integer 8 in memory. In binary, 8 is represented as 1000.

  2. Binary Evaluation: The >> operator shifts all the bits to the right by the specified number of positions. Bits that move beyond the right side are discarded, and 0s are added on the left for positive numbers.

  1000  (binary for 8)
>>    1  (shift right by 1 position)
------
  0100  (binary for 4)

<<= Left Shift Assignment

x = 3
x <<= 1  # Output: 6
  1. Initial Assignment: x = 3 stores the integer 3 in memory. In binary, 3 is represented as 011.

  2. Binary Evaluation: The << operator shifts all the bits to the left by the specified number of positions. 0s are added on the right.

  011  (binary for 3)
<<  1  (shift left by 1 position)
-----
  110  (binary for 6)

:= Walrus Assignment

if (x := 5) > 3:
    print(x)  # Output: 5
  1. Assignment and Evaluation: x := 5 assigns the value 5 to x and at the same time returns the value 5.

  2. Condition Evaluation: Since 5 > 3 is True, the if block executes.

  x := 5
  ↓
  x gets 5
  ↓
  expression also gives 5
  ↓
  5 > 3 → True