Other Operators

Python Operators

Bitwise Operators

  • & (bitwise and)

  • (bitwise or)
  • ^ (bitwise xor)

  • >> (shift bits right)

  • << (shift bits left)

>>> 3 & 4 # 011 & 100 = 000
0
>>> 3 | 4 # 011 & 100 = 111
7
>>> 5 ^ 3 # 101 & 011 = 110
6
>>> 5 >> 1  # 110 >> 1 = 11
3
>>> 3 << 4  # 11 >> 4 = 110000
48

Assignment Operators

  • =

  • +=

  • -=

  • *=

  • =

  • \=

  • %=

  • ^=

  • &=

  • |=

  • >>=

  • <<=

>>> x = 5
>>> x *= 7 # x = x * 7
>>> x
35
>>> y = 3
>>> y <<= 2 # y = y << 2
>>> y
12

Identity Operators

  • is

  • is not

>>> first_letter = 'A'
>>> second_letter = 'A'
>>> third_letter = 'a'
>>> first_letter is second_letter
True
>>> third_letter is first_letter
False

Membership Test Operators

  • in

  • not in

>>>'a' in 'abc'
True
>>>'A' in 'abc'
False