OPERATORS
Operators are constructs used to modify the values of operands. Consider the following expression:
3 + 4 = 7
In the above expression, 3 and 4 are the operands whereas + is operator. Based on
functionality, operators are categories into following seven types:
1. Arithmetic operator
2. Comparison operator
3. Assignment operator
4. Logical operator
5. Bitwise operator
6. Membership operator
7. Identity operator
Operator Precedence
Operator Description
NOT, OR AND Logical operators
in , not in Membership operator
is, not is Identity operator
=, %=, /=, //=, -=, +=, *=, **== Assignment operators.
<>, ==, != Equality comparison operator
<=, <, >, >= Comparison operators
^, | Bitwise XOR and OR operator
& Bitwise AND operator
<<, >> Bitwise left shift and right shift
+, - Addition and subtraction
*, /, %, // Multiplication, Division, Modulus and floor division
** Exponential operator
Arithmetic Operators
These operators are used to perform arithmetic operations such as addition, subtraction, multiplication and
division.
List of Arithmetic Operators
Operator Description Example
+ Addition operator to add two operands. 10+20=30
– Subtraction operator to subtract two operands. 10–20=–10
* Multiplication operator to multiply two operands. 10*20=200
/ Division operator to divide left hand operator by right hand Operator. 5/2=2.5
** Exponential operator to calculate power. 5**2=25
% Modulus operator to find remainder. 5%2=1
// Floor division operator to find the quotient and remove the fractional part. 5//2=2
Example
>>> x = 10
>>> y = 12
>>> z = 0
>>> z = x + y
>>> print z
22 #Ouput
>>> z = x - y
>>> print z
-2 #Ouput
>>> z = x * y
>>> print z
120 #Ouput
>>> z = x / y
>>> print z
0 #Ouput
>>> z = x % y
>>> print z
10 #Ouput
>>> z = x ** y
>>> print z
1000000000000 #Ouput
>>> z = x // y
>>> print z
0 #Ouput
Comparison Operators
These operators are used to compare values. Comparison operators are also called relational operators. The
result of these operators is always a Boolean value, that is, either true or false. Table provides a list of
comparison operators.
List of Comparison Operators
Operator Description Example
== Operator to check whether two operands are equal. 10 == 20, false
!= or <> Operator to check whether two operands are not equal. 10 !=20, true
> Operator to check whether first operand is greater than second operand. 10 > 20, false
< Operator to check whether first operand is smaller than second operand. 10 < 20, true
>= Operator to check whether first operand is greater than or equal to second operand. 10 >= 20, false
<= Operator to check whether first operand is smaller than or equal to second operand. 10 <= 20, true
Assignment Operators
This operator is used to store right side operand in the left side operand. Table provides a list of
assignment operators.
List of Assignment Operators
Operator Description Example
= Store right side operand in left side operand. a=b+c
+= Add right side operand to left side operand and store the result in left side operand. a+=b or
a=a+b
–= Subtract right side operand from left side operand and store the result in left side a–=b or a=a–
operand. b
*= Multiply right side operand with left side operand and store the result in left side a*=b or
operand. a=a*b
/= Divide left side operand by right side operand and store the result in left side operand. a/b or a=a/b
%= Find the modulus and store the remainder in left side operand. a%=b or a=a
%b
** = Find the exponential and store the result in left side operand. a**=b or
a=a**b
// = Find the floor division and store the result in left side operand. a//=b or
a=a// b
Example
>>> x = 10
>>> y = 12
>>> y += x
>>> print y
22
#Output
>>> y *= x
>>> print y
220 #Output
>>> y /= x
>>> print y
22 #Output
>>> y %= x
>>> print y
2 #Output
>>> y **= x
>>> print y
1024 #Output
>>> y //= x
>>> print y
102 #Output
Bitwise Operators
These operators perform bit level operation on operands. Let us take two operands x = 10 and y = 12. In binary
format this can be written as x = 1010 and y = 1100. 3.5 presents a list of bitwise operators.
List of Bitwise Operators
Operator Description Example
& Bitwise AND This operator performs AND operation between operands. Operator x & y results 1000
copies bit if it exists in both operands.
| Bitwise OR This operator performs OR operation between operands. Operator copies bit x | y results 1110
if it exists in either operand.
^ Bitwise XOR This operator performs XOR operation between operands. Operator x ^ y results 0110
copies bit if it exists only in one operand.
~ bitwise inverse This operator is a unary operator used to opposite the bits of operand. ~ x results 0101
<< left shift This operator is used to shift the bits towards left x << 2 results 101000
<< right shift This operator is used to shift the bits towards right x >> 2 results 0010
Example
>>> x = 10
>>> y = 12 # 10 = 0000 1010
>>> z = 0 # 12 = 0000 1100
# Bitwise AND
>>> z = x & y
>>> print z
8 # 8 = 0000 1000
# Bitwise OR
>>> z = x | y
>>> print z
14 # 14 = 0000 1110
# Bitwise XOR
>>> z = x ^ y
>>> print z
6 # 6 = 0000 0110
# Bitwise inverse
>>> z = ~x
>>> print z
-11 # -11 = 1111 0101
# Left shift
>>> z = x << 2
>>> print z
40 # 40 = 0010 1000
# Right shift
>>> z = x >> 2
>>> print z
2 # 2 = 0000 0010
Logical Operators
These operators are used to check two or more conditions. The resultant of this operator is always a Boolean
value. Here, x and y are two operands that store either true or false Boolean values. Table presents a list of
logical operators. Assume x is true and y is false.
List of Logical Operators
Operator Description Example
and logical AND This operator performs AND operation between operands. When both x and y results false
operands are true, the resultant become true.
or logical OR This operator performs OR operation between operands. When any x or y results true
operand is true, the resultant becomes true.
not logical NOT This operator is used to reverse the operand state. not x results false
Membership Operators
These operators are used to check an item or an element that is part of a string, a list or a tuple. A membership
operator reduces the effort of searching an element in the list. Suppose, x stores a value 20 and y is the list
containing items 10, 20, 30, and 40. Then, x is a part of the list y because the value 20 is in the list y. Table
gives a list of membership operators.
Operator Description Example
in Return true, if item is in list or in sequence. Return false, if item is not in list or x in y, results true
in sequence.
not in Return false, if item is in list or in sequence. Return true, if item is not in list or x not in y, results false
in sequence.
Example
>>> x = 10
>>> y = 12
>>> list = [21, 13, 10, 17]
>>> if (x in list):
print “x is present in the list” else: print “x is not present in the list”
x is present in the list
#Output
>>> if (y not in list):
print “y is not present in the list” else:
print “y is present in the list”
y is not present in the list
Identity Operators
These operators are used to check whether both operands are same or not. Suppose, x stores a value 20 and y
stores a value 40. Then x is y returns false and x not is y returns true. Table 3 provides a list of
identity operators
List of Identity Operators
Operator Description Example
is Return true, if the operands are same. Return false, if the operands are not same. x is y, results false
not is Return false, if the operands are same. Return true, if the operands are not same. x not is y, results true
Example
>>> x = 12
>>> y = 12
>>> if ( x is y):
print “x is same as y”
else:
print “x is not same as y”
x is same as y #Output
>>> y = 10
>>> if ( x is not y):
print “x is not same as y”
else:
print “x is same as y”
x is not same as y