Operators in Python
• Operators are the constructs which can
manipulate the value of operands.
• In an expression, an operator is used on
operands.
• Example: 4 + 5 = 9.
• 4 and 5 are called operands
• + is called an Operator
• There are various operators in python:
1. Arithmetic Operator
2. Comparison/Relational Operator
3. Bitwise Operator
4. Assignment Operator
5. Logical Operator
6. Membership Operator
7. Identity Operator
Arithmetic Operators
Arithmetic operators are used to perform some basic arithmetic
operations. These operators can be applied on numbers as well as on
variables to perform the corresponding operations.
Example:
>>>x=10
>>>y=20
>>>z=x+y
>>>print z
30 #output
>>> z=x*y
>>>print z
200
>>>z=x**2
>>>print z
100
• Find the volume of sphere with radius ‘r’
r=5.
Operator Description Example
Performs exponential (power) calculation on
** Exponent c=a**b
operators
* Multiplication Multiplies values on either side of the operator c=a * b
Divides left hand operand by right hand
/ Division c=b / a
operand
Divides left hand operand by right hand
% Modulus c=b % a
operand and returns remainder
Floor Division - The division of operands where
the result is the quotient in which the digits after
the decimal point are removed. But if one of the
// 9//2=4
operands is negative, the result is floored, i.e.,
rounded away from zero (towards negative
infinity):
+ Addition Add values on either side of the operator. c=a + b
Subtracts right hand operand from the left hand
- Subtraction c=a – b
operand.
Relational (Comparison) Operators
Comparison operators also known as relational operators are used to compute
the values on its either sides and determines the relation between them.
Example, a=100 and b=200
Operator Description Example
If the values of two operands are equal, then the (a == b)
==
condition becomes true. returns False
If the values of two operands are not equal, then (a!=b)
!=
condition becomes true. returns true
If the value of left operand is greater than the value of (a > b)
>
right operand, then condition becomes true. Returns False
If the value of left operand is less than the value of right (a < b)
<
operand, then condition becomes true. returns True
If the value of left operand is greater than or equal to the (a >= b)
>=
value of right operand, then condition becomes true. Returns False
If the value of left operand is less than or equal to the (a <= b)
<=
value of right operand, then condition becomes true. Returns True
Bitwise Operators
• These operators perform bit level operations on operands. Let
us take two operands x=10 and y=4. In binary format this can
be written as x=1010 and y=0100.
Operator Description Example
Operator copies a bit to the result if it exists in x& y = 0
& Binary AND
both operands (0000 0000)
x | y = 14
| Binary OR It copies a bit if it exists in either operand.
(0000 1110)
It copies the bit if it is set in one operand but x ^ y = 14
^ Binary XOR
not both. (0000 1110)
<< Binary Left The left operands value is moved left by the x<< 2 = 40
Shift number of bits specified by the right operand. (0010 1000)
>> Binary Right The left operands value is moved right by the x>> 2 = 2
Shift number of bits specified by the right operand. (0000 0010)
Bitwise
operators
i. & (binary and)
ii. | (binary or)
iii. ^ (binary xor)
iv.<< (left shift)
v >> (right shift)
& (binary and)
• a = 1010 (Binary of 10)
• b = 0100 (Binary of 4)
• a&b=
1010
&
0100
= 0000 =
0 (Decimal)
| (binary or)
• a = 1010 (Binary of 10)
• b = 0100 (Binary of 4)
• a|b=
1010
|
0100
= 1110 =
14 (Decimal)
^ (binary xor)
• a = 1010 (Binary of 10)
• b = 0100 (Binary of 4)
• a^b=
1010
^
0100
= 1110 =
14 (Decimal)
<< (left shift)
a= 0000 0101 (Binary of 5)
a << 1 = 0000 1010
= 10 (decimal)
a << 2
= 0001 0100
= 20 (decimal)
>> (right
shift)
• a=1010 (Binary of 10)
• a>>2 1010>>2
1 0 1 0
1 0
10 (Binary) = 2 in Decimal
Assignment Operator
• This operator is used to store the right side operand in the left
side operand
Operator Description Example
Assigns values from right side operands to left c = a + b assigns value
=
side operand of a + b into c
It adds right operand to the left operand and c += a is equivalent to
+=
assign the result to left operand c=c+a
It subtracts right operand from the left operand c -= a is equivalent to
-=
and assign the result to left operand c=c-a
It multiplies right operand with the left operand c *= a is equivalent to c
*=
and assign the result to left operand =c*a
It divides left operand with the right operand c /= a is equivalent to
/=
and assign the result to left operand c =c/a
It takes modulus using two operands and assign c %= a is equivalent to
%=
the result to left operand c=c%a
**= Performs exponential (power) calculation on c **= a is equivalent to
operators and assign value to the left operand c = c ** a
//= It performs floor division on operators and c //= a is equivalent to
assign value to the left operand c = c // a
Logical Operators
• These operators are used to check two or more conditions. The
resultant operator is always a Boolean value. Here, x and y
are two operands that store either true or false Boolean values.
• Assume x is 10
Operator Description Example
and (logical When both operands are true, the x>8 and x>5
and) resultant become true. results true
When any operand is true, the x>8 or x>10
or (logical or)
resultant become true. results true
not (logical This operator is used to reverse the not (x>8 or x>10)
NOT) operand state. results false
Membership Operators
Python supports two types of membership operators–in and not in. These
operators, test for membership in a sequence such as strings, lists, or tuples.
in Operator: The operator returns true if a variable is found in the specified
sequence and false otherwise.
not in Operator: The operator returns true if a variable is not found in the
specified sequence and false otherwise.
Example:
>>>x=10
>>>y=12
>>>list=[21,13,10,17]
>>>10 in list
True
>>>5 in list
False
>>>10 not in list
False
Identity Operators
• is Operator: Returns true if operands or values on
both sides of the operator point to the same object
and false otherwise.
• is not Operator: Returns true if operands or
values on both sides of the operator does not point
to the same object and false otherwise. Example:
>>>x=12 >>>x=12
>>>y=12 >>>y=12
>>> x is y >>> x is not y
True False
Precedence of Python Operators
Operator Description
** Exponentiation (raise to the power)
~+- Complement, unary plus(+a) and minus (-a)
* / % // Multiply, divide, modulo and floor division
+- Addition and subtraction
>> << Right and left bitwise shift
& Bitwise 'AND'
^| Bitwise XOR and OR
<= < > >= Comparison operators
== != Equality operators
= %= /= //= -= += *= **= Assignment operators
is is not Identity operators
in not in Membership operators
not or and Logical operators
Example:
Output: