1.4 Operators, Precedence, and Type Conversion
Key Takeaways
- Python arithmetic includes `+`, `-`, `*`, `/`, `//`, `%`, and `**`, with `/` always producing a float result.
- Parentheses override default precedence and are the safest way to communicate intended grouping.
- Exponentiation binds tighter than unary signs in some expressions and is right-associative.
- Mixed integer and float arithmetic commonly converts the result to `float`, while incompatible operands raise `TypeError`.
- `int()`, `float()`, and `str()` perform explicit conversions, but invalid text such as `'7.5'` passed to `int()` raises `ValueError`.
Operators create expressions
An operator combines values into a larger expression. PCEP focuses heavily on arithmetic and basic logical operators because small changes in symbols can change both value and type.
| Operator | Meaning | Example result |
|---|---|---|
+ | addition or string concatenation | 3 + 2 is 5 |
- | subtraction or unary negation | 3 - 2 is 1 |
* | multiplication or string repetition | 'ha' * 2 is 'haha' |
/ | true division | 5 / 2 is 2.5 |
// | floor division | 5 // 2 is 2 |
% | remainder | 5 % 2 is 1 |
** | exponentiation | 2 ** 4 is 16 |
The division operators are common exam traps. / returns a float even when the mathematical result is whole, so 4 / 2 produces 2.0. // floors the result, which means it moves down on the number line. With negative numbers, that can surprise candidates.
Precedence and associativity
Precedence decides which operation is grouped first when there are no parentheses. Associativity decides grouping among operators at the same precedence level.
A practical PCEP precedence ladder is:
- Parentheses and function calls
- Exponentiation
** - Unary plus and minus
- Multiplication, true division, floor division, remainder
- Addition and subtraction
- Comparisons
notandor
value = 1 + 2 * 3
print(value) # 7
value = (1 + 2) * 3
print(value) # 9
Most binary arithmetic operators group left to right. Exponentiation groups right to left, so 2 ** 3 ** 2 means 2 ** (3 ** 2). Parentheses remove doubt and are often the cleanest answer in a code-completion question.
Unary signs also need care. -2 ** 2 is interpreted as -(2 ** 2), so the result is -4, while (-2) ** 2 is 4. On the exam, a small pair of parentheses can be the whole difference between two answer choices.
Type conversion
Python sometimes performs automatic numeric conversion. Combining an int with a float usually produces a float result.
price = 8
rate = 1.25
total = price * rate
print(total) # 10.0
Python does not automatically convert unrelated types in every context. 3 + '4' raises TypeError because Python will not guess whether you meant numeric addition or string concatenation.
Explicit conversion functions are tested often:
| Function | Purpose | Example |
|---|---|---|
int(x) | Convert to integer | int('42') gives 42 |
float(x) | Convert to float | float('4.5') gives 4.5 |
str(x) | Convert to string | str(42) gives '42' |
bool(x) | Convert to truth value | bool(0) gives False |
Invalid conversions raise errors. int('7.5') raises ValueError because the string is not a valid integer literal for int(). Convert it with float('7.5') first only when that matches the intended program.
Exam tracing method
For expression snippets, do not calculate left to right blindly. Mark parentheses, apply precedence, check associativity, then check the resulting type. If the expression includes input text, conversion must happen before arithmetic.
What is the value of 1 + 2 * 3 in Python?
What happens when Python evaluates int('7.5')?
Which expression produces the float value 2.0?