Career upgrade: Learn practical AI skills for better jobs and higher pay.
Level up

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`.
Last updated: May 2026

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.

OperatorMeaningExample result
+addition or string concatenation3 + 2 is 5
-subtraction or unary negation3 - 2 is 1
*multiplication or string repetition'ha' * 2 is 'haha'
/true division5 / 2 is 2.5
//floor division5 // 2 is 2
%remainder5 % 2 is 1
**exponentiation2 ** 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:

  1. Parentheses and function calls
  2. Exponentiation **
  3. Unary plus and minus
  4. Multiplication, true division, floor division, remainder
  5. Addition and subtraction
  6. Comparisons
  7. not
  8. and
  9. or
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:

FunctionPurposeExample
int(x)Convert to integerint('42') gives 42
float(x)Convert to floatfloat('4.5') gives 4.5
str(x)Convert to stringstr(42) gives '42'
bool(x)Convert to truth valuebool(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.

Test Your Knowledge

What is the value of 1 + 2 * 3 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