Study guide

1.4 Operators, Precedence, and Type Conversion

PCEP Certified Entry-Level Python Programmer Study Guide Exam

Track your progress as you read

Finished sections get a check mark.

1 PCEP Exam Map and Study Strategy

Current PCEP-30-02 Facts How PCEP Tests Code Reading Official Resources and Ethical Prep Four-Week Study Plan and Readiness

2 Computer Programming and Python Fundamentals

1.1 Lexis, Syntax, Semantics, and Indentation 1.2 Identifiers, Keywords, Comments, and Basic Program Structure 1.3 Literals, Types, and Numeral Systems 1.4 Operators, Precedence, and Type Conversion 1.5 print(), input(), and Console Tracing

3 Control Flow

Boolean Expressions, Comparisons, and Truthiness If, Elif, Else, and Decision Trees While Loops and Sentinel Patterns For, Range, and Iteration Patterns Break, Continue, Pass, Loop Else, and Nesting

4 Data Collections

Indexing, Slicing, and Sequence Rules Lists, Mutation, Methods, and Aliasing Tuples, Immutability, and Packing Dictionaries, Keys, Membership, and Iteration Strings, Sets, and Collection Choice

5 Functions and Exceptions

def, return, None, and Call Flow Parameters, Arguments, Defaults, and Keywords Scope, Shadowing, global, and Local Errors Exception Types, try, except, else, and finally Recursion, Debugging, and Final Code Reading

1.4 Operators, Precedence, and Type Conversion

Key Takeaways Explain This Topic AI

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.

  • Operator Meaning Example Result
  • + addition / string concatenation 3 + 2 5
  • subtraction / unary negation 3 - 2 1
  • * multiplication / string repetition 'ha' * 2 'haha'
  • / true division 5 / 2 2.5
  • // floor division 5 // 2 2
  • % remainder (modulo) 5 % 2 1
  • ** exponentiation 2 ** 4 16

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:

  • Parentheses ( ) and function calls
  • Exponentiation **
  • Unary +x , -x
  • * , / , // , %
  • + , -
  • Comparisons < , <= , > , >= , == , != (and in , is )
  • not
  • and
  • 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:

  • Function Purpose Example
  • int(x) to integer (truncates floats toward zero) int(7.9) -> 7
  • float(x) to float float('4.5') -> 4.5
  • str(x) to text str(42) -> '42'
  • bool(x) to truth value bool(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.