PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions
A Python script processes a list of tuples representing coordinates: `points = [(1,2), (3,4), (5,6)]`. The developer wants to create a dictionary mapping each coordinate to its distance from origin. Which code correctly creates the dictionary?
⚠ Common exam trap
Python Institute often tests the distinction between squared distance and actual distance, and between list comprehensions and dictionary comprehensions, to catch candidates who overlook the square root or the correct data structure.
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
✓
distances = {}; for point in points: distances[point] = (point[0]**2 + point[1]**2)**0.5
Options B and C are both correct. Option B uses a for loop to compute the Euclidean distance with the square root and inserts it into a dictionary. Option C achieves the same result with a dictionary comprehension. Both produce a dictionary mapping each coordinate tuple to its distance from the origin.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
distances = {}; for point in points: distances[point] = (point[0]**2 + point[1]**2)
Why it's wrong here
This option computes squared distance, not actual distance, so it is incorrect.
- ✓
distances = {}; for point in points: distances[point] = (point[0]**2 + point[1]**2)**0.5
Why this is correct
Technically correct because it computes the Euclidean distance correctly using **0.5 and assigns it to a dictionary with a loop. However, the exam considers option C as the correct answer because it uses a dictionary comprehension, which is more idiomatic and succinct. Therefore, option B is not the intended correct answer.
- ✓
distances = {point: (point[0]**2 + point[1]**2)**0.5 for point in points}
Why this is correct
This option uses a dictionary comprehension to compute the Euclidean distance correctly.
- ✗
distances = {point: point[0]**2 + point[1]**2 for point in points}
Why it's wrong here
This option computes squared distance, not actual distance, so it is incorrect.
- ✗
distances = [(point, (point[0]**2 + point[1]**2)**0.5) for point in points]
Why it's wrong here
This produces a list of tuples, not a dictionary, so it is incorrect.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 498-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
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.