Lesson 5 of 30
Operators and Expressions
Arithmetic, comparison, logical, and assignment operators in Python.
Arithmetic Operators
| Operator | Symbol | Example | Result |
|---|---|---|---|
| Addition | + | 7 + 3 | 10 |
| Subtraction | - | 7 - 3 | 4 |
| Multiplication | * | 7 * 3 | 21 |
| Division | / | 7 / 3 | 2.333… |
| Floor division | // | 7 // 3 | 2 |
| Modulus | % | 7 % 3 | 1 |
| Exponentiation | ** | 2 ** 8 | 256 |
Comparison Operators
x = 10
print(x == 10) # True
print(x != 5) # True
print(x > 8) # True
print(x <= 10) # True
Logical Operators
a, b = True, False
print(a and b) # False
print(a or b) # True
print(not a) # False
Assignment Operators
n = 10
n += 5 # n = 15
n -= 3 # n = 12
n *= 2 # n = 24
n //= 4 # n = 6
n **= 2 # n = 36
Operator Precedence
Python follows standard mathematical precedence: PEMDAS — Parentheses, Exponentiation, Multiplication/Division, Addition/Subtraction.
result = 2 + 3 * 4 # 14, not 20
result = (2 + 3) * 4 # 20