Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

What is the output of the following dictionary comprehension?

{x: x**2 for x in range(3)}

⚠ Common exam trap

Python Institute often tests whether candidates remember that `range(3)` starts at 0, not 1, and that `0**2` equals 0, not an omitted or undefined value, causing many to drop the first key-value pair or miscalculate the square of 1.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

{0:0, 1:1, 2:4}

The dictionary comprehension `{x: x**2 for x in range(3)}` iterates over `x` values 0, 1, and 2 (from `range(3)`). For each `x`, it creates a key-value pair where the key is `x` and the value is `x**2` (x squared). This produces `{0: 0**2, 1: 1**2, 2: 2**2}`, which evaluates to `{0:0, 1:1, 2:4}`.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✓

    {0:0, 1:1, 2:4}

    Why this is correct

    The comprehension iterates x over range(3), giving 0, 1 and 2, and maps each to its square. The resulting dictionary pairs 0 with 0, 1 with 1 and 2 with 4, producing exactly {0:0, 1:1, 2:4}.

  • ✗

    {0:1, 1:2, 2:3}

    Why it's wrong here

    The comprehension squares each value, producing {0:0, 1:1, 2:4}; this option lists the keys as values instead. It is tempting because it mirrors the key sequence, and it would be correct if the expression were the identity 'x' rather than 'x**2'.

  • ✗

    {0:0, 1:2, 2:4}

    Why it's wrong here

    Squaring 1 yields 1, not 2, so the middle pair is wrong; the correct mapping is {0:0, 1:1, 2:4}. It is tempting because doubling is a common operation, and it would be correct if the expression were 'x*2' rather than 'x**2'.

  • ✗

    {1:1, 2:4}

    Why it's wrong here

    range(3) yields 0, 1 and 2, so the key 0 must appear; omitting it drops a valid entry. It is tempting because starting at 1 feels natural, and it would be correct if the iterable were range(1,3).

About these practice questions

One of 482 original PCEP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.