Courseiva

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

    range(5) yields 0,1,2,3,4, so the resulting list holds squares of 0 to 4, omitting 25 and including an unwanted 0. It is tempting because range(5) is the idiomatic zero-based loop construct, correct when the required sequence genuinely starts at zero.

  • ✗

    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

    The comprehension iterates x over range(1,6), yielding 1 through 5 inclusive, and squares each value with the ** operator. This satisfies the stem's requirement of numbers 1 to 5, since range's stop argument is exclusive; range(1,5) would omit 25.

  • ✗

    squares = [x^2 for x in [1,2,3,4,5]]

    Why it's wrong here

    '^' is bitwise XOR, not exponent.

About these practice questions

Courseiva writes every PCEP question from scratch — 482 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 →

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.