Operators in Python are special symbols used to perform operations on variables and values. Python provides several categories of operators, including:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Identity Operators
- Membership Operators
- Bitwise Operators
1. Arithmetic Operators
Arithmetic operators perform basic mathematical operations:
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 5 / 3 | 1.666 |
// | Floor division | 5 // 3 | 1 |
% | Modulus (remainder) | 5 % 3 | 2 |
** | Exponentiation | 5 ** 3 | 125 |
x = 10
y = 4
print(x + y) # 14
print(x - y) # 6
print(x * y) # 40
print(x / y) # 2.5
print(x // y) # 2
print(x % y) # 2
print(x ** y) # 10000
2. Assignment Operators
Assignment operators are used to assign values to variables:
Operator | Example | Equivalent |
---|---|---|
= | 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 = 10
x += 5 # x becomes 15
print(x)
x *= 2 # x becomes 30
print(x)
3. Comparison Operators
Comparison operators compare two values and return a boolean result (True
or False
):
Operator | Meaning | Example | Result |
---|---|---|---|
== | Equal | 5 == 5 | True |
!= | Not equal | 5 != 3 | True |
> | Greater than | 5 > 3 | True |
< | Less than | 5 < 3 | False |
>= | Greater than or equal | 5 >= 5 | True |
<= | Less than or equal | 5 <= 3 | False |
print(10 == 10) # True
print(10 != 5) # True
print(10 > 5) # True
print(5 < 2) # False
4. Logical Operators
Logical operators combine boolean expressions:
Operator | Description | Example | Result |
---|---|---|---|
and | True if both statements are True | (5 > 2) and (3 < 4) | True |
or | True if one statement is True | (5 < 2) or (3 < 4) | True |
not | Reverses the result | not(5 > 2) | False |
print((4 > 2) and (5 > 3)) # True
print((4 > 6) or (5 > 3)) # True
print(not(4 > 2)) # False
5. Identity Operators
Identity operators determine if two variables point to the same object:
Operator | Description | Example | Result |
---|---|---|---|
is | True if both variables are same object | x is y | True or False |
is not | True if both variables are not same object | x is not y | True or False |
x = [1, 2, 3]
y = [1, 2, 3]
z = x
print(x is y) # False (different objects)
print(x is z) # True (same object)
print(x is not y) # True
6. Membership Operators
Membership operators check if a value exists within an object (e.g., string, list, tuple):
Operator | Description | Example | Result |
---|---|---|---|
in | True if value is found | 3 in [1, 2, 3] | True |
not in | True if value is not found | 4 not in [1,2,3] | True |
numbers = [1, 2, 3, 4]
print(2 in numbers) # True
print(5 not in numbers) # True
7. Bitwise Operators
Bitwise operators perform operations at the bit-level:
Operator | Meaning | Example | Result (binary) |
---|---|---|---|
& | Bitwise AND | 5 & 3 | 1 (0101 & 0011 = 0001) |
| | | | 5 | 3 | 7 |
^ | Bitwise XOR | 5 ^ 3 | 6 (0101 ^ 0011 = 0110) |
~ | Bitwise NOT | ~5 | -6 |
<< | Left Shift | 5 << 2 | 20 (0101 → 10100) |
>> | Right Shift | 5 >> 2 | 1 (0101 → 0001) |
print(5 & 3) # 1
print(5 | 3) # 7
print(5 ^ 3) # 6
print(~5) # -6
print(5 << 2) # 20
print(5 >> 2) # 1