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. Check a Leap Year

Conditional Statements · Python example

Check a Leap Year

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

Open the compilerAll examples
beginnerconditionscalendarmodulo

Leap years follow three divisibility rules. The boolean expression in this example mirrors those rules directly.

example.py
year = 2024

is_leap = (year % 4 == 0 and year % 100 != 0) or year % 400 == 0

if is_leap:
    print(f"{year} is a leap year")
else:
    print(f"{year} is not a leap year")

How the code works

  • 2024 % 4 is 0 and 2024 % 100 is not 0, so the first part is True.
  • The or expression is already True, so the second part is not needed for the result.
  • The if statement prints the leap-year message.

Key ideas

  • A year divisible by 4 is a leap year unless it is divisible by 100.
  • A year divisible by 400 is always a leap year.
  • and has higher precedence than or, but parentheses make the logic clearer.

Try changing it

  • Test 1900 and 2000 to see the century rule in action.
  • Read the year from input.
  • Wrap the check in a reusable is_leap_year(year) function.

Related examples

beginner

Even or Odd Checker

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

beginner

Grade Calculator

Map a numeric score to a letter grade using an if-elif ladder.

Previous exampleFind the Largest of Three NumbersNext exampleGrade Calculator

Expected output

2024 is a leap year

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