Beginner Python Exercises

Check a Leap Year in Python

Divisible by 4, but not by 100, unless by 400. Turn the calendar rule into one boolean expression.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • The three leap-year rules
  • Combining conditions with and / or
  • Testing 1900, 2000 and 2024
year = 2024
  1. 1year % 400 == 0no
  2. 2year % 100 == 0no
  3. 3year % 4 == 0yes → leap year
One expression: (y % 4 == 0 and y % 100 != 0) or y % 400 == 0

Three rules, checked in order

The problem

Calendars, booking systems and date calculations all need to know whether February has 29 days. The rule is not just every fourth year: century years are an exception, and years divisible by 400 are an exception to the exception.

Think of it like this

The leap-year rule works like a club with a special invitation list. Every fourth year is invited, except century years, who are turned away, unless they hold a gold card by being divisible by 400.

From a club door to code
year % 4 == 0🎟️ Every fourth year is invited
year % 100 != 0🚫 But not century years such as 1900
year % 400 == 0🥇 Unless they hold a gold card: 2000 gets in
or🔀 Either the gold card or the normal invitation
2024 and 2000 are leap years; 1900 is not.
In one sentence

A year is a leap year when it is divisible by 4 but not by 100, or when it is divisible by 400.

Key points

  1. year % 4 == 0 and year % 100 != 0 covers the normal leap years, such as 2024.
  2. year % 400 == 0 brings back century years such as 2000.
  3. or combines the two cases, so leap is True if either one matches.
  4. The f-string chooses "a" or "not a" with a conditional expression.

When should you use it?

  • Date calculations, calendars and booking systems.
  • Practicing conditions combined with and, or and parentheses.
  • Validating dates such as February 29.

When not to use it

  • Real applications: the calendar.isleap() function and the datetime module already handle it.
  • Dates before the Gregorian calendar (before 1582): the rule was different.

Example code

Run code →
main.py
year = 2024
# Century years must also be divisible by 400.
leap = year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)
print(f"{year} is {'a' if leap else 'not a'} leap year")
Get a Run button for this example

Result of running the code

2024 is a leap year

Code explanation

The expression has two parts joined by or. year % 4 == 0 and year % 100 != 0 accepts ordinary leap years and rejects century years, while year % 400 == 0 accepts the century years that are leap years after all. For 2024 the first part is True, so leap is True.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Checking only divisibility by 4

1900 would wrongly count as a leap year.

Problem
leap = year % 4 == 0
Fix
leap = year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges