Python Operators
Special symbols or keywords are used in Python to perform operations on variables and values. Different types of operators are supported in Python, and each type has a different function.
Let’s go through them one by one:
1. Arithmetic Operators
These operators are used to perform mathematical operations.
Operator | Description | Example |
---|---|---|
+ | Addition | 5 + 3 = 8 |
- | Subtraction | 5 - 3 = 2 |
* | Multiplication | 5 * 3 = 15 |
/ | Division (float) | 5 / 2 = 2.5 |
// | Floor Division | 5 // 2 = 2 |
% | Modulus (remainder) | 5 % 2 = 1 |
** | Exponentiation | 2 ** 3 = 8 |
Example:
a = 10
b = 3
print(a + b) # Addition
print(a - b) # Subtraction
print(a * b) # Multiplication
print(a / b) # Division
print(a // b) # Floor Division
print(a % b) # Modulus
print(a ** b) # Exponentiation
2. Comparison (Relational) Operators
These operators compare values and return a boolean (True
or False
).
Operator | Description | Example |
---|---|---|
== | Equal to | 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 |
Example:
a = 10
b = 5
print(a > b) # True
print(a < b) # False
print(a == b) # False
print(a != b) # True
print(a >= b) # True
print(a <= b) # False
3. Logical Operators
These are used to combine conditional statements.
Operator | Description | Example |
---|---|---|
and | Logical AND | (5 > 3) and (3 > 1) → True |
or | Logical OR | (5 > 3) or (3 < 1) → True |
not | Logical NOT | not(5 > 3) → False |
Example:
x = 5
y = 3
z = 8
print((x > y) and (z > x)) # True
print((x > y) or (z < x)) # True
print(not(x > y)) # False
4. Assignment Operators
Used to assign values to variables, and combine assignment with arithmetic.
Operator | Example | Equivalent To |
---|---|---|
= | x = 5 | Assign value 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 |
Example:
x = 5
x += 3 # Equivalent to x = x + 3
print(x) # 8
x *= 2 # Equivalent to x = x * 2
print(x) # 16
5. Bitwise Operators
Operate on binary representations of integers.
Operator | Description | Example |
---|---|---|
& | Bitwise AND | 5 & 3 → 1 |
` | ` | Bitwise OR |
^ | Bitwise XOR | 5 ^ 3 → 6 |
~ | Bitwise NOT | ~5 → -6 |
<< | Left Shift | 5 << 1 → 10 |
>> | Right Shift | 5 >> 1 → 2 |
Example:
a = 5 # Binary: 0101
b = 3 # Binary: 0011
print(a & b) # AND → 0001 → 1
print(a | b) # OR → 0111 → 7
print(a ^ b) # XOR → 0110 → 6
print(~a) # NOT → -(5+1) → -6
print(a << 1) # Left Shift → 1010 → 10
print(a >> 1) # Right Shift → 0010 → 2
6. Membership Operators
These test whether a value is a member of a sequence.
Operator | Description | Example |
---|---|---|
in | Returns True if present | 'a' in 'apple' → True |
not in | Returns True if not present | 'b' in 'apple' → False |
Example:
text = "hello"
print('h' in text) # True
print('z' not in text) # True
7. Identity Operators
These check if two objects share the same memory location.
Operator | Description | Example |
---|---|---|
is | True if identical objects | x is y |
is not | True if not identical | x is not y |
Example:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True (b points to the same object as a)
print(a is c) # False (different objects with same content)
print(a == c) # True (contents are the same)
8. Special Operators
None
is used for null values.Ternary operator
for conditional assignment:
x = 10
result = "Even" if x % 2 == 0 else "Odd"
print(result) # Output: Even