PCEP Control Flow, Loops, Lists and Logic Practice Question
A software tester is examining a list of error codes: `errors = [404, 500, 403, 404, 200]`. She wants to determine if the code 404 appears in the list and print 'Found' if it does, otherwise 'Not found'. Which code correctly achieves this?
⚠ Common exam trap
The trap here is using `index()` or `count()` for membership testing, which can lead to incorrect results or exceptions when the value is at index 0 or absent.
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
✓
if 404 in errors: print('Found') else: print('Not found')
The `in` operator is the correct tool for checking if an element exists in a list. It returns a boolean and does not raise errors. The other options either misuse the `in` operator, rely on `index()` which can raise exceptions and misinterpret index 0, or invert the logic with `count()`.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
if errors.count(404) == 0: print('Found') else: print('Not found')
Why it's wrong here
The `count()` method returns the number of occurrences. This code prints 'Found' when the count is zero, which means the value is absent. It reverses the logic. For the given list, count is 2, so it would print 'Not found', which is incorrect.
- ✗
if errors in 404: print('Found') else: print('Not found')
Why it's wrong here
This reverses the operands: it checks if the list `errors` is contained within the integer 404, which is nonsensical and raises a `TypeError`. The `in` operator requires the left operand to be an element and the right operand to be a container. This is a common syntax mistake.
- ✓
if 404 in errors: print('Found') else: print('Not found')
Why this is correct
The `in` operator checks membership in a list. If 404 is present, it prints 'Found'; otherwise, 'Not found'. Since 404 is in the list, it prints 'Found'. This is the simplest and most direct way to test for presence.
- ✗
if errors.index(404): print('Found') else: print('Not found')
Why it's wrong here
The `index()` method returns the first index of the value, or raises a `ValueError` if not found. If found at index 0, it returns 0, which is falsy, so it would print 'Not found' incorrectly. Also, if not found, it raises an exception. This is unreliable for membership testing.
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.