- Arithmetic operators
- Comparison operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
1. Arithmetic operators
+ (addition)
- (subtraction)
*(multiplication)
/(divide), %(reminder)
//(floor division)
exponent (**) operators.
Operator | Name | Example |
+ | Addition | x + y |
– | Subtraction | x – y |
* | Multiplication | x * y |
/ | Division | x / y |
% | Modulus | x % y |
** | Exponentiation | x ** y |
// | Floor division | x // y |
# Arithmetic Operator
a = 9
b = 4
# Addition
a1 = a + b
# Subtraction
a2 = a - b
# Multiplication
a3 = a * b
# Division(float)
a4 = a / b
# Division(floor)
a5 = a // b
# Modulo of both number
a6 = a % b
# Power
a7 = a ** b
# print results
print(a1)
print(a2)
print(a3)
print(a4)
print(a5)
print(a6)
print(a7)
Output
13
5
36
2.25
2
1
6561
2. Comparison operators
Operator | Name | Example |
== | Equal | x == y |
!= | Not equal | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
# Relational Operators
a = 13
b = 33
# a > b is False
print(a > b)
# a < b is True
print(a < b)
# a == b is False
print(a == b)
# a != b is True
print(a != b)
# a >= b is False
print(a >= b)
# a <= b is True
print(a <= b)
Output
False
True
False
True
False
True
3. Assignment Operators
Operator | Example | Same As |
= | x = 5 | x = 5 |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x – 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
%= | x %= 3 | x = x % 3 |
//= | x //= 3 | x = x // 3 |
**= | x **= 3 | x = x ** 3 |
&= | x &= 3 | x = x & 3 |
|= | x |= 3 | x = x | 3 |
^= | x ^= 3 | x = x ^ 3 |
>>= | x >>= 3 | x = x >> 3 |
<<= | x <<= 3 | x = x << 3 |
# Assignment Operators
a = 10
# Assign value
b = a
print(b)
# Add and value
b += a
print(b)
# Subtract and value
b -= a
print(b)
# multiply and assign
b *= a
print(b)
# bitwise lishift operator
b <<= a
print(b)
Output
10
20
10
100
102400
4. Logical Operators
Operator | Description | Example |
and | Returns True if both statements are true | x < 5 and x < 10 |
or | Returns True if one of the statements is true | x < 5 or x < 4 |
not | Reverse the result, returns False if the result is true | not(x < 5 and x < 10) |
# Logical Operator
a = True
b = False
# Print a and b is False
print(a and b)
# Print a or b is True
print(a or b)
# Print not a is False
print(not a)
Output:
False
True
False
5. Bitwise Operators
Operator | Name | Description |
& | AND | Sets each bit to 1 if both bits are 1 |
| | OR | Sets each bit to 1 if one of two bits is 1 |
^ | XOR | Sets each bit to 1 if only one of two bits is 1 |
~ | NOT | Inverts all the bits |
<< | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off |
>> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
# Bitwise operators
a = 10
b = 4
# AND operation
print(a & b)
# OR operation
print(a | b)
# NOT operation
print(~a)
# XOR operation
print(a ^ b)
# right shift operation
print(a >> 2)
# left shift operation
print(a << 2)
Output:
0
14
-11
14
2
40
6. Membership Operators
Operator | Description | Example |
in | Returns True if a sequence with the specified value is present in the object | x in y |
not in | Returns True if a sequence with the specified value is not present in the object | x not in y |
x = 24
y = 20
list = [15, 20, 35, 40, 50]
if (x not in list):
print("x is NOT present in given list")
else:
print("x is present in given list")
if (y in list):
print("y is present in given list")
else:
print("y is NOT present in given list")
Output
x is NOT present in given list
y is present in given list
7. Identity Operators
Operator | Description | Example |
is | Returns True if both variables are the same object | x is y |
is not | Returns True if both variables are not the same object | x is not y |
a = 10
b = 20
c = a
print(a is not b)
print(a is c)
Output:
True
True
Keywords:
What is the operator in Python?
What are the 3 operators in Python?
What are the 4 operators in Python?
How many types of operators are in Python?
Arithmetic operators,
Comparison operators,
Assignment Operators,
Logical Operators,
Bitwise Operators,
Membership Operators,
Identity Operators,