This article will help you understand the algorithm of the leap year program in Python.

So let us begin to explore more about this.

What is Leap Year?

There are 365 days from January to December in a typical year. February month has 29 days instead of the usual 28 since we add a day to it every four years. As a result, these years are referred to as leap years since they have 366 total days instead of 365. We can keep the calendar in line with the real length of a year, which is roughly 365.24 days, by adding an extra day every four years.

Get the tech career you deserve, faster!

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

Flowchart of Leap Year in Python

How can one determine if a year is a Leap year or not?

Using the flowchart in the previous section, you can easily find out if a year is a leap year or not. Let's take a few examples.

2020: This year is divisible by 4 but not divisible by 100, which makes it a leap year.

2021: This year is not divisible by 4, making it a common year.

1900: This year is divisible by 4 and 100 but not divisible by 400, which makes it a common year.

2000: This year is divisible by 4, 100 and 400 making it a leap year.

Leap Year Program in Python

There are many ways to print leap year in Python. Let us look at some of them briefly.

Using if-else statement

Using calendar module

Using function

Using if-else statement

Code:

Python

Python

enteryear=int(input("Enter the year you want to check:")) if(enteryear%4==0 and enteryear%100!=0 or enteryear%400==0): print("The year you've entered is a leap year!") else: print("The year you've entered isn't a leap year!")

Output:

Explanation:

1. The year that will be checked must first be entered.

2. The conditional statement determines whether the year is a multiple of 4 but not the multiple of 100 or the multiple of 400 (not all multiples of 4 are leap years).

3. The outcome is then printed if the condition satisfies, print “The year you've entered is a leap year!" otherwise, "The year you've entered isn't a leap year!".

Using calendar module

Code:

Python

Python

import calendar year = int(input("Enter year you want to check: ")) leap_year = "Leap year" if calendar.isleap(year) else " The year you've entered is not a leap year" print(leap_year)

Output:

Explanation:

This code checks if the year you've entered is a leap year or not using the calendar.isleap() function from the calendar module in Python. If the entered year is a leap year, it prints "Leap year". Otherwise, it prints, "The year you've entered is not a leap year". The user is prompted to enter the year to check through the input() function.

Using function

Code:

Python

Python

def checkifyearisleap(Enteredyear): return (((Enteredyear % 4 == 0) and (Enteredyear % 100 != 0)) or (Enteredyear % 400 == 0)) year = int(input("Enter year you want to check: ")) if checkifyearisleap(year): print(" The year you entered is a Leap Year") else: print("The year you entered is Not a Leap Year")

Output:

Explanation:

In this code, we defined a function called "checkifyearisleap" that takes a year as input and returns a boolean indicating whether the year is a leap year or not. It uses the standard leap year formula, where a year is considered a leap year if it is divisible by 4 but not by 100 or if it is divisible by 400. The main code prompts the user to enter a year, calls the function to check if it is a leap year or not, and prints the message that it is a leap year or not.

The code for finding leap year consists of simple conditional logic. In this article, we looked at the Python implementation of the leap year logic.

What is the algorithm for leap year or not?

The leap year algorithm determines if a year is a leap year. If a year is divisible by 4 but not by 100, or if it's divisible by 400, it's a leap year.

Is leap year Python datetime?

Yes, Python's datetime module can check if a year is a leap year. Use calendar.isleap(year) to return True if 'year' is a leap year, otherwise False.

Conclusion

In this article, you have learned about leap year and how we can do the leap year program in Python, like using the if-else statement, the calendar module, and the functions. We hope this article helped you know how to do the leap year program in Python.

If you want to learn more, refer to these articles: