Arithmetic operators
print(7 + 3, 7 - 3, 7 * 3)
print(7 / 2) # true division -> float
print(7 // 2) # floor division -> int
print(7 % 2) # remainder (modulo)
print(2 ** 10) # power
print(-7 // 2) # floors toward negative infinity
print(divmod(17, 5))The two divisions confuse most beginners: / always produces a float even when the answer is whole (6 / 3 is 2.0), and // throws the fractional part away. The modulo % is how you test "is this number even?" — n % 2 == 0.
Assignment shortcuts
count = 0
count += 1 # count = count + 1
count *= 5
count -= 2
count //= 2
print(count)
text = "ab"
text += "c" # works on strings and lists too
print(text)Comparison operators
print(5 == 5, 5 != 3)
print(5 > 3, 5 < 3, 5 >= 5, 5 <= 4)
print(1 < 2 < 3) # chained comparison
print(0.1 + 0.2 == 0.3) # floating-point surprise
print(abs((0.1 + 0.2) - 0.3) < 1e-9) # the safe wayComparisons always produce True or False. Do not confuse == (equal?) with = (assign) — it is the most common syntax error in an if statement.
Logical operators: and, or, not
age, member = 20, True
print(age >= 18 and member)
print(age < 18 or member)
print(not member)
# short-circuit: the right side is skipped when not needed
x = 0
print(x != 0 and 10 / x > 1) # no ZeroDivisionError
# and / or return the deciding value, not always a bool
print("" or "default")
print("value" and "second")The last two lines show a useful trick: name = user_input or "Guest" gives a fallback when the input is empty.
Truthiness
Every value can be used where a boolean is expected. These are falsy: False, None, 0, 0.0, "", [], {}, set(). Everything else is truthy, which lets you write if items: instead of if len(items) > 0:.
Operator precedence
From highest to lowest: ** → unary - → * / // % → + - → comparisons → not → and → or. When in doubt, add parentheses; they cost nothing and make intent obvious.
print(2 + 3 * 4) # 14, not 20
print((2 + 3) * 4) # 20
print(-2 ** 2) # -4 (power binds first)
print((-2) ** 2) # 4
print(not True or True) # True: not binds tighter than orThe math module and useful built-ins
import math
print(round(3.14159, 2), abs(-8), max(3, 9, 1), min([4, 2, 7]))
print(sum([1, 2, 3, 4]), pow(2, 8))
print(math.sqrt(16), math.floor(3.9), math.ceil(3.1))
print(math.pi, math.e)
print(math.gcd(12, 18), math.factorial(5))
print(int("ff", 16), bin(10), hex(255))Now that you can compute and compare, the next lesson uses those results to make decisions with if, elif and else.
Frequently asked questions
What is the difference between / and // in Python?
/ is true division and always returns a float (7 / 2 = 3.5). // is floor division and returns the whole part rounded down (7 // 2 = 3, -7 // 2 = -4).
What does % do in Python?
% is the modulo operator: it returns the remainder after division. 17 % 5 is 2. It is commonly used to test even/odd numbers and to wrap values around a range.
How do I round a number in Python?
round(x, n) rounds to n decimal places. Note Python uses banker's rounding for exact halves: round(2.5) is 2 and round(3.5) is 4.
Is there an increment operator ++ in Python?
No. Use x += 1 instead. Writing x++ is a syntax error and ++x simply evaluates to x.