Types of Python Operators
1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. Bitwise Operators
6. Special Operators
Python Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction,
multiplication, etc. For example,
Operator Operation Example
+ Addition 5 + 2 = 7
- Subtraction 4 - 2 = 2
* Multiplication 2 * 3 = 6
/ Division 4 / 2 = 2
// Floor Division 10 // 3 = 3
% Modulo 5 % 2 = 1
** Power 4 ** 2 = 16
Example 1: Arithmetic Operators in Python
a=7 print ('Modulo: ', a % b) In the above example, we have used multiple
b=2 arithmetic operators,
# a to the power b
# addition • + to add a and b
print ('Power: ', a ** b)
print ('Sum: ', a + b) • - to subtract b from a
Run Code
# subtraction • * to multiply a and b
Output
print ('Subtraction: ', a - b) • / to divide a by b
Sum: 9
# multiplication • // to floor divide a by b
Subtraction: 5
print ('Multiplication: ', a * b) • % to get the remainder
Multiplication: 14
# division
• ** to get a to the power b
Division: 3.5
print ('Division: ', a / b)
Floor Division: 3
# floor division
Modulo: 1
print ('Floor Division: ', a // b)
# modulo Power: 49
2. Python Assignment Operators
Here's a list of different assignment operators available in Python.
Operator Name Example
= Assignment Operator a=7
+= Addition Assignment a += 1 # a = a + 1
-= Subtraction Assignment a -= 3 # a = a - 3
*= Multiplication Assignment a *= 4 # a = a * 4
/= Division Assignment a /= 3 # a = a / 3
%= Remainder Assignment a %= 10 # a = a % 10
**= Exponent Assignment a **= 10 # a = a ** 10
Example 2: Assignment Operators
# assign 10 to a 3. Python Comparison Operators
a = 10 Comparison operators compare two values/variables and return a boolean
result: True or False. For example,
# assign 5 to b
b=5 Operator Meaning Example
# assign the sum of a and b to a
== Is Equal To 3 == 5 gives us False
a += b #a=a+b
print(a)
!= Not Equal To 3 != 5 gives us True
# Output: 15
> Greater Than 3 > 5 gives us False
< Less Than 3 < 5 gives us True
>= Greater Than or Equal To 3 >= 5 give us False
<= Less Than or Equal To 3 <= 5 gives us True
4. Python Logical Operators
Logical operators are used to check whether an expression is True or False. They are used in decision-making. For
example,
Operator Example Meaning
Logical AND:
and a and b
True only if both the operands are True
Logical OR:
or a or b
True if at least one of the operands is True
Logical NOT:
not not a
True if the operand is False and vice-versa.
# logical AND
print(True and True) # True
print(True and False) # False
# logical OR
print(True or False) # True
# logical NOT
print(not True) # False
5. Python Bitwise operators
Bitwise operators act on operands as if they were strings of binary digits. They operate bit by bit, hence the name.
For example, 2 is 10 in binary, and 7 is 111.
In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)
Operator Meaning Example
& Bitwise AND x & y = 0 (0000 0000)
| Bitwise OR x | y = 14 (0000 1110)
~ Bitwise NOT ~x = -11 (1111 0101)
^ Bitwise XOR x ^ y = 14 (0000 1110)
>> Bitwise right shift x >> 2 = 2 (0000 0010)
<< Bitwise left shift x <<2=40 (0010 1000)
6. Python Special operators
Python language offers some special types of operators like the identity operator and the membership operator.
They are described below with examples.
Identity operators
In Python,
is
is not
are used to check if two values are located at the same memory location.
It's important to note that having two variables with equal values doesn't necessarily mean they are
identical.
Operator Meaning Example
True if the operands are identical (refer to the
is x is True
same object)
True if the operands are not identical (do not refer x is not
is not
to the same object) True
x1 = 5
Here, we see that x1 and y1 are integers of the same values,
y1 = 5
so they are equal as well as identical. The same is the case
x2 = 'Hello' with x2 and y2 (strings).
y2 = 'Hello' But x3 and y3 are lists. They are equal but not identical. It is
because the interpreter locates them separately in memory,
x3 = [1,2,3]
although they are equal.
y3 = [1,2,3]
print(x1 is not y1) # prints False
print(x2 is y2) # prints True
print(x3 is y3) # prints False
7.Membership operators
In Python,
in
not in
are the membership operators. They are used to test whether a value or variable is found in a sequence
(string, list, tuple, set and dictionary).
In a dictionary, we can only test for the presence of a key, not the value.
Operator Meaning Example
True if value/variable is found in the
in 5 in x
sequence
True if value/variable is not found in the
not in 5 not in x
sequence
message = 'Hello world'
dict1 = {1:'a', 2:'b'}
# check if 'H' is present in message string
print('H' in message) # prints True
# check if 'hello' is present in message string
print('hello' not in message) # prints True
# check if '1' key is present in dict1
print(1 in dict1) # prints True
# check if 'a' key is present in dict1
print('a' in dict1) # prints False
Run Code
Output
True
True
True
False
Here, 'H' is in message, but 'hello' is not present in message (remember, Python is case-sensitive).
Similarly, 1 is key, and 'a' is the value in dictionary dict1. Hence, 'a' in y returns False.