1.4 Operators, Precedence, and Type Conversion

Key Takeaways

  • Python arithmetic includes `+`, `-`, `*`, `/`, `//`, `%`, and `**`; `/` always produces a `float`.
  • Parentheses override default precedence and are the safest way to communicate intended grouping.
  • Exponentiation `**` is right-associative and binds tighter than unary minus, so `-2 ** 2` is `-4`.
  • Mixed `int`/`float` arithmetic promotes the result to `float`; incompatible operands raise `TypeError`.
  • `int()`, `float()`, `str()`, and `bool()` convert explicitly, but `int('7.5')` raises `ValueError`.
Last updated: June 2026

Operators create expressions

An operator combines values into a larger expression. PCEP leans heavily on arithmetic and basic logical operators because a one-symbol change can alter both the value and its type.

OperatorMeaningExampleResult
+addition / string concatenation3 + 25
-subtraction / unary negation3 - 21
*multiplication / string repetition'ha' * 2'haha'
/true division5 / 22.5
//floor division5 // 22
%remainder (modulo)5 % 21
**exponentiation2 ** 416

The division operators are the most common traps. / returns a float even when the math is whole, so 4 / 2 is 2.0. // floors toward negative infinity, not toward zero: -7 // 2 is -4, not -3. The remainder follows so the identity a == (a // b) * b + (a % b) holds, so -7 % 2 is 1.

Precedence and associativity

Precedence decides which operation groups first when no parentheses are present; associativity decides grouping among operators of equal precedence. A practical PCEP ladder, highest first:

  1. Parentheses ( ) and function calls
  2. Exponentiation **
  3. Unary +x, -x
  4. *, /, //, %
  5. +, -
  6. Comparisons <, <=, >, >=, ==, != (and in, is)
  7. not
  8. and
  9. or
print(1 + 2 * 3)     # 7
print((1 + 2) * 3)   # 9

Most binary arithmetic operators associate left to right. Exponentiation associates right to left, so 2 ** 3 ** 2 means 2 ** (3 ** 2) = 2 ** 9 = 512, not 64. Unary signs need care too: -2 ** 2 parses as -(2 ** 2) = -4, while (-2) ** 2 = 4. On the exam a single pair of parentheses is often the entire difference between two answer choices.

Boolean operators short-circuit: in a and b, if a is falsy, b is never evaluated, and a and b returns the actual operand, not always True/False. So 0 and 5 is 0 and 2 or 0 is 2.

Type conversion

Python performs automatic numeric promotion: combining an int with a float produces a float.

price = 8
rate = 1.25
print(price * rate)   # 10.0

Python does not silently convert unrelated types. 3 + '4' raises TypeError because Python will not guess between numeric addition and string concatenation. By contrast '3' * 2 is allowed and gives '33' (repetition), a frequent distractor.

Explicit conversion functions are tested often:

FunctionPurposeExample
int(x)to integer (truncates floats toward zero)int(7.9) -> 7
float(x)to floatfloat('4.5') -> 4.5
str(x)to textstr(42) -> '42'
bool(x)to truth valuebool(0) -> False

Invalid conversions raise errors. int('7.5') raises ValueError because the string is not a valid integer literal; you must convert with float('7.5') first if a numeric value is intended. bool is falsy for 0, 0.0, '', None, and empty containers; everything else is truthy.

Exam tracing method

Do not calculate strictly left to right. Mark parentheses first, apply the precedence ladder, then check associativity (especially around ** and unary minus), then check the resulting type (was a float created by /?). If the snippet involves input() text, the conversion must occur before any arithmetic, or you will be repeating or concatenating strings instead of adding numbers.

Comparison and logical operators

Comparison operators return a bool. They can be chained, a feature unique among the languages PCEP candidates often know first: 1 < x < 10 is evaluated as (1 < x) and (x < 10), with x evaluated once. Equality == compares values, while is compares object identity; 2 == 2.0 is True but their types differ, and is should be reserved for None checks such as x is None. The membership operators in and not in test containment, as in 'a' in 'cat' returning True.

Logical and, or, and not work on truthiness but return operands, not always booleans, as noted with short-circuiting. A subtle exam trap: not 0 is True, not '' is True, and not 5 is False, because not always returns a clean boolean even though and/or do not.

Compound assignment operators

Python provides augmented assignments that combine an operation with assignment: x += 1 means x = x + 1, and the family includes -=, *=, /=, //=, %=, and **=. These read the current value, apply the operator, and rebind the name. The exam may show x = 5; x //= 2; print(x) and expect 2. Note that /= produces a float, so x = 4; x /= 2 makes x equal 2.0, not 2. Tracing augmented assignments line by line, computing the right side from the current value before rebinding, prevents the most common arithmetic-tracing errors on this part of the syllabus.

Test Your Knowledge

What is the value of the expression 2 ** 3 ** 2 in Python?

A
B
C
D
Test Your Knowledge

What happens when Python evaluates int('7.5')?

A
B
C
D
Test Your Knowledge

Which expression produces the float value 2.0?

A
B
C
D