Courseiva
Control Flow, Loops, Lists and LogicmediumMultiple ChoiceObjective-mapped

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

    Invalid syntax: 'then' is not a keyword in list comprehensions.

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

    Why it's wrong here

    Invalid syntax: else not allowed in list comprehension filter.

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

    Why it's wrong here

    Invalid syntax: else not allowed in list comprehension filter.

About these practice questions

This PCEP question is part of Courseiva's 498-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.