Phase: 1. Fundamentals | Estimated time: 2 hours | Milestone Project: No
By the end of this module, you will be able to:
- Use all arithmetic operators including
//,%, and** - Compare values using
==,!=,<,>,<=,>= - Combine conditions with
and,or,not - Use augmented assignment operators like
+=,-= - Predict evaluation order using operator precedence
Operators are the tools you use to compute, compare, and control. Every program — from a simple calculator to a complex AI model — relies on these fundamental operations. Mastering them gives you the vocabulary to express any computation.
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 5 - 3 |
2 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division (float) | 5 / 3 |
1.666... |
// |
Floor division | 5 // 3 |
1 |
% |
Modulo (remainder) | 5 % 3 |
2 |
** |
Exponentiation | 5 ** 3 |
125 |
Divides and rounds DOWN to the nearest integer:
print(10 // 3) # 3
print(-10 // 3) # -4 (rounds DOWN, not toward zero)
print(10.0 // 3) # 3.0 (float if any operand is float)Returns the remainder after division. Useful for checking even/odd:
print(10 % 3) # 1
print(15 % 5) # 0 (divisible)
print(7 % 2) # 1 (odd)
print(8 % 2) # 0 (even)Raises a number to a power:
print(2 ** 3) # 8
print(4 ** 0.5) # 2.0 (square root)
print(10 ** -1) # 0.1Comparison operators return True or False (boolean):
x = 10
y = 5
print(x == y) # False (equal to)
print(x != y) # True (not equal to)
print(x < y) # False
print(x > y) # True
print(x <= 10) # True
print(x >= 10) # TrueYou can chain comparisons:
age = 25
print(18 <= age < 65) # True (age is between 18 and 64 inclusive)and, or, not combine boolean values:
age = 20
has_id = True
print(age >= 18 and has_id) # True (both conditions True)
print(age >= 18 or has_id) # True (at least one is True)
print(not has_id) # False (negates)Truth table for and:
True and True → True
True and False → False
False and True → False
False and False → False
Truth table for or:
True or True → True
True or False → True
False or True → True
False or False → False
Beyond simple =, Python provides augmented assignment operators:
| Operator | Example | Equivalent to |
|---|---|---|
+= |
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 |
count = 10
count += 5 # count is now 15
count -= 3 # count is now 12
count *= 2 # count is now 24
print(count)Python follows the standard PEMDAS order. From highest to lowest precedence:
**(exponentiation)+x,-x(unary plus/minus)*,/,//,%(multiplication, division)+,-(addition, subtraction)==,!=,<,>,<=,>=(comparison)not(logical NOT)and(logical AND)or(logical OR)
result = 5 + 3 * 2 ** 3
# 2 ** 3 → 8
# 3 * 8 → 24
# 5 + 24 → 29
print(result) # 29Use parentheses to make precedence explicit:
result = (5 + 3) * 2 ** 3 # 8 * 8 → 64
print(result) # 64/vs//:10 / 3is3.333...,10 // 3is3. Know the difference.==vs=:if x = 5is an assignment, not a comparison. Use==for equality checks.- Chaining
and/orwithout parentheses:age > 18 and has_id or is_vipmay not mean what you think. Use parentheses:(age > 18 and has_id) or is_vip. - Forgetting that
%with negative numbers can surprise you:-5 % 3gives1, not-2. - Integer division with negatives:
-10 // 3gives-4(floor division rounds down).
Let's build a "Number Analyzer" program:
# Get a number from the user
num = int(input("Enter an integer: "))
# Arithmetic
print("Square:", num ** 2)
print("Cube:", num ** 3)
print("Square root (approx):", num ** 0.5)
# Even or odd?
print("Is even?", num % 2 == 0)
# Analysis
print("Positive?", num > 0)
print("Negative?", num < 0)
print("Zero?", num == 0)
# Range check
print("Between 1 and 100?", 1 <= num <= 100)
# Augmented assignment demo
total = 0
total += num
total += 10
print("Total after adding num and 10:", total)Example run with input 7:
Square: 49
Cube: 343
Square root (approx): 2.6457513110645907
Is even? False
Positive? True
Negative? False
Zero? False
Between 1 and 100? True
Total after adding num and 10: 17
- Arithmetic:
+,-,*,/,//(floor),%(modulo),**(power). - Comparison:
==,!=,<,>,<=,>=— all returnbool. - Logical:
and(both True),or(at least one True),not(negation). - Augmented assignment:
+=,-=,*=,/=,//=,%=,**=. - Operator precedence:
**> unary >*//////%>+/-> comparison >not>and>or. - Use parentheses to clarify precedence.
//rounds down (floor), not toward zero.%gives remainder;x % 2 == 0checks for even numbers.
Dive deeper into text manipulation in Module 007: Strings Deep Dive.