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
Symbol | Operation | Example |
+ | Addition | x+y |
- | Subtraction | x-y |
* | Multiplication | x*y |
/ | Division | x/y |
% | Modulus | x%y |
** | Exponent | 2**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
Symbol | Operation | Example |
== | Equal to | 2==3 returns 0 |
!= | Not equal to | 2!=3 returns 1 |
> | Greater than | 2>3 returns 0 |
< | Less than | 2<3 returns 1 |
>= | Greater than or equal to | 2>=3 returns 0 |
<= | Less than or equal to | 2<=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
Symbol | Example | Same as |
= | x=y | x=y |
+= | x+=y | x=x+y |
-= | x-=y | x=x-y |
*= | x*=y | x=x*y |
/= | x/=y | x=x/y |
%= | x%=y | x=x%y |
**= | x**=y | x=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
Symbol | Operation | Example |
& | Bitwise AND | x&y |
| | Bitwise OR | x|y |
<< | Shift Left | x<<2 |
>> | Shift Right | x>>2 |
^ | X-OR | x^y |
For more details click herex=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 """
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).
operator | Description |
in | Returns true if it finds specified element in sequence otherwise returns false. |
not in | Returns 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).
operator | Description |
is | It returns true if both variables are the same object otherwise returns false. |
is not | It 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
"""