Python Code Output Practice for PCEP and PCAP
Read a short Python snippet and predict exactly what it prints, or which exception stops it. Tap or type your answer: a wrong one names the slip, then the working shows each step.
What does this print?
An error? The answer is its name, such as TypeError. Every answer is the real output of CPython 3.12.10, 3.13.3 and 3.14.4.
Practise the exams
PCEP and PCAP both ask you to read and trace short programs like these. Start with the free real exam questions, then get the full pack.
Python output FAQ
Which Python version do PCEP and PCAP use?
The Python Institute syllabi test Python 3 and do not name a minor version. PCEP-30-03 includes the walrus operator :=, added in Python 3.8, and PCAP-31-04 includes match, added in Python 3.10. Every snippet here gives the same output on CPython 3.12.10, 3.13.3 and 3.14.4.
Which exam versions does this cover?
PCEP-30-03 and PCAP-31-04 came out on 31 October 2026. PCEP-30-02 and PCAP-31-03 stay active until their sunset date, 30 November 2027. The PCEP snippets follow PCEP-30-02. All of them are also in PCEP-30-03 except loop else (PCEP-30-02 objective 2.2), which now sits in PCAP-31-04 (objective 5.2.2). PCAP adds mutable default arguments (PCAP-31-04 objective 4.6.2), lambdas and closures (PCAP-31-03 5.2 and 5.3, PCAP-31-04 5.3.1 and 5.3.3), and the exception hierarchy, finally and raise (PCAP-31-03 2.1, PCAP-31-04 2.1.2 to 2.1.4).
Why is -7 // 2 equal to -4?
// is floor division: true division with the floor applied, so -3.5 becomes -4, not -3. % takes the sign of the right operand, so -7 % 2 is 1, and x == (x // y) * y + x % y always holds. With a float operand both give a float, so 7.0 // 2 is 3.0.
Why is -3 ** 2 equal to -9?
** binds more tightly than a minus on its left, so -3 ** 2 is -(3 ** 2). ** also groups from right to left: 2 ** 3 ** 2 is 2 ** 9, which is 512. Other operators that share a priority group left to right, so 10 - 4 - 3 is 3, and *, /, // and % all come before + and -.
Why does changing b also change a?
b = a gives the same list a second name; it copies nothing. a[:], list(a) and a.copy() make a new list, but a shallow one: lists inside it are still shared. b += [x] extends the shared list in place, while b = b + [x] builds a new list.
Why does a default list keep growing between calls?
A default value is evaluated only once, when def runs, so every call that leaves the argument out shares the same list or dict. The Python tutorial shows f(1), f(2), f(3) printing [1], [1, 2] and [1, 2, 3]. Use None as the default and make a new list inside the function.
Does range() include the stop value?
No. range(start, stop, step) gives start, start + step and so on, and stops before stop: below it for a positive step, above it for a negative step. range(n) starts at 0, so its last value is n - 1.
When does the else of a loop run?
When the loop ends without break: after the last pass of a for loop, or when the while condition turns false. A break skips it. The else belongs to the loop, not to an if inside it.
What do and and or return?
One of their operands, not always True or False. x or y returns x if x is true, else y; x and y returns x if x is false, else y. Both stop as soon as the result is known, so the right side may never run. not has a lower priority than ==, so not a == b means not (a == b).
Why does t[0] = 9 fail when t[1].append(9) works?
Tuples are immutable, so item assignment raises TypeError. A tuple can hold a mutable object such as a list, and that list can still change. A one-item tuple needs a comma: (5,) is a tuple, (5) is just 5.
In what order are dict keys, and what does get() do?
Insertion order, guaranteed since Python 3.7. Updating a key keeps its place; a key added again after del goes at the end. d[key] raises KeyError for a missing key, while d.get(key) returns None, or the default you give, and never raises KeyError.
Why do I get UnboundLocalError?
If a function assigns to a name anywhere in its body, that name is local in the whole function, so reading it before the assignment raises UnboundLocalError, a subclass of NameError. global n or nonlocal n makes the function use the outer name.
Which exceptions should I know?
PCEP-30-03 objective 4.2.1 names ValueError, TypeError, ZeroDivisionError, IndexError, KeyError and NameError. IndexError is a sequence index out of range (a slice past the end is cut, not an error), KeyError a missing dict key, ZeroDivisionError a division or modulo by zero, TypeError a wrong type, ValueError the right type with a wrong value, such as int('3.5'), and NameError a name that is not found.
How were the answers made?
This is the tool's own method. Each snippet comes from a fixed template. Every snippet was run as a program in its own CPython process, with a timeout, on CPython 3.12.10, 3.13.3 and 3.14.4, and the printed output or the exception name was stored only when all three agreed. Each wrong option is the real output of the code that its slip pictures. The page runs no Python itself: it shows those stored results.
Sources
- Python Institute: PCEP certification (PCEP-30-03) and PCEP-30-03 Exam Syllabus (blueprint 1.5, revised 2 November 2026); PCEP-30-02 and PCEP-30-02 Exam Syllabus (active until 30 November 2027).
- Python Institute: PCAP certification (PCAP-31-04) and PCAP-31-04 Exam Syllabus (blueprint 1.5, revised 2 November 2026); PCAP-31-03 and PCAP-31-03 Exam Syllabus (active until 30 November 2027).
- Python documentation, Expressions: the power operator, binary arithmetic (floor division and modulo), Boolean operations, assignment expressions and operator precedence.
- Python documentation, Built-in Types: Boolean operations, common sequence operations (indexes and slices), mutable sequence operations (s += t, copy), ranges, tuples, dict order and get(), str.find() and str.index(), list.sort().
- Python documentation, Built-in Functions: int(), Built-in Exceptions, Compound statements (while, for, try, finally, match) and Simple statements (global, nonlocal).
- Python documentation, More Control Flow Tools (else clauses on loops, default argument values), Data Structures (tuples), Classes (scopes and namespaces), Execution model (resolution of names) and the Programming FAQ (UnboundLocalError, lambdas in a loop, shared default values, why changing list y also changes x).
- Checked 20 November 2026: the same runner reproduces 23 worked examples from these documentation pages exactly, and every one of the 2,682 snippets gave the same output in a fresh process on CPython 3.12.10, 3.13.3 and 3.14.4.
Checked 20 November 2026 against the Python Institute exam syllabi and the Python 3 documentation on docs.python.org.
- DrillJava code output practicePredict what a short Java snippet prints, which exception stops it or whether it compiles.
- DrillOracle SQL NULL practicePredict what a query returns with NULLs in NOT IN, COUNT, NVL and ORDER BY, plus set operators.
- DrillGitHub Actions job outcome practiceRead a workflow's needs, if and continue-on-error and tap the jobs that are skipped.