OPERATORS
> Operators are the constructs which can manipulate the value of operands.
Consider the expression 4 + 5 = 9.
> Here, 4 and 5 are called operands and + is called operator Types of
Operators: -Python language supports the following types of operator
• Arithmetic Operators
• Comparison (Relational)Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
Arithmetic operators:
They are used to perform mathematical operations like addition, subtraction,
multiplication etc. Assume, a=10 and b=5
Operator Description Example
+ Addition Adds values on either side of the a + b = 30
operator.
- Subtraction Subtracts operand. right hand a – b = -10
operand from left hand
* Multiplication Multiplies values on either side of a * b = 200
the operator
/ Division Divides left hand operand by right b/a=2
hand operand
% Modulus Divides left hand operand by right b%a=0
hand operand and returns remainder
** Exponent Performs operators exponential a**b =10 to the power 20
(power) calculation on operators
// Floor Division - The division of 5//2=2
operands where the result is the
quotient in which the digits after the
decimal point are removed
Examples Output:
a=10; b=5
print("a+b=",a+b) a+b=15
print("a-b=",a-b) a-b= 5
print("a*b=",a*b) a*b= 50
print("a/b=",a/b) print("a a/b= 2.0
%b=",a%b) print("a//b=",a//b) a%b=0
print("a**b=",a**b) a//b=2
a**b= 100000
Comparison (Relational)Operators:
• Comparison operators are used to compare values.
• It either returns True or False according to the condition. Assume, a=10 and b=5
Operator Description Example
== If the values of two operands are equal, (a == b) is not true.
then the condition becomes true.
!= If values of two operands are not equal, (a!=b) is true
then condition becomes true.
> If the value of left operand is greater than (a > b) is true.
the value of right operand, then condition
becomes true.
< If the value of left operand is less than the (a < b) is not true.
value of right operand, then condition
becomes true
>= If the value of left operand is greater than (a >= b) is not true.
or equal to the value of right operand,
then condition becomes true.
<= If the value of left operand is less than or (a <= b) is true.
equal to the value of right operand, then
condition becomes true.
Example
a=10 Output:
b=5 a>b=> True
print("a>b=>",a>b) a>b=> False
print("a>b=>",a<=b) a==b=> False
print("a==b=>",a==b) a!=b=> True
print("a!=b=>",a!=b) a>=b=> False
print("a>=b=>",a>=b) a>=b=> True
print("a<=b=>",a<=b)
Logical Operators:
Logical operators are the and, or, not operators
operator Meaning Example
AND True If both operands are true X and Y
OR True if either of the operands are true X or Y
NOT True if operand is False Not X
Example Output
a = True
b = False
print('a and b is', a and b) x and y is False
print('a or b is' ,a or b) x or y is True
print('not a is', not a) not x is False
Assignment Operators:
-Assignment operators are used in Python to assign v
alues to variables.
Logical Operators:
-Logical operators are the and, or, not operators.
Example Output
a = True a and b is False
b = False a or b is True
print('a and b is', a and b) not a is False
print('a or b is' ,a or b)
print('not a is', not a)
Membership Operators:
Evaluates to find a value or a variable is in the
specified sequence of string, list, tuple, dictionary or
not.
Let, x=[5,3,6,4,1]. To check particular item in list or not,
in and not in operators areused.
Example:
x=[5,3,6,4,1]
>>>5 in x
True
>>>5 not in x
False
Example
x =5
y =5
x2 = 'Hello'
y2= 'Hello'
print(x1 is not y1)
print(x2 is y2)
Output:
False
True
PRECEDENCE OPERATOR
When an expression contains more than one operator, the order of
evaluation depends on the order of operations.
Operator Description
** Exponentiation (raise to the power)
* / % // Multiply, divide, modulo and floor division
+- Addition and subtraction
>><< Right and left bitwise shift
& Bitwise 'AND'
^| Bitwise exclusive `OR' and regular `OR'
is is not Identity operators
in not in Membership operators
not or and Logical operators
Thank you