Check a Leap Year in Python
Divisible by 4, but not by 100, unless by 400. Turn the calendar rule into one boolean expression.
- The three leap-year rules
- Combining conditions with and / or
- Testing 1900, 2000 and 2024
- 1
year % 400 == 0no - 2
year % 100 == 0no - 3
year % 4 == 0yes → leap year
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.
year % 4 == 0🎟️ Every fourth year is invitedyear % 100 != 0🚫 But not century years such as 1900year % 400 == 0🥇 Unless they hold a gold card: 2000 gets inor🔀 Either the gold card or the normal invitationA year is a leap year when it is divisible by 4 but not by 100, or when it is divisible by 400.
Key points
year % 4 == 0 and year % 100 != 0covers the normal leap years, such as 2024.year % 400 == 0brings back century years such as 2000.orcombines the two cases, soleapis True if either one matches.- 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,orand parentheses. - Validating dates such as February 29.
When not to use it
- Real applications: the
calendar.isleap()function and thedatetimemodule already handle it. - Dates before the Gregorian calendar (before 1582): the rule was different.
Example code
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")
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.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Checking only divisibility by 4
1900 would wrongly count as a leap year.
leap = year % 4 == 0
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.
- Leap Year Check whether a year follows the Gregorian leap-year rule. Easy
- Days in a Month Return how many days a month has, including leap years. Easy