Python - Operators
Python provides various types of operators, including arithmetic, comparison, assignment, logical, bitwise, identity, and membership operators. Here's an overview of each type:
| Arithmetic Operators |
| Operator |
Description |
Example |
Results |
| + |
Addition |
5 + 3 |
8 |
| - |
Subtraction |
5 - 3 |
2 |
| * |
Multiplication |
5 * 3 |
15 |
| / |
Division |
5 / 3 |
1.6667 |
| // |
Floor Division |
5 // 3 |
1 |
| // |
Modulus |
5 % 3 |
2 |
| ** |
Exponentiation |
5 ** 3 |
125 |
| Comparison Operators |
| Operator |
Description |
Example |
Results |
| == |
Equal to |
5 + 3 |
8 |
| - |
Subtraction |
5 == 3 |
False |
| != |
Not equal to |
5 != 3 |
True |
| > |
Greater than |
5 > 3 |
True |
| < |
Less than |
5 < 3 |
False |
| >= |
Greater than or equal to |
5 >= 3 |
True |
| <= |
Less than or equal to |
5 <= 3 |
False |
| Assignment Operators |
| Operator |
Description |
Example |
Results |
| = |
Assign value |
x = 5 |
x (contain 5) |
| += |
Add and assign |
x += 3 |
x = x + 3 |
| -= |
Subtract and assign |
x -= 3 |
x = x - 3 |
| *= |
Multiply and assign |
x *= 3 |
x = x * 3 |
| /= |
Divide and assign |
x /= 3 |
x = x / 3 |
| //= |
Floor divide and assign |
x //= 3 |
x = x // 3 |
| %= |
Modulus and assign |
x %= 3 |
x = x % 3 |
| **= |
Exponentiate and assign |
x **= 3 |
x = x ** 3 |
| Logical Operators |
| Operator |
Description |
Example |
Results |
| and |
Logical AND |
True and False |
False |
| or |
Logical OR |
True or False |
True |
| not |
Logical NOT |
not True |
False |
| Bitwise Operators |
| Operator |
Description |
| & |
Bitwise AND |
| | |
Bitwise OR |
| ^ |
Bitwise XOR |
| ~ |
Bitwise NOT |
| << |
Left shift |
| >> |
Right shift |
| Identity Operators |
| Operator |
Description |
Example |
| is |
Evaluates if both sides have the same identity |
x is y |
| is not |
Evaluates if both sides have different identities |
x is not y |
| Membership Operators |
| Operator |
Description |
Example |
Result |
| is |
Evaluates if a value is present in a sequence (list, tuple, string, etc.) |
5 in [1, 2, 3] |
False |
| is not |
Evaluates if a value is not present in a sequence |
5 not in [1, 2, 3] |
True |
• Utilizes various Python operators:
py Copy Code
# Arithmetic operators example
x = 10
y = 5
print("Arithmetic Operators Example:")
print("Addition:", x + y)
print("Subtraction:", x - y)
print("Multiplication:", x * y)
print("Division:", x / y)
print("Modulus:", x % y)
print("Exponentiation:", x ** y)
print()
# Comparison operators example
a = 10
b = 20
print("Comparison Operators Example:")
print("Greater than:", a > b)
print("Less than:", a < b)
print("Equal to:", a == b)
print()
# Logical operators example
c = True
d = False
print("Logical Operators Example:")
print("Logical AND:", c and d)
print("Logical OR:", c or d)
print("Logical NOT:", not c)
print()
# Bitwise operators example
e = 5
f = 3
print("Bitwise Operators Example:")
print("Bitwise AND:", e & f)
print("Bitwise OR:", e | f)
print("Bitwise XOR:", e ^ f)
print("Bitwise NOT:", ~e)
print("Left shift:", e << 1)
print("Right shift:", e >> 1)
print()
# Identity operators example
g = [1, 2, 3]
h = [1, 2, 3]
print("Identity Operators Example:")
print("Identity:", g is h)
print("Not Identity:", g is not h)
print()
# Membership operators example
i = 2
j = [1, 2, 3]
print("Membership Operators Example:")
print("Membership:", i in j)
print("Not Membership:", i not in j)
Output:
Arithmetic Operators Example:
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0
Modulus: 0
Exponentiation: 100000
Comparison Operators Example:
Greater than: False
Less than: True
Equal to: False
Logical Operators Example:
Logical AND: False
Logical OR: True
Logical NOT: False
Bitwise Operators Example:
Bitwise AND: 1
Bitwise OR: 7
Bitwise XOR: 6
Bitwise NOT: -6
Left shift: 10
Right shift: 2
Identity Operators Example:
Identity: False
Not Identity: True
Membership Operators Example:
Membership: True
Not Membership: False
• This program demonstrates the usage of various Python operators with examples, and it covers arithmetic, comparison, logical, bitwise, identity, and membership operators.