Sunday, 11 October 2020

Python Operators: Arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators, identity operators, membership operators

Python  Basic Operator

  • It is a special symbol which is used to perform logical or mathematical operation on data or variable.

Operand

  • It is a data or variable on which the operation is to be performed.

Types of Operator

  • ⇒Arithmetic Operators
  • ⇒Relational Operators
  • ⇒Logical Operators
  • ⇒Assignment Operators
  • ⇒Bitwise Operators
  • ⇒Membership Operators
  • ⇒Identity Operators

Arithmetic Operators

SymbolOperationExample
+Additionx+y
-Subtractionx-y
*Multiplicationx*y
/Divisionx/y
%Modulusx%y
**Exponent2**3=8

x=5
y=2
print("x+y=",x+y)
print("x-y=",x-y)
print("x*y=",x*y)
print("x/y=",x/y)
print("x%y=",x%y)
print("x**y=",x**y)
"""
***Output***
x+y= 7
x-y= 3  
x*y= 10
x/y= 2.5
x%y= 1
x**y= 25
"""

Relational Operators

SymbolOperationExample
==Equal to2==3 returns 0
!=Not equal to2!=3 returns 1
>Greater than2>3 returns 0
<Less than2<3 returns 1
>=Greater than or equal to2>=3 returns 0
<=Less than or equal to2<=3 returns 1

Logical Operators

(x>y)and(x>z)Here this expression returns true if both conditions are true.
(x>y)or(x>z)Here this expression returns true if any one or both conditions are true.
not(x>y)Not operator reverses the state means if the condition is true it returns false and if the condition is false it returns true.

#Show result according to percent 
x=int(input("Enter first number:"))
y=int(input("Enter second number:"))
z=int(input("Enter third number:"))
if x>y and x>z:
    print(x," is greatest")
if y>x and y>z:
    print(y," is greatest")
if z>x and z>y:
    print(z," is greatest")
"""
***Output***
Enter first number:58
Enter second number:86
Enter third number:27
86  is greatest
"""
 

Assignment Operators

SymbolExampleSame as
=x=yx=y
+=x+=yx=x+y
-=x-=yx=x-y
*=x*=yx=x*y
/=x/=yx=x/y
%=x%=yx=x%y
**=x**=yx=x**y

x1 = 9
y1 = 4
x1 += y1 #x1=x1+y1
print(x1)
x2 = 9
y2 = 4
x2 -= y2 #x2=x2-y2
print(x2)
x3 = 9
y3 = 4
x3 *= y3 #x3=x3*y3
print(x3)
x4 = 9
y4 = 4
x4 /= y4 #x4=x4/y4
print(x4)
x5 = 9
y5 = 4
x5 %= y5 #x5=x5%y5
print(x5)
x6=2
y6=3
x6**=y6 #x6=x6**y6
print(x6)
"""
***Output***
13
5
36
2.25
1
8
"""
 

Bitwise Operators

SymbolOperationExample
&Bitwise ANDx&y
|Bitwise ORx|y
<<Shift Leftx<<2
>>Shift Rightx>>2
^X-ORx^y

x=6
y=3
print("x&y=",x&y)
print("x|y=",x|y)
print("x>>2=",x>>2)
print("x<<2=",x<<2)
print("x^2=",x^2)
"""
***Output***
x&y= 2
x|y= 7
x>>2= 1
x<<2= 24
x^2= 4
"""
 
For more details click here

Membership Operators

  • These operators are used to check specified element is present in a sequence(string,list,tuples etc) or not.
  • It returns boolean values(True/False).
operatorDescription
inReturns true if it finds specified element in sequence otherwise returns false.
not inReturns true if it does not find specified element in sequence otherwise returns false.

list=["Ravi","Shyam","Ashish"]
print("Ravi" in list)#true
print("Tom" in list)#false

print("Ravi" not in list)#false
print("Tom" not in list)#true
"""
***Output***
True
False
False
True
"""


( Reference and Acknowledgement: Easy Softwares by Mr Anil Singhania)

Identity Operators

  • These operators are used to compare two objects.
  • It returns boolean values(True/False).
operatorDescription
isIt returns true if both variables are the same object otherwise returns false.
is notIt returns true if both variables are not the same object otherwise returns false.

x=10
y=10
z=20
#true because of same identity of a and y
print(x is y)
#false because of same identity of a and y
print(x is not y)
#false because a and b have not same identity
print(x is z)
#true because a and b have not same identity
print(x is not z)
"""
***Output***
True
False
False
True
"""

Beneficial information

Shipwreck more tragic than Titanic

Watch video to know: Shipwreck more tragic than Titanic https://youtu.be/7IVaHgxHU20

Impact-Site-Verification: b5ea34f9-4d6d-4198-89d2-2740a9156405