Python Operators
Operator Categories
Python provides different types of operators for various operations:
Category | Operators |
---|---|
Arithmetic | +, -, *, /, //, %, ** |
Comparison | ==, !=, >, <, >=, <= |
Logical | and, or, not |
Assignment | =, +=, -=, *=, /=, %=, **=, //= |
Bitwise | &, |, ^, ~, <<, >> |
Identity | is, is not |
Membership | in, not in |
Arithmetic Operators
Example
# Arithmetic examples
a, b = 10, 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.333...
print(a // b) # 3 (floor division)
print(a % b) # 1 (modulus)
print(a ** b) # 1000 (exponentiation)
Output
13 7 30 3.3333333333333335 3 1 1000
ℹ️ Note: Division (`/`) always returns a float in Python 3. Use `//` for integer division.
Comparison and Logical Operators
Example
# Comparison operators
x, y = 5, 10
print(x == y) # False
print(x != y) # True
print(x < y) # True
# Logical operators
age = 25
is_student = True
print(age > 18 and is_student) # True
print(age < 18 or not is_student) # False
Output
False True True True False
Assignment Operators
Assignment operators combine assignment with arithmetic or bitwise operations.
Example
x = 5
x += 3 # Same as x = x + 3
print(x) # 8
x *= 2 # Same as x = x * 2
print(x) # 16
x **= 2 # Same as x = x ** 2
print(x) # 256
Output
8 16 256
Bitwise Operators
Bitwise operators perform operations on the binary representation of numbers.
Example
a, b = 0b1100, 0b1010
print(bin(a & b)) # AND: 0b1000
print(bin(a | b)) # OR: 0b1110
print(bin(a ^ b)) # XOR: 0b110
print(bin(~a)) # NOT: -0b1101
print(bin(a << 2)) # Left shift: 0b110000
print(bin(b >> 1)) # Right shift: 0b0101
Output
0b1000 0b1110 0b110 -0b1101 0b110000 0b101
ℹ️ Note: Useful for flags, masks, and low-level data manipulation.
Identity and Membership Operators
⚠️ Warning: `is` checks if two references point to the same object, while `==` checks if values are equal.
Example
# Identity operators (compare memory locations)
list1 = [1,2,3]
list2 = [1,2,3]
print(list1 is list2) # False
print(list1 is not list2) # True
# Membership operators
fruits = ['apple', 'banana', 'cherry']
print('banana' in fruits) # True
print('orange' not in fruits) # True
Output
False True True True
Operator Precedence
Operators are evaluated according to precedence rules, unless overridden by parentheses.
Example
# Example of precedence
result = 5 + 3 * 2 ** 2 # 5 + (3 * (2 ** 2)) = 17
print(result)
Output
17
ℹ️ Note: Use parentheses to make order of operations explicit and readable.
Precedence | Operators |
---|---|
Highest | () (parentheses) |
** (exponentiation) | |
~ + - (unary) | |
* / % // | |
+ - | |
>> << | |
& | |
^ | | |
== != > < >= <= | |
is, is not | |
in, not in | |
not | |
and | |
Lowest | or |
Special Operators
Example
# Walrus operator (Python 3.8+)
if (n := len('hello')) > 4:
print(f"Length is {n}")
# Ternary operator
age = 20
status = "Adult" if age >= 18 else "Minor"
# Matrix multiplication (Python 3.5+)
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
print(a @ b) # Matrix product
Output
Length is 5 [[19 22] [43 50]]