Operators in python

In this tutorial you will learn about operators in python and it’s types which is symbol used to perform specific mathematical and logical manipulation.

Types of operators

  • Arithmetic operator

Arithmetic operator is basically used to perform mathematical computation like addition, subtraction, multiplication and division.

OperatorMeaningExample
+Add two operands
or unary plus
*n+n
+5
Subtract
or unary minus
m-n
-10
*Multiply two operandm*n
/divide
(Result is in float)
m/n
%Modulus
(Return Remainder)
m%n
//Floor division
(Return whole number)
m//n
**Exponent
(Left operand raised to power of right)
m**n
(m to the power of y)

Examples :

m=10
n=4
print("m+n=",m+n)
print("m-n=",m-n)
print("m*n=",m*n)
print("m/n=",m/n)
print("m//n=",m//n)
print("m**n=",m**n)

Output:

m+n=14
m-n=6
m*n=40
m/n=2.5
m//n=2
m**n=10000
  • Comparison operator

The operator which is used to compared two or more values is known as comparison operator. It return True or False boolean value according to condition.

OperatorMeaningExample
>Greater thanm>n
<Less thanm<n
==Equal tom==n
!=not equal tom!=n
>=Greater than or equal tom>=n
<=Less than or equal tom<=n

Examples:

m=15
n=5
print("m>n=",m>n)
print("m<n=",m<n)
print("m==n=",m==n)
print("m!=n=",m!=n)
print("m>=n=",m>=n)
print("m<=n=",m<=n)

Output :

m>n=True
m<n=False
m==n=False
m>=n=True
m<=n=False
  • Logical operator

Logical operator are operator which is used for conditioning a statement. ‘and, or, not ‘ are logical operator.

OperatorMeaningExample
AndTrue if both operands are truem and n
OrTrue if any one operands are truem or n
NotTrue if operand is falsenot m

Example :

x=True
y=False
print(m and y)
print(m or y)
print(not m)

Output :

False
True
False
  • Bitwise operator
OperatorMeaningExample
|Bit wise OR2|2=1
&Bit wise AND2&2=4
^Bit wise XOR5^3=6
  • Assignment operator
OperatorMeaningExample
=x=10x=10
+=x+=10x=x+10
-=x-=10x=x-10
/=x/=10x=x/10
*=x*=10x=x*10
%=x%=10x=x%10
//=x//=10x=x//10
**=x**=10x=x**10
Was this article helpful?
YesNo

Leave a Comment