Courseiva

PCEP Control Flow, Loops, Lists and Logic Practice Question

A student is writing a Python script to sum all even numbers in a list `nums`. The script should ignore odd numbers. Which code correctly computes the sum?

⚠ Common exam trap

Many exam-takers confuse the modulo condition for even numbers with the one for odd numbers, or mistakenly using division instead of modulo.

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

✓

total = 0 for n in nums: if n % 2 == 0: total += n

The correct loop uses the modulo operator to test whether each number is even, and adds only those numbers to the running total. This directly implements the requirement to sum even numbers while ignoring odd ones, using a standard accumulator pattern in Python.

Answer analysis

Option-by-option breakdown

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

  • ✗

    total = sum(nums) if nums % 2 == 0 else 0

    Why it's wrong here

    This attempts to apply the modulo operator to the entire list, which raises a TypeError because lists do not support the modulo operation. Additionally, the conditional expression checks the list rather than individual elements. This code is both syntactically and semantically wrong for summing even numbers.

  • ✗

    total = 0 for n in nums: if n / 2 == 0: total += n

    Why it's wrong here

    Using division instead of modulo is incorrect. n / 2 equals zero only when n is zero, so this condition would add only zeros to the total. It does not identify even numbers and would produce an incorrect sum, likely zero or missing most even numbers.

  • ✗

    total = 0 for n in nums: if n % 2 == 1: total += n

    Why it's wrong here

    The condition n % 2 == 1 is true for odd numbers, so this loop sums the odd numbers instead of the even ones. The student wants to sum even numbers, so this code produces the opposite result. It would be correct only if the goal were to sum odd values.

  • ✓

    total = 0 for n in nums: if n % 2 == 0: total += n

    Why this is correct

    This loop checks each number for evenness using the modulo operator; if the remainder when divided by 2 is zero, the number is added to the total. Odd numbers are skipped. This correctly sums only the even numbers in the list, matching the student's requirement.

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 →

How Courseiva writes practice questions · Editorial policy

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.