This tutorial on Python Operators illustrates the Arithmetic, Assignment, Comparision, Logical, Identity, Membership, and Bitwise operators with examples.
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.
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.
a + b, 5 + 3
a - b, 5 - 3
a * b, 5 * 3
a / b, 5 / 3
a % b, 5 % 3
a ** b, 5 ** 3
a // b, 5 // 3
Example. The following code illustrates the use of all of the above-defined arithmetic operators.
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)
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 store 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 store the value in the variable. Given below is the tabular representation of the different assignment operators with examples.
|Assignment Operator||Example||Similar To|
|=||a = 5||
a = 5
|+=||a += 3||
a = a + 3
|-=||a -= 3||
a = a - 3
|*=||a *= 3||
a = a * 3
|/=||a /= 3||
a = a / 3
|%=||a %= 3||
a = a % 3
|//=||a //= 3||
a = a // 3
|**=||a **= 3||
a = a ** 3
|&=||a &= 3||
a = a & 3
||=||a |= 3||
a = a | 3
|^=||a ^= 3||
a = a ^ 3
|>>=||a >>= 3||
a = a >> 3
|<<=||a <<= 3||
a = a << 3
Example. The following snippet of code illustrates the use of all of the above-listed assignment operators.
#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 are used to compare 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.
a == b
a != b
a > b
a < b
|>=||Greater than or equal to||
a >= b
|<=||Less than or equal to||
a <= b
Note. All of the comparison operators return Boolean value i.e. either
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.
#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 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.
|and||It returns True if both the statements are True.||
(5 < 10) and (20 < 40) #returns True
|or||It returns True if any one of the statements are True.||
(5 < 10) or (200 < 40) #returns True
|not||It returns True if a statement is not true.||
not (20 < 40) #returns True
Example. The following example illustrates the use of these logical operators.
#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 are used to check 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.
|is||It will only return True if two python objects are exactly the same objects.||
a is b
|is not||It will return True if two python objects are not exactly the same objects.||
a is not b
Example. Observe the following example carefully to understand the use of identity operators.
#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 are used to checking 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 tabular form. Both of these variables will return boolean values.
|in||returns True if a value is in the sequence||
a in b #b is a sequence object #like Python List
|not in||returns True if a value is not in the sequence||
a not in b
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.
#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)
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.
|&||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 the left by pushing zeros in from the right and the leftmost bit will fall off.|
|>>||ZERO FILL RIGHT SHIFT||The resultant bits will be shifted to the right by pushing the leftmost bit copies to the left and the rightmost bit will fall off.|
Example. The following example illustrates the use of bitwise operators in python.
#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)