Beginner ยท ages 8+ ยท if / else ยท remainder ยท loops
Even or odd in Python.
Say whether each number from 1 to 10 is even or odd.
The program
Press Run to see what it prints. Change anything you like, then run it again.
for n in range(1, 11):
if n % 2 == 0:
print(n, "is even")
else:
print(n, "is odd")1 is odd 2 is even 3 is odd 4 is even 5 is odd 6 is even 7 is odd 8 is even 9 is odd 10 is even
How it works
%gives the remainder after dividing. An even number divides by 2 with no remainder.ifchecks a condition. If it is true, the first block runs; otherwise theelseblock runs.==asks whether two values are equal. A single=stores a value, so do not mix them up.
Try this
- Change the range to 1 to 20.
- Print only the even numbers.
- Print whether each number is divisible by 3.
Stuck on an error? Read how to debug Python.
Similar ideas
More programs
FizzBuzz
Count from 1 to 20. For multiples of 3 print Fizz, for multiples of 5 print Buzz, and for both print FizzBuzz.
Easy ยท ages 9+Fibonacci numbers
Build the Fibonacci sequence, where each number is the sum of the two before it.
Easy ยท ages 10+Reverse a string
Turn a sentence round in three different ways.
Beginner ยท ages 9+FAQ
Even or odd: questions
What does the even or odd program do?
Say whether each number from 1 to 10 is even or odd. It prints: 1 is odd / 2 is even / 3 is odd โฆ
Which Python ideas does it use?
if / else, remainder, loops. Beginner level, suitable from about age 8.
How do I run it?
Press Run on this page. To run it on your own computer, install Python 3 and save the code in a file ending in .py. It prints the same thing.
What should I try next?
Change the range to 1 to 20. Print only the even numbers. Print whether each number is divisible by 3.
Start a conversation
Want a teacher to guide the learning?
Tell us the learner's age and what they can already do, and we will suggest a starting point. A parent or guardian can make the enquiry.
- Tell us the learnerโs age or grade and goals
- Weโll suggest a suitable class or learning path
- Weโll confirm format, timing and fees with a parent or guardian
Play it, don't just read it
Write some code right now
Free games that need no sign-up. Pick one and play for five minutes.
Pick a game
- Code BuilderThe lines of a Python program are mixed up. Tap them into the rightโฆPlay
- Predict the OutputRead a short Python program and choose what it prints. Then watch it runโฆPlay
- Bug HunterA Python program prints the wrong answer. Find the one line with theโฆPlay
- Code JungleWrite real Python to guide Miko the monkey to every banana. 24 levels:โฆPlay