PCEP Control Flow, Loops, Lists and Logic Practice Question
A developer is writing a Python inventory reconciliation script. They need to examine each element of a list named `stock` and, for every element, execute a block of code that updates a running total. They do not want the loop to terminate early under any condition. Which statement should be placed at the beginning of the loop body to iterate over every element in order?
⚠ Common exam trap
The trap here is assuming that `range()` or `len()` can be used interchangeably with a list in a for loop header, when both actually require an integer and will raise a TypeError if given a list object.
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
✓
for item in stock:
Iterating over a list with `for item in stock:` visits every element in order and assigns each value to the loop variable, making it the direct way to process all entries without early exit. The alternatives misuse `range()`, `len()`, or `while` in ways that either raise errors or fail to traverse the list sequentially, so they cannot accomplish the reconciliation task.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
for item in len(stock):
Why it's wrong here
`len(stock)` returns an integer representing the number of elements, and an integer is not iterable, so Python raises a TypeError immediately. To iterate by index you would need `range(len(stock))`, but that yields index positions rather than the stock values themselves, which does not satisfy the requirement to examine each element value directly.
- ✓
for item in stock:
Why this is correct
This header iterates directly over each element contained in the `stock` list, assigning each value to `item` in order from index 0 to the final index. Because no break or early termination is included, the loop body runs once per element, which matches the requirement to update a running total for every entry without skipping or stopping.
- ✗
for item in range(stock):
Why it's wrong here
`range()` requires an integer argument, and passing a list object raises a TypeError at runtime because a list is not an integer. Even if the list contained numbers, `range(stock)` would treat the list as a single numeric bound rather than iterating over the list's elements, so this header cannot work for the described inventory scenario.
- ✗
while item in stock:
Why it's wrong here
A while loop evaluates its condition before each iteration, and `item in stock` would only be true while the current value of `item` happens to exist somewhere in the list. It does not walk through every element in sequence and can loop forever or exit prematurely, so it cannot reliably process each inventory entry exactly once.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 482-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 and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official Python Institute exam blueprint
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.