Operators, Expressions & Precedence
Operators and operands
An operator is a symbol that performs an operation; the values it works on are operands. In marks + 5, + is the operator and marks and 5 are operands. An operator with one operand (like -x or not x) is unary; with two it is binary.
Arithmetic operators
| Operator | Meaning | 17 ? 5 |
|---|---|---|
+ | Addition | 22 |
- | Subtraction | 12 |
* | Multiplication | 85 |
/ | Division (always float) | 3.4 |
// | Floor division (quotient) | 3 |
% | Modulus (remainder) | 2 |
** | Exponent (power) | 17**2 = 289 |
a, b = 17, 5 print(a + b, a - b, a * b) print(a / b, a // b, a % b, a ** 2) print(-17 // 5, -17 % 5) # floor division rounds DOWN
n % 2 == 0 checks even numbers; n % 10 gives the last digit; n // 10 removes the last digit. + also joins strings ('Py' + 'thon') and * repeats them ('-' * 20).
Relational (comparison) operators
They compare two values and always give True or False.
a, b = 17, 5
print(a > b, a < b, a == 17, a != b, a >= 17, b <= 4)
print('apple' < 'banana', 'Zoo' < 'apple') # compared by Unicode codesStrings are compared letter by letter using their Unicode codes; capital letters (65–90) come before small letters (97–122), so 'Zoo' < 'apple' is True.
Assignment operators
A compound assignment like x += 5 is a short form of x = x + 5.
x = 10 x += 5; print(x) x -= 3; print(x) x *= 2; print(x) x //= 5; print(x) x **= 3; print(x) x %= 10; print(x)
Logical operators
| A | B | A and B | A or B | not A |
|---|---|---|---|---|
| True | True | True | True | False |
| True | False | False | True | False |
| False | True | False | True | True |
| False | False | False | False | True |
age, has_id = 20, False print(age >= 18 and has_id) # both must be True print(age >= 18 or has_id) # any one True is enough print(not has_id) print(0 and 5, 3 and 5, 0 or 'default', '' or 'guest')
Short-circuit: and stops at the first false value; or stops at the first true value, and they return that value itself. That is why '' or 'guest' gives 'guest' — handy for default values.
Identity and membership operators
L = [1, 2, 3]
M = L
N = [1, 2, 3]
print(M is L, N is L) # identity: same object?
print(N == L, N is not L) # equality: same value?
print('a' in 'class', 5 in [1, 2, 3], 'z' not in 'python')== (equality)
- Are the values the same?
- N == L → True
- Use for comparing data
is (identity)
- Is it the same object?
- N is L → False (two separate lists)
- Use mainly for x is None
Bitwise operators
They work on the binary digits of integers: & AND, | OR, ^ XOR, ~ NOT, << left shift, >> right shift.
print(12 & 10, 12 | 10, 12 ^ 10) # 1100 and 1010
12 = 1100 and 10 = 1010 → AND 1000 = 8, OR 1110 = 14, XOR 0110 = 6.
Expressions and precedence
An expression is a combination of values, variables and operators that Python evaluates to a single value, e.g. (a + b) / 2. When an expression has many operators, precedence decides which is done first, and associativity decides the order among operators of equal precedence (mostly left to right; ** is right to left).
print(2 + 3 * 4) # * before + print((2 + 3) * 4) # brackets first print(2 ** 3 ** 2) # ** is right to left: 2 ** 9 print(10 - 4 - 3) # left to right print(100 / 10 * 2) # left to right print(5 + 4 > 8 and not 3 > 4)
2 ** 2 = 4 → 3 * 4 = 12 → 12 // 4 = 3 → 12 + 3 = 15 → 15 − 1 = 14.
Summary & practice
/gives float,//floor quotient,%remainder,**power.- Comparisons give True/False; logical operators short-circuit.
ischecks identity,==checks value,inchecks membership.- Precedence: () → ** → unary → * / // % → + - → comparisons → not → and → or.
Find the output: (a) print(15 % 4 + 2 ** 3) (b) print(-9 // 2) (c) print(not 5 > 3 or 2 == 2) (d) print(10 / 4 * 2)
Answers: (a) 11 (b) -5 (c) True (d) 5.0
