PCEP Control Flow, Loops, Lists and Logic Practice Question
A developer wants to generate a list of squares of integers from 1 to 10 inclusive. Which list comprehension is correct?
⚠ Common exam trap
Python Institute often tests the exclusive nature of the `stop` argument in `range()`, trapping candidates who forget that `range(10)` stops at 9 and `range(1, 10)` stops at 9, leading them to choose options that omit the final value (10) or include an unwanted starting value (0).
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
✓
[x**2 for x in range(1, 11)]
`range(1, 11)` generates integers from 1 to 10 inclusive, and the list comprehension `[x**2 for x in range(1, 11)]` squares each integer, producing the desired list of squares. The `range()` function's stop value is exclusive, so `range(1, 11)` stops at 10, covering all numbers from 1 to 10.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
[x**2 for x in range(11)]
Why it's wrong here
range(11) yields 0 through 10, so the comprehension includes 0 squared and produces eleven elements rather than the ten required. It is tempting because 11 looks like the inclusive upper bound. The correct form is range(1, 11), which starts at 1 and stops before 11.
- ✗
[x**2 for x in range(10)]
Why it's wrong here
range(10) yields 0 through 9, so the comprehension squares zero and omits 10, failing the inclusive 1-to-10 requirement. It is tempting because range(10) is the common idiom for ten iterations. The correct form is range(1, 11), which produces exactly 1 through 10.
- ✗
[x**2 for x in range(1, 10)]
Why it's wrong here
range(1, 10) stops at 9, so the resulting list omits 100, the square of 10. The start value is right, which makes it tempting. Python's range excludes its stop argument, so reaching 10 inclusive requires range(1, 11).
- ✓
[x**2 for x in range(1, 11)]
Why this is correct
The `range(1, 11)` call yields integers 1 through 10, since the stop value is exclusive, satisfying the inclusive upper bound. Each value is squared by the `x**2` expression and collected into a new list, producing exactly the ten required squares without filtering or conditional logic.
Go deeper
Related to this question
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 →
Same concept, more angles
3 more ways this is tested on PCEP
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. What is the output of the following dictionary comprehension? {x: x**2 for x in range(3)}
medium- ✓ A.{0:0, 1:1, 2:4}
- B.{0:1, 1:2, 2:3}
- C.{0:0, 1:2, 2:4}
- D.{1:1, 2:4}
Why A: 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}`.
Variation 2. Which code correctly creates a list of squares for numbers 1 to 5 using a list comprehension?
easy- A.squares = [x**2 for x in range(5)]
- B.squares = [x^2 for x in (1,2,3,4,5)]
- ✓ C.squares = [x**2 for x in range(1,6)]
- D.squares = [x^2 for x in [1,2,3,4,5]]
Why C: 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]`.
Variation 3. Which two of the following are valid ways to create a list with elements 1, 2, 3? (Select two.)
easy- ✓ A.list = list(range(1, 4))
- ✓ B.list = [1, 2, 3]
- C.list = [1, 2, 3]]
- D.list = (1, 2, 3)
- E.list = [1; 2; 3]
Why A: `list(range(1, 4))` creates a list from the range object that generates numbers 1, 2, and 3 (the `range` function stops before the stop value 4). This is a common Python idiom for converting a range into a list.
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.