Lesson 5 of 7
Loop + condition = super combo
By combining for and if, you can sort, count or filter a whole list in a few lines.
The % operator gives the remainder of a division: n % 2 == 0 means "n is even".
for n in [1, 2, 3, 4]:
if n % 2 == 0:
print(n)🎯 Your mission
Here are your dice rolls: numbers = [3, 8, 5, 12, 7, 6]. Count how many are even in a variable evens (starting at 0), then print evens.
PRO lesson
This advanced chapter is part of CodeAge PRO. Unlock it to keep your adventure going.
Discover PRO →