Skip to main content

Command Palette

Search for a command to run...

Python Leap Year Syntax

A Beginner's struggles.

Updated
3 min read
U

Stuck in a continuous loop of Hadamard gates, CNOTs and What nots. Maximally entangled in classical coding and Quantum Computing and Physics. Energy Quantisation, Wave-particle duality >>>

print("Welcome to the Leap year checker! ")

year = int(input("Enter the year in question! "))

if year % 4 == 0:
    if year % 100 == 0:
        if year % 400 == 0:
            print("This year is a leap year. ")
        else:
            print("This is not a leap year. ")
    else:
        print("This year is a leap year. ")
else:
    print("This isn't a leap year. ")

In the leap year checker code snippets, I realized that a year being divisible* by 400 makes it a guaranteed leap year. But a year divisible by 100 is NOT a leap year EXCEPT it’s divisible by 400 {This is encapsulated in divisibility by 400, right? Since 100 is a factor of 400}.

Now, here’s where it gets interesting: Any year divisible by 4 IS a leap year if it CANNOT be divided by 100, but is a leap year if it is divisible by 100 and also by 400 (simply, if divisible by 400).

Now, you might wonder why there’s the need for the divisible-by-100 check, since 400 ends up encapsulating 100, right? Well, not exactly. .

As an example, think of the year 300.
The year 300 is divisible by 4.
This would make it seem like a leap year but no, it’s ALSO divisible by 100! And since it’s divisible by 100, it won’t be a leap year unless it’s divisible by 400.
But, 300 cannot be divided by 400, hence it’s not a leap year.

I’m guessing you’ve equally realized the importance of the 100-divisibility check: it’s for years divisible by 4 which are greater than 100, but less than 400.

Wait, no. Upon thinking about it, that’s a narrowed view. Divisibility by 100, but not 400 doesn’t mean that the number is between 100 and 400! The year 600 is a year greater than 400, divisible by 100, but not divisible by 400 itself. Wow! I was previously thinking of coming up with a Python code that doesn’t follow the code structure I learnt (attached above) {yes, I'm currently taking a Python Course!}.

In the course I'm taking, three nested if functions were used. It was cool, but I wanted something different. I wanted a different approach. I tried various methods with elif and if. Yet, my codes either reported errors, or had flawed logics. It felt like the only approach (for a flat, beginner-level Python coder) to the problem.

However, upon some research, and further introspection, I eventually came up with a leap year calculation method that included an elif: function. Yes, that was my goal from the onset, haha.

How did I do that? I utilized a flowchart. After drawing the flowchart for the leap year calculation (tailored to the code structure I learnt, which I was so determined to rebel against), I began to think of other ways to draw the flowchart as I stared at it. And that’s when I came up with the flowchart corresponding to this current code structure that I so much adore for its originality (yes, originality because I came up with it, lol):

print("Welcome to the Leap year checker! ")

year = int(input("What year would you like to check? "))

if year % 4 == 0:
    if year % 400 == 0:
        print("This is a leap year.")
    elif year % 100 == 0:
        print("This is not a leap year.")
    else:
        print("This is a leap year.")
else:
    print("This is not a leap year.")

* divisible here refers to a division without remainders, i.e. the modulo operation.

Learning Python

Part 1 of 1

Follows my journey through completely (if possible) mastering python. It's not an everyday thing. I'll do this when my schedule and an unforgiving academic curriculum permits.