An expression is any piece of code that produces a value. Operators are the symbols that combine values into expressions.
Arithmetic operators
a, b = 17, 5
print(a + b, a - b, a * b) # 22 12 85
print(a / b) # 3.4
print(a // b) # 3
print(a % b) # 2
print(a ** b) # 1419857Comparison operators return booleans
print(3 < 5) # True
print(3 == 3.0) # True (equal value)
print(3 != 4) # True
print(2 <= 1) # False
# Python allows chaining:
x = 7
print(0 < x < 10) # TrueNote the difference between = (assignment) and == (comparison). Confusing them is the most common beginner bug.
Logical operators
sunny = True
warm = False
print(sunny and warm) # False
print(sunny or warm) # True
print(not sunny) # Falseand and or are short-circuiting: in a and b, if a is falsy Python never evaluates b. Falsy values are False, None, 0, 0.0, "", [], {} and (). Everything else is truthy.
Augmented assignment
count = 10
count += 5 # same as count = count + 5
count -= 3
count *= 2
print(count) # 24Precedence
Python follows mathematics: ** first, then unary minus, then * / // %, then + -, then comparisons, then not, then and, then or. When in doubt use parentheses — they cost nothing and make intent obvious.