Green = even, faded = odd
0
even numbers
in the list
in the list
1print(sum(1 for x in nums if x % 2 == 0))
x % 2 == 0 checks divisibility by 2. Combine it with sum() and a generator to count in one elegant line.
Visit each number. If it is even, add 1 to the counter.
From a list of numbers, count how many are even numbers. Even numbers divide by 2 without any remainder.
Used when filtering IDs, grouping seats (odd/even), selecting alternating hardware ports, or quickly counting items that match a numeric rule.
x % 2 == 0 is True for even numbers, False for odd.sum(1 for x in nums if x % 2 == 0) counts matching items without a manual loop variable.A warehouse app checks which shelf IDs are even-numbered (reserved for heavy items). The same % 2 == 0 filter runs on thousands of shelf numbers instantly.