Operators in Python [Part-3]

Membership operators: This operators are used to test if a value or variable is present in a sequence (string, list, tuple, set and dictionary). Assume two variable x and y, then:
Operator Description Example
in True if value or variable is present in the sequence x in y
not in True if value or variable is not present in the sequence x not in y

Example -
my=["Neoogy","Google","Whatsapp","Facebook"]
print("Neoogy" in my) # returns True because the value "Neoogy" is in the sequence (list)
print("Twitter" not in my) # returns True because the value "Twitter" is not in the sequence (list)

Output will be
True
True


Bitwise operators: The following table shows the bitwise operators used in Python to compare binary numbers.
Operator Name Description
& AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one of two bits is 1
^ XOR Sets each bit to 1 if only one of two bits is 1
~ NOT Inverts all the bits
<< Left shift The left operands value is moved left by the number of bits specified by the right operand.
>> Right shift The left operands value is moved right by the number of bits specified by the right operand.

Example -
a=20 # In binary 10 = 0001 0100
b=10 # In binary 13 = 0000 1010
c=a&b
print(c) #0000 0000 which is 0
d=a|b
print(d) #0001 1110 which is 30 in decimal
e=a^b
print(e) #0001 1110 which is 30 in decimal
f=~b
print(f) #1111 0101 which is -11 in decimal
g=a<<3
print(g) # 1010 0000 which is 160 in decimal
h=b>>3
print(h) #0000 0001 which is 1 in decimal

Output will be
0
30
30
-11
160
1


0 Comments