Learn about more advanced operators in Python

Photo by Jess Bailey on Unsplash

Learn about more advanced operators in Python

Written by Jacqueline Lim

What will I be covering today?

I will be going through the advanced types of operators as this is going to be a continuation of my last post, 'Learning about common operators in Python'.

What are the types of advanced operators in Python?

The more advanced types of operators can be classified as Membership operators, Identity operators, and Bitwise operators.

Membership operators:

  • in operators
  • not in operator

Here's how to use the in operator:

In the example below, the in operator checks if a value (x) is in another value (y), if the value (x) is in another value (y) then it returns True.

x = 1
y = [0, 1, 2]
is x in y?
#output: True
x = 'hello'
y = 'world'
Is x in y?
#output: False

Here's how to use not in operator:

In the example below, the not-in operator checks if a value (s) is not in another value (t), if the value is not in another value then it returns True, otherwise, it returns False.

s = 0
t = [0, 1, 3]
Is s not in t?
#output: False (as s is in t)
s = 'hello'
t = 'world'
Is s not in t?
#output: True (as s is not in t)

Identity operators:

  • is
  • is not

Here's how to use is operator:

In the example below, the is operator compares 2 values (o and n) and returns True if both values are the same, otherwise, it returns False.

o = 9
n = 9
o is n
#output: True
o = 10
n = 11
o is n
#output: False

Here's how to use is not operator:

In the example below, the is operator compares 2 values (c and b) and returns a False if both values are the same, otherwise, it returns True.

c = 9
b = 9
c is not b
#output: False
c = 'hello'
b = 'welcome'
c is not b
#output: True

Bitwise operators:

  • & (Bitwise And)
  • | (Bitwise Or)
  • ^ (Bitwise xor)
  • ~ (Bitwise 1's complement)
  • << (Bitwise left-shift)
  • (>>) (Bitwise right-shift)

Here's how to use the bitwise and operator:

In the example below, the bitwise and (&) operator does logical And on the corresponding bits in the values (v and k).

v = 3 (bits - 011)
k = 5 (bits - 101)
v & k 
#output:  1 (bits - 001)

Here's how to use the bitwise or operator:

In the example below, the bitwise or operators perform logically Or on corresponding bits in the values (a and b).

a = 9 (bits - 1001)
b = 6 (bits - 0110)
a | b (1001 or 0110 = 1101)
#output: 15

Here's how to use the bitwise xor operator:

In the example below, the bitwise xor operator (^) performs logical XOR on corresponding bits in values (o and g).

o = 9
g = 5 
o^g
#output: 12 (1001^0101 = 1010)

Here's how to use the bitwise 1's operator:

In the example below, the bitwise 1's (~) operator inverts the bits of the corresponding value (p) and adds 1 to that value (p).

p = 5 (bits - 101)
~p
#output: 6 (~101 = 5 + 1 (110))

Here's how to use the bitwise left-shift operator:

In the example below, the bitwise left-shift (<<) operator moves/shifts the bits for a value (v) left by a certain number of spaces specified (w), and it adds 0s to new positions.

v = 5 (101)
w = 2 
v << w (10100 = 16 + 4)
#output: 20 (because the value got shifted left 2 times)

Here's how to use the bitwise right operator:

In the example below, the bitwise-right (>>) operator moves/shifts the bits for a value (o) right by a certain number of spaces (d), and it adds 0s to new positions, and some bits will be lost in the process.

o = 5 (101)
d = 2
o>>d (00001)
#output: 1 (because the bits got shifted right twice)

Conclusion

Whilst learning about more advanced operators that are in Python, I was surprised about the bitwise operators as I had used some of them before but I had no idea that there were more bitwise operators.

Learn about more operators and how they can be used!