Arithmetic Operators
Basic Arithmetic
# Addition
5 + 3 # 8
5.5 + 2.5 # 8.0
# Subtraction
10 - 4 # 6
# Multiplication
4 * 3 # 12
'ha' * 3 # 'hahaha' (string repetition)
# Division (always returns float)
10 / 3 # 3.3333333333333335
10 / 5 # 2.0 (still float!)
# Floor Division (integer division)
10 // 3 # 3
-10 // 3 # -4 (rounds toward negative infinity)
# Modulo (remainder)
10 % 3 # 1
-10 % 3 # 2 (sign follows divisor)
# Exponentiation
2 ** 10 # 1024
9 ** 0.5 # 3.0 (square root)
Division Gotchas
# ⚠️ Floor division vs truncation
import math
-7 // 2 # -4 (floor division)
math.trunc(-7 / 2) # -3 (truncation)
# ⚠️ Modulo with negative numbers
-7 % 2 # 1 (not -1!)
# Because: -7 = (-4) * 2 + 1
Operator Precedence (High to Low)
| Operator | Description |
|---|---|
** |
Exponentiation |
+x, -x, ~x |
Unary plus, minus, bitwise NOT |
*, /, //, % |
Multiplication, division, etc. |
+, - |
Addition, subtraction |
<<, >> |
Bitwise shifts |
& |
Bitwise AND |
^ |
Bitwise XOR |
| ` | ` |
Comparison Operators
Equality and Identity
# Equality (==) compares values
5 == 5 # True
'5' == 5 # False (different types)
[1, 2] == [1, 2] # True
# Identity (is) compares memory addresses
a = [1, 2, 3]
b = [1, 2, 3]
a == b # True (same value)
a is b # False (different objects)
a = b # Now a references same object as b
a is b # True
Chained Comparisons
# Python allows chaining
x = 5
# Instead of:
if x > 0 and x < 10:
pass
# You can write:
if 0 < x < 10:
pass
# Works with any comparison
if 0 <= x < y <= 100:
pass
Comparison Operators Table
# a = 5, b = 3
a == b # False (equal)
a != b # True (not equal)
a > b # True (greater than)
a < b # False (less than)
a >= b # True (greater or equal)
a <= b # False (less or equal)
Comparing Different Types
# ⚠️ Comparing different types
'hello' > 5 # TypeError in Python 3
# (Python 2 would return True)
# None comparisons
None == None # True
None is None # True (preferred)
None < 5 # TypeError
Comparing Strings
# Lexicographic comparison
'apple' < 'banana' # True
'abc' < 'abd' # True
'abc' < 'ab' # False (longer string is 'greater')
# Case matters
'Apple' < 'apple' # True (uppercase < lowercase)
Logical & Assignment Operators
Logical Operators
# and - returns first falsy or last value
True and True # True
True and False # False
False and True # False
False and False # False
# or - returns first truthy or last value
True or True # True
True or False # True
False or True # True
False or False # False
# not - returns opposite boolean
not True # False
not False # True
Short-Circuit Evaluation
# 'and' short-circuits on first falsy
def expensive_func():
print('Called!')
return True
False and expensive_func() # 'Called!' is NOT printed
# 'or' short-circuits on first truthy
True or expensive_func() # 'Called!' is NOT printed
# Common pattern: default values
name = user_input or 'Anonymous'
# Another pattern: guard clauses
if items and items[0] == 'special':
process(items[0])
Walrus Operator (:=)
# Assignment expression (Python 3.8+)
import random
# Instead of:
num = random.randint(1, 10)
if num > 5:
print(f'Big number: {num}')
# You can write:
if (num := random.randint(1, 10)) > 5:
print(f'Big number: {num}')
# Useful in while loops
while (line := input('Enter: ')) != 'quit':
print(f'You entered: {line}')
Assignment Operators
x = 10
x += 5 # x = x + 5 → 15
x -= 3 # x = x - 3 → 12
x *= 2 # x = x * 2 → 24
x /= 4 # x = x / 4 → 6.0
x //= 2 # x = x // 2 → 3.0
x %= 2 # x = x % 2 → 1.0
x **= 3 # x = x ** 3 → 1.0
# Bitwise assignment
x = 0b1010
x &= 0b1100 # x = x & 0b1100
x |= 0b0001 # x = x | 0b0001
x ^= 0b1111 # x = x ^ 0b1111
x >>= 1 # x = x >> 1
x <<= 2 # x = x << 2
Identity Operators
# is / is not - compare memory addresses
a = [1, 2, 3]
b = [1, 2, 3]
c = a
a is b # False (different objects)
a is c # True (same object)
a is not b # True
# Use 'is' for singletons
if x is None:
pass
if x is not None:
pass