The
operators are used to perform operations on variables and values. Python has the following operators:
x = 5
y = 2
print(x+y)
o/p = 7
Print (x - y)
o/p = 3
Print (x * y)
o/p = 10
Print (x/y)
o/p = 2.5
Print (x%y)
o/p = 1
Print the power of (x ** 2) operation
o/p = 25
print ( x // 2) performs integer division
o/p = 2
Print (x > y)
o/p = true
Print (x
o/p = false
Print (x >= y)
o/p = true
Print (x
o/p = false
Print (x==y)
o/p = false
Print (x!=y)
o/p = true
x = true
y = false
Print (x or y)
o/p = true
Print (x and y)
o/p = false
Print (not x)
o/p = false
Print (not y)
o/p = true
They handle binary values
x = 2
y = 3
Print (x & y)
o/p = 2
Binary 2 = 010
Binary 3 = 011
Bitwise operation on 010 and 011 gives 010 (decimal 2)
Print (x | y)
o/p = 3
Bitwise OR of 010 and 011 gives 011 (decimal 3)
Print (x >> 2)
o/p = 0
Print(y
o/p = 24
print (~x) # 1’s complement
o/p = -3
a = 3
Print (1)
o/p = 3
a += 3
o/p = 6
a =- 3
o/p = 3
a *= 3
o/p = 9
a &= 3
a++ and ++a do not exist in Python. Using it will produce a syntax error.
Check if 2 variables are in the same memory location.
a = 3
b = 3
Print (a is b)
o/p = true
a = [1,2,3]
b = [1,2,3]
Print (a is b)
o/p = false
Print (a is not b)
o/p = true
x="Delhi"
Print ("D" in x)
o/p = true
Print ("D" is not in x)
o/p = false
x = [1,2,3]
Print (5 inches x)
o/p = false
The above is the detailed content of Python operators explained. For more information, please follow other related articles on the PHP Chinese website!