Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Examples
  3. Even or Odd Checker

Conditional Statements · Python example

Even or Odd Checker

Use the modulo operator and an if-else statement to classify an integer.

Open the compilerAll examples
beginnerif elsemoduloconditions

The remainder operator % makes it easy to test divisibility. An even number has a remainder of 0 when divided by 2.

example.py
number = 27

if number % 2 == 0:
    print(f"{number} is even")
else:
    print(f"{number} is odd")

How the code works

  • 27 % 2 evaluates to 1.
  • The condition 1 == 0 is False.
  • Python skips the if block and runs the else block.

Key ideas

  • number % 2 computes the remainder after division by 2.
  • The == operator compares two values for equality.
  • Only one branch of an if-else statement runs.

Try changing it

  • Read the number from standard input.
  • Check divisibility by 3 or 5 instead.
  • Use a conditional expression: result = 'even' if n % 2 == 0 else 'odd'.

Related examples

beginner

Find the Largest of Three Numbers

Combine conditions with and to find the maximum of three values.

beginner

Check a Leap Year

Combine divisibility rules to decide whether a year is a leap year.

Previous exampleArea and Circumference of a CircleNext exampleFind the Largest of Three Numbers

Expected output

27 is odd

Output is compared after trimming trailing whitespace on each line. Run the code to see it produced by CPython in your browser.

Practice next

Apply the same idea with automatic tests or browse a guided project.

ExercisesProjects