Ad space (banner)
🐍Python Lessons
Lesson 66 / 69

Checking Whether a Year Is a Leap Year

This lesson covers the logic for determining leap years, so you can learn how to correctly translate a rule that looks simple but hides exceptions into code. It's written for anyone searching "Python leap year check".

A year is a leap year if it's divisible by 4 but not by 100, or if it's divisible by 400. This is a fundamental piece of logic commonly used in calendar and date-calculation programs. It's distinctive in that judging purely by "a multiple of 4" gives the wrong answer — a classic exercise for practicing correctly structured conditionals.

The sample code determines leap years with a single condition: (year % 4 == 0 and year % 100 != 0) or year % 400 == 0. Check the results: 2024 is a multiple of 4 and not a multiple of 100, so it's a leap year; 1900 is a multiple of 4 but also a multiple of 100 (and not a multiple of 400), so it's not.

A common beginner mistake is overlooking the exception for years divisible by 100. Implementing only the simple rule "divisible by 4 means leap year" incorrectly marks a year like 1900 as a leap year. This is good practice for correctly translating a "simple-looking rule with hidden exceptions" into code.

This is foundational knowledge behind any program that works with dates, like a calendar app or age calculator. Subtle spec details like leap years are exactly the kind of thing you always need to keep in mind when building a real calendar application.

Python
OUTPUT

💡 The Python engine may take a few seconds to load the first time you run code.

Ad space (banner)
Ad space (in-article)