Python Operators
The symbols that do the work — comparisons, logic, assignments.
Arithmetic
| Op | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | True division | 7 / 2 | 3.5 |
// | Floor division | 7 // 2 | 3 |
% | Modulo | 7 % 2 | 1 |
** | Exponent | 2 ** 8 | 256 |
Comparison
Python
x = 10
x == 10 # True — equal
x != 5 # True — not equal
x > 5 # True — greater than
x < 5 # False — less than
x >= 10 # True — greater or equal
x <= 5 # False — less or equal
# Python allows chaining:
1 < x < 20 # True — same as (1 < x) and (x < 20)
Logical
Python
True and False # False — both must be True
True or False # True — at least one must be True
not True # False
# Short-circuit: Python stops as soon as it knows the answer
0 and risky_function() # risky_function never called
True or risky_function() # risky_function never called
Assignment operators
Python
x = 10
x += 5 # x = 15
x -= 3 # x = 12
x *= 2 # x = 24
x /= 4 # x = 6.0
x //= 2 # x = 3.0
x **= 3 # x = 27.0
x %= 5 # x = 2.0
Identity and membership
Python
a = [1, 2, 3]
b = a
c = [1, 2, 3]
a is b # True — same object in memory
a is c # False — equal contents, different objects
a == c # True — equal contents
2 in a # True — membership check
5 not in a # True
== vs is
Use
== to compare values. Use is only to check identity (same object in memory). x is None is the Pythonic way to check for None — never x == None.