This tutorial of Python Operators illustrates the Arithmetic, Assignment, Comparision, Logical, Identity, Membership, and Bitwise operators with examples.
Python Operators
In any programming language, operators are used for performing different kinds of operations on the variables and the values. Python allows us to use an extensive set of operators. Python Operators can be divided into seven different categories that are listed below.
 Arithmetic Operators
 Assignment Operators
 Comparison Operators
 Logical Operators
 Identity Operators
 Membership Operators
 Bitwise Operators
We’ll learn about the variables of each of these categories one by one.
Arithmetic Operators
The operators that are used to perform mathematical calculations on different values and variables are known as Arithmetic operators. As they are arithmetic operators, so they’re usually used for numerical values and the variables storing those numerical values. The different arithmetic operators available in python for simple mathematical calculations are listed below in tabular form and are well described with examples.
Arithmetic Operator  Name  Example  
+  Addition 


–  Subtraction 


*  Multiplication 


/  Division 


%  Modulus 


**  Exponentiation 


//  Floor Division 

Example. The following code illustrates the use of all of the abovedefined arithmetic operators.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 
a = 5 b = 3 #Addition add = a + b #Subtraction subtract = a  b #Multiplication multiply = a * b #Division divide = a / b #Modulus modulus = a % b #Exponentiation exponentiation = a ** b #Floor Division floorDivision = a // b print(add) print(subtract) print(multiply) print(divide) print(modulus) print(exponentiation) print(floorDivision) 
Assignment Operators
As the name suggested, the assignment operators are used to assign values to the variables. But assignment operators are also used to implicitly perform operations first on the assigned values and then storing the value into the variable. = is the only assignment operator that assigns the value as it is in the variable, all of the other assignment operators are used to first perform some specific operation on the value and then storing the value into the variable. Given below is the tabular representation of the different assignment operators with examples.
Assignment Operator  Example  Similar To  
=  a = 5 


+=  a += 3 


=  a = 3 


*=  a *= 3 


/=  a /= 3 


%=  a %= 3 


//=  a //= 3 


**=  a **= 3 


&=  a &= 3 


=  a = 3 


^=  a ^= 3 


>>=  a >>= 3 


<<=  a <<= 3 

Example. The following snippet of code illustrates the use of all of the above listed assignment operators.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 
#Assignment Operators a = 5 print(a) a = 5 a += 3 #a = a + 3 print(a) a = 5 a = 3 #a = a  3 print(a) a = 5 a *= 3 # a = a * 3 print(a) a = 5 a /= 3 # a = a / 3 print(a) a = 5 a %= 3 # a = a % 3 print(a) a = 5 a //= 3 # a = a // 3 print(a) a = 5 a **= 3 # a = a ** 3 print(a) a = 5 a &= 3 # a = a & 3 print(a) a = 5 a = 3 # a = a  3 print(a) a = 5 a ^= 3 # a = a ^ 3 print(a) a = 5 a >>= 3 # a = a >> 3 print(a) a = 5 a <<= 3 # a = a << 3 print(a) 
Comparison Operators
Comparison Operators are used to comparing two values at a time. The values need not just be numerical, these operators can also be applied to other python objects and even can be used for things like Python Strings Comparison. The following table lists the different comparison operators.
Comparison Operator  Name  Example  
==  Equal 


!=  Not equal 


>  Greater than 


<  Less than 


>=  Greater than or equal to 


<=  Less than or equal to 

Note. All of the comparison operators return Boolean value i.e. either True
or False.
Example. Although these operators can be used on different python objects yet, for example, we’ve illustrated the use of comparison operators only on the numerical values type variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
#Comparison Operators a = 5 b = 3 #Is Equal? print(a == b) #Is Not Equal? print(a != b) #Is a greater than b? print(a > b) #Is a smaller than b? print(a < b) #Is a greater than or equal to b? print(a >= b) #is a smaller than or equal to b? print(a <= b) 
Logical Operators
Logical Operators also return a Boolean value i.e. either True or False. They are used to check the result of two different boolean resultant expressions. In other words, they’re used to check if two or more conditions that are going to return boolean values are mutually True or False. There are only three logical operators in python that are listed below in the table.
Logical Operator  Description  Example  
and  It returns True if both the statements are True. 


or  It returns True if any one of the statements are True. 


not  It returns True if a statement is not true. 

Example. The following example illustrates the use of these logical operators.
1 2 3 4 5 6 7 8 9 10 11 
#Logical Operators a = 5 b = 3 andExample = (a > b) and (a < b) orExample = (a > b) or (a < b) notExample = not((a > b) and (a < b)) print(andExample) print(orExample) print(notExample) 
Identity Operators
Identity operators are used to checking if two different Python objects are exactly the same. Here the same does not mean equal, rather the same here means if two objects are exactly the same with the same memory location. Do not confuse it with equal and not equal operators. There are only two identity operators that are listed in the following table. Both of these operators return only boolean values.
Identity Operator  Description  Example  
is  It will only return True if two python objects are exactly the same objects. 


is not  It will return True if two python objects are not exactly the same objects. 

Example. Observe the following example carefully to understand the use of identity operators.
1 2 3 4 5 6 7 8 9 
#Idenitity Operators a = [1,2,3,4,5] b = [1,2,3,4,5] c = a print(a is c) print(a is b) print(a is not c) print(a is not b) 
Membership Operators
Membership Operators are used to check if a value is a member of a sequence or not. These are one of the most helpful operators in python and similar checks in other programming languages required extra coding efforts. The two membership operators available in python are defined below in the tabular form. Both of these variables will return boolean values.
Membership Operator  Description  Example  
in  returns True if a value is in the sequence 


not in  returns True if a value is not in the sequence 

Example. To illustrate the usage of the membership operators, we’ve provided a code snippet below that checks if the value is present in the list or not.
1 2 3 4 5 6 7 8 9 
#Membership Operators a = 5 b = 10 c = [1,2,3,4,5] print(a in c) print(a not in c) print(b in c) print(b not in c) 
Bitwise Operators
The operators that are used to perform operators bit by bit on the binary value of the actual value are known as Bitwise operators. For example, if the value is 5, then its binary form is 101, which includes three bits, two ones and one zero. Each individual bit will be involved in bitwise operations. There are six different bitwise operators that are listed below.
Bitwise Operator  Name  Description 
&  AND  Bitwise AND operation i.e. resultant bit will be 1 if bits from both the values are 1. 
  OR  Bitwise OR Operation i.e. resultant bit ill be 1 if anyone bit from both of the values is 1. 
^  XOR  Bitwise XOR operation. It will set the resultant bit to 1 if only one of the bits from both of the values will be 1. 
~  NOT  Bitwise NOT operation. It will simply invert all of the bits. 
<<  ZERO FILL LEFT SHIFT  The resultant bits will be shifted to left by pushing zeros in from the right and the leftmost bit will be fall off. 
>>  ZERO FILL RIGHT SHIFT  The resultant bits will be shifted to the right by pushing leftmost bit copies to the left and rightmost bit will fall off. 
Example. The following example illustrates the use of bitwise operators in python.
1 2 3 4 5 6 7 8 9 10 
#Bitwise Operators a = 5 #0101 b = 3 #0011 print(a & b) print(a  b) print(a ^ b) print(~ b) print(a << b) print(a >> b) 
I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!