Operators In Python
Python’s Operators
Operators are special symbols in Python that
carry out arithmetic or logical computation.
Type of operators in Python:
Arithmetic operators
Comparison operators
Logical operators
Assignment operators
Membership operators
Arithmetic operators
Arithmetic operators are used to perform
mathematical operations like addition,
subtraction, multiplication etc.
Example Meaning Operator
x+y Addition operation +
x-y Subtraction operation -
x*y Multiplication operation *
x/y Division operation(always result in /
float)
x%y Modulus(remainder) operation %
x//y Integer Division Operator //
x**y Exponentiation operation **
Example:
x=15
y=4 output
print(x+y) 19
print(x-y) 11
print(x*y) 60
print(x/y)
3.75
print(x%y)
3
print(x//y)
3
print(x**y)
50625
Comparison operators
Comparison operators are used to compare
values. It either returns True or False
according to the condition.
Example Meaning Operator
x>y Greater than >
x<y Less than <
x>=y Greater than or equal =>
x<=y Less than or equal =<
x==y equal ==
x!=y Not equal
=!
Example:
x=10
y=30 Output
print(x>y) False
print(x<y) True
print(x>=y) False
print(x<=y) True
print(x==y) False
print(x!=y)
True
Logical operators
Logical operators are used to connect the
comparison operators
Example Description
Operator
x>z and x>y Become true if the two condition
and
are true, otherwise become false
x>z or x>y Become false if the two condition
or
are false, otherwise become true
not x>=y Become true if the condition
false and become false if the not
condition true
Example:
x=10
y=5
Output
z=30
True
print(x>y and z>x)
True
print(x<y or y<z)
False
print(not x>y)
Assignment operators
Assignment operators are used in Python to
assign values to variables.
equivalent to Example Operator
x=5 x=5 =
x=x+5 x+=5 =+
x=x-5 x-=5 =-
x=x*5 x*=5 =*
x=x/5 x/=5 =/
x=x%5 x%=5 =%
x=x//5 x//=5 =//
x=x**5 x**=5 =**
Example:
x=5
x+=10 Output
print(x)
15
x//=2
7
print(x)
x%=4 3
print(x) 27
x**=3
print(x)
Membership operators
Membership operators are used to test whether a
value or variable is found in a sequence (string, list,
tuple, set and dictionary).
Example Meaning Operator
in x 5 True if value/variable is found in the sequence in
not in x 5 True if value/variable is not found in the not in
sequence
Example:
x=“hello python”
y=[3,8,1,4] Output
print(“h”in x) True
print(“py”not in x) False
print(3 in y) True
print(9 not in y) True
print([1] in y) False