Operators are special symbols in Python that perform computations on values and variables. Python supports various types of operators, each serving a specific purpose in operations like arithmetic calculations, comparisons, logical evaluations, and more.
Types of Operators in Python
Python operators are categorized into the following types:
- Arithmetic Operators
- Comparison (Relational) Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Identity Operators
- Membership Operators
1. Arithmetic Operators
Arithmetic operators perform basic mathematical operations such as addition, subtraction, multiplication, etc.
Operator | Symbol | Example | Output |
---|---|---|---|
Addition | + | 5 + 3 | 8 |
Subtraction | - | 10 - 4 | 6 |
Multiplication | * | 6 * 3 | 18 |
Division | / | 10 / 2 | 5.0 |
Floor Division | // | 10 // 3 | 3 |
Modulus | % | 10 % 3 | 1 |
Exponentiation | ** | 2 ** 3 | 8 |
Example Code:
a = 10
b = 3
print(a + b) # Output: 13
print(a - b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.3333
print(a // b) # Output: 3
print(a % b) # Output: 1
print(a ** b) # Output: 1000 (10^3)
2. Comparison (Relational) Operators
Comparison operators compare two values and return a Boolean (True
or False
).
Operator | Symbol | Example | Output |
---|---|---|---|
Equal to | == | 5 == 5 | True |
Not equal to | != | 5 != 3 | True |
Greater than | > | 10 > 3 | True |
Less than | < | 4 < 7 | True |
Greater than or equal to | >= | 6 >= 6 | True |
Less than or equal to | <= | 5 <= 8 | True |
Example Code:
x = 10
y = 5
print(x == y) # False
print(x != y) # True
print(x > y) # True
print(x < y) # False
print(x >= y) # True
print(x <= y) # False
3. Logical Operators
Logical operators are used for combining multiple conditions.
Operator | Symbol | Example | Output |
---|---|---|---|
Logical AND | and | (5 > 3) and (10 > 5) | True |
Logical OR | or | (5 > 10) or (10 > 5) | True |
Logical NOT | not | not(5 > 3) | False |
Example Code:
x = True
y = False
print(x and y) # False
print(x or y) # True
print(not x) # False
4. Bitwise Operators
Bitwise operators perform operations at the binary level.
Operator | Symbol | Example (x=5, y=3) | Output |
---|---|---|---|
AND | & | 5 & 3 | 1 |
OR | | | 5 | 3 | 7 |
XOR | ^ | 5 ^ 3 | 6 |
NOT | ~ | ~5 | -6 |
Left Shift | << | 5 << 1 | 10 |
Right Shift | >> | 5 >> 1 | 2 |
Example Code:
a = 5 # Binary: 101
b = 3 # Binary: 011
print(a & b) # Output: 1 (001)
print(a | b) # Output: 7 (111)
print(a ^ b) # Output: 6 (110)
print(~a) # Output: -6
print(a << 1) # Output: 10 (1010)
print(a >> 1) # Output: 2 (10)
5. Assignment Operators
Assignment operators are used to assign values to variables.
Operator | Symbol | Example | Equivalent To |
---|---|---|---|
Assign | = | x = 10 | x = 10 |
Add and Assign | += | x += 5 | x = x + 5 |
Subtract and Assign | -= | x -= 2 | x = x - 2 |
Multiply and Assign | *= | x *= 3 | x = x * 3 |
Divide and Assign | /= | x /= 2 | x = x / 2 |
Floor Divide and Assign | //= | x //= 2 | x = x // 2 |
Modulus and Assign | %= | x %= 3 | x = x % 3 |
Exponent and Assign | **= | x **= 2 | x = x ** 2 |
Example Code:
x = 10
x += 5
print(x) # Output: 15
x *= 2
print(x) # Output: 30
6. Identity Operators
Identity operators check if two variables reference the same object in memory.
Operator | Symbol | Example | Output |
---|---|---|---|
is | is | x is y | True or False |
is not | is not | x is not y | True or False |
Example Code:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True (Same object)
print(a is c) # False (Different objects)
print(a is not c) # True
7. Membership Operators
Membership operators check if a value is present in a sequence (list, tuple, string, etc.).
Operator | Symbol | Example | Output |
---|---|---|---|
in | in | 5 in [1, 2, 3, 5] | True |
not in | not in | 7 not in [1, 2, 3] | True |
Example Code:
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits) # True
print("grape" not in fruits) # True