How PCEP Tests Code Reading

Key Takeaways

  • PCEP code-reading questions reward exact tracing of Python execution, not general familiarity with programming vocabulary.
  • The first pass through any snippet should identify data types, control-flow boundaries, mutation points, and function return values.
  • Common traps include `input()` returning strings, `range()` excluding its stop value, list methods returning `None`, and the loop `else` clause.
  • Short handwritten trace tables are often faster and more reliable than trying to hold every variable state in memory.
  • Practice should include predicting output, identifying exceptions, choosing corrected code, and explaining why tempting distractors fail.
Last updated: June 2026

The Core Skill: Read Like the Interpreter

PCEP is entry-level, but it is not a vocabulary-only test. The exam asks whether you can follow small Python programs accurately under time pressure. A typical item asks for the printed output, the value of a variable, the exception raised, the missing line in a function, or the statement that correctly describes a snippet. The shared skill is code reading: stepping through Python statements in the same order the interpreter would, with the same rules.

A useful first pass is mechanical. Mark each variable, its value type, and every control-flow boundary before trying to answer. If a snippet contains if, while, for, break, continue, return, or except, do not jump to the options. Trace the path first, then compare your result to the answer choices. Reading the options first biases you toward a plausible-looking wrong answer.

This habit protects you from distractors. Many wrong answers are nearly correct: they include the stop value from range(), treat a string digit as an integer, assume a list method returns the list, or miss that an unhandled exception stops every statement after it. The gap between right and wrong is usually one Python rule, not a broad concept.

Trace Table Method

For loops and functions, use a small trace table. You do not need a spreadsheet; two or three columns are enough.

StepWhat to RecordWhy It Matters
Inputs and literalsStrings, ints, floats, booleans, listsAvoids type-conversion guesses
Branch conditionsTrue or false at each checkPrevents skipped elif/else errors
Loop variablesCurrent item or index each passCatches off-by-one range() mistakes
MutationsList or dictionary changesSeparates changed objects from new objects
Return pathValue returned or NoneDistinguishes print() from return
Exception pathFirst matching handlerTests handler order and propagation

Frequent PCEP Traps

The blueprint makes certain traps natural. In fundamentals, input() always returns a string, so input() + 1 raises TypeError and you must wrap it in int() or float() to do arithmetic. Operator precedence matters: ** binds tighter than unary minus, so -2 ** 2 is -4, and 2 ** 2 ** 3 is 256 because ** is right-associative. Integer division // floors toward negative infinity, so -7 // 2 is -4, not -3, and 7 % -2 is -1. In control flow, range(start, stop, step) excludes stop, and a loop's else clause runs only when the loop finishes without a break.

In collections, list methods such as append(), sort(), and reverse() mutate in place and return None, while string methods (upper(), replace()) return a new string and never change the original because strings are immutable. In functions, a function with no executed return returns None even if it prints output. In exceptions, handlers are tested top to bottom, so a broad except Exception placed before a specific one hides it.

Example of Exact Reading

Consider this original snippet:

items = [1, 2]
result = items.append(3)
print(items, result)

A beginner may expect result to be the new list. Python does not do that. append() changes items in place and returns None, so the output is [1, 2, 3] None. Compare it to slicing, which builds a new object:

words = ['a', 'b', 'c', 'd']
print(words[1:3], words[-1], words[::-1])

Here words[1:3] is ['b', 'c'] (stop index 3 excluded), words[-1] is 'd', and words[::-1] is the reversed copy ['d', 'c', 'b', 'a'] while words itself is unchanged. Tracing the type and whether an operation mutates or copies resolves most collection items.

Know the Common Exceptions on Sight

PCEP loves "what exception is raised?" items. You do not need the full exception hierarchy, but you must recognize the handful that beginner code produces and the exact trigger for each. Memorize this table cold:

ExceptionTypical triggerExample
TypeErrorOperation on incompatible types'a' + 1, len(5)
ValueErrorRight type, wrong valueint('abc')
ZeroDivisionErrorDividing by zero5 / 0, 5 % 0
IndexErrorSequence index out of range[1, 2][5]
KeyErrorMissing dictionary key via brackets{'a': 1}['b']
NameErrorUsing an undefined nameprint(x) before x is assigned
AttributeErrorMethod/attribute does not exist(1).append(2)

Also note that an unhandled exception halts the program at that line, so any print() after it never runs. In a try/except chain, the first matching handler runs and the rest are skipped; because ValueError, IndexError, and KeyError are all subclasses of broader classes, handler order decides which one catches.

Practice Targets

Build practice from four item styles:

  1. Predict the output of five-line snippets.
  2. Identify whether code raises TypeError, ValueError, IndexError, KeyError, ZeroDivisionError, NameError, or no exception.
  3. Choose the replacement line (gap-fill) that preserves a requested behavior.
  4. Explain the difference between two similar snippets.

The explanation step is the part most candidates skip. Do it anyway. If you can state why each wrong choice is wrong, you are training the exact discrimination the exam rewards, which directly mirrors the multiple-select and code-insertion item formats. A disciplined trace plus a one-sentence reason for each rejected option is the single most reliable PCEP technique.

Test Your Knowledge

What does this original snippet print? text = 'Py'; print(text * 2)

A
B
C
D
Test Your Knowledge

In a PCEP code-reading question, which first step is usually most reliable?

A
B
C
D
Test Your Knowledge

What value is assigned to saved after nums = [3, 1, 2]; saved = nums.sort()?

A
B
C
D