PCEP Control Flow, Loops, Lists and Logic Practice Question
Which code correctly creates a list of squares for numbers 1 to 5 using a list comprehension?
⚠ Common exam trap
Python Institute often tests the distinction between `**` (exponentiation) and `^` (bitwise XOR), as well as the correct use of `range()` boundaries, to catch candidates who confuse operators or off-by-one errors.
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
✓
squares = [x**2 for x in range(1,6)]
It uses the proper syntax for a list comprehension: `[expression for item in iterable]`. Here, `x**2` computes the square, and `range(1,6)` generates numbers 1 through 5 (since range excludes the stop value). This produces the list `[1, 4, 9, 16, 25]`.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
squares = [x**2 for x in range(5)]
Why it's wrong here
Generates 0 to 4, not 1 to 5.
- ✗
squares = [x^2 for x in (1,2,3,4,5)]
Why it's wrong here
'^' is bitwise XOR, not exponent.
- ✓
squares = [x**2 for x in range(1,6)]
Why this is correct
Correct; range(1,6) gives 1-5 and ** is exponent.
- ✗
squares = [x^2 for x in [1,2,3,4,5]]
Why it's wrong here
'^' is bitwise XOR, not exponent.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCEP question from scratch — 498 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or 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.