Courseiva

PCEP Control Flow, Loops, Lists and Logic Practice Question

A company needs to filter a list of temperatures in Celsius to only those above 0, then convert to Fahrenheit (multiply by 9/5 and add 32). Which code snippet correctly accomplishes this using a list comprehension?

⚠ Common exam trap

Python Institute often tests the distinction between the filter `if` (placed after the `for` clause) and the conditional expression `if-else` (placed in the expression part), and the trap here is that candidates mistakenly add an `else` to a filter-only comprehension, expecting it to work like a ternary operator.

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

✓

[t*9/5+32 for t in temps if t>0]

It uses the standard list comprehension syntax: `[expression for item in iterable if condition]`. Here, `t*9/5+32` is the expression that converts Celsius to Fahrenheit, `for t in temps` iterates over the list, and `if t>0` filters out temperatures at or below zero. This produces a new list containing only the Fahrenheit equivalents of positive Celsius temperatures.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✓

    [t*9/5+32 for t in temps if t>0]

    Why this is correct

    Correct list comprehension with expression and filter.

  • ✗

    [t for t in temps if t>0 then t*9/5+32]

    Why it's wrong here

    Python list comprehensions have no `then` keyword; the syntax is `[expression for item in iterable if condition]`. Writing `if t>0 then t*9/5+32` raises a SyntaxError. The `then` construct belongs to languages such as shell or SQL CASE expressions, not Python.

  • ✗

    [t*9/5+32 for t in temps if t>0 else 0]

    Why it's wrong here

    A conditional expression cannot follow the comprehension's `if` filter; `if t>0 else 0` is invalid syntax there, so the snippet raises a SyntaxError. The `else` clause belongs to a ternary in the output expression, which would be written `[t*9/5+32 if t>0 else 0 for t in temps]` — but that keeps every element, not filtering.

  • ✗

    [t*9/5+32 for t in temps if t>0 else t]

    Why it's wrong here

    Placing `else t` after the `if t>0` filter is a syntax error; a comprehension's `if` clause takes no `else`. The ternary form `[t*9/5+32 if t>0 else t for t in temps]` is valid but returns unconverted non-positive values, so it fails the filter requirement.

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 →

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.