The Leap Year rule that confused me for years

I always thought a leap year has 366 days and occurs once every 4 years. And I was certainly correct — as far as that goes. What I didn't realize were the intricacies: the calendar it follows, the century exceptions, and why you need to check beyond a simple 'divisible by 4' rule.
Back in the day (in 1997) when I was learning PL/SQL, I was given a problem statement like every other beginner is given — to verify if a year entered is a leap year or not.
It seemed so simple. I wrote my logic using the basic building blocks of PL/SQL, just using a mod operator to check divisibility by 4. Here's what my flawed logic looked like (I'm showing it in Python since that's what I'm learning now):
My original (flawed) logic:
if year_entered % 4 == 0:
print("Leap year") # This would wrongly mark 1900 as leap!
else:
print("Not a leap year")
I proudly presented it to my instructor, and she said I was missing other conditions.
Missing other conditions? What were they?
She showed me the full solution — that we need to check for divisibility by 100 and 400 as well. All of this was presented to me without 'why' and 'what' is needed to make it work.
Maybe it was the only way I understood back then, without really realizing why they were doing it that way. Every Google search just presented the solution as follows:
if (year_entered % 100 == 0 and year_entered % 400 == 0):
print('It is a leap year')
elif (year_entered % 100 == 0 and year_entered % 400 != 0):
print('It is not a leap year')
elif (year_entered % 4 == 0):
print('It is a leap year')
else:
print("It is not a leap year")
I just could not understand the logic behind this leap year program — until I started learning Python from a well-known instructor, Mr. Abdul Bari (the same gentleman who teaches DSA). You may check out Mr. Bari's profile at LinkedIn.
He explained the how and what behind leap year calculation. Let me share what I learned.
How a leap year is actually calculated
First, what is a leap year?
a) A leap year has 366 days instead of the usual 365.
b) It happens when February has 29 days instead of 28.
c) Here's where it gets interesting — the leap year exists to compensate for the fact that Earth's orbit around the sun takes approximately 365.242 days.
That 365.242 days is not exactly 365. When rounded, it becomes 365.25 days — an extra quarter of a day. So after 4 orbits around the sun (4 years), the fourth year becomes a leap year. This explains why we have a leap year once every 4 years.
So far, so good.
Where the confusion came in?
If I entered the year 1900 to check, it is divisible by 4 (1900 % 4 = 0). Based on the "every 4 years" rule, it should have been a leap year. But in reality, 1900 is not a leap year. This is where I was always baffled.
Mr. Bari explains it perfectly. If we were following the Julian calendar, yes — 1900 would be a leap year. End of story. But today, we follow the Gregorian calendar, and that calls for an additional check.
The additional checks Here are the complete rules:
d) If the year is divisible by 100 and also divisible by 400, then yes — it is a leap year. (Example: 2000)
e) If the year is divisible by 100 but not divisible by 400, then it is not a leap year. (Example: 1900, 2100)
f) Only after checking the above two conditions do we apply the simple 4-year rule — if the year is divisible by 4, it is a leap year.
Why this makes sense now
Without the century checks, the year 1900 would incorrectly be called a leap year. The extra rules about 100 and 400 fix that error.
This clear explanation finally helped me understand why the 100, 400, and 4-year checks are all required — and why checking just the 4-year rule is not enough. This was my "ahhaa" moment.
Thanks a lot, Mr. Bari, for this excellent explanation. Now I finally get it.


