Book a free demo

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

  1. % gives the remainder after dividing. An even number divides by 2 with no remainder.
  2. if checks a condition. If it is true, the first block runs; otherwise the else block runs.
  3. == asks whether two values are equal. A single = stores a value, so do not mix them up.
See the Python path โ†’

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

All programs โ†’

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

For a childโ€™s class, please have a parent or guardian send the enquiry. Avoid sharing passwords or sensitive personal details.

Weโ€™ll use your details only to respond to this request.

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.

All free games โ†’
Chat on WhatsApp
Esc

Try