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)
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. |
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")
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
print(x1)
x2 = 9
y2 = 4
x2 -= y2
print(x2)
x3 = 9
y3 = 4
x3 *= y3
print(x3)
x4 = 9
y4 = 4
x4 /= y4
print(x4)
x5 = 9
y5 = 4
x5 %= y5
print(x5)
x6=2
y6=3
x6**=y6
print(x6)
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 |
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)
For more details click hereMembership 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)
print("Tom" in list)
print("Ravi" not in list)
print("Tom" not in list)
( 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
print(x is y)
print(x is not y)
print(x is z)
print(x is not z)