Sometimes it is hard to tell if a year is a leap year or not, especially the years back in 1900-1999 right!
Have you ever thought how to check if a year is a leap year using Python?
Before we get our hands on coding check out this article to get more basic information on Python.
Let's dive in coding
We shall create a program to check leap year
There are two ways:
- using calender module.
- Using operators.
1. Using calender module
For this is the simplest as one just have to import calender and use
calender.isleap() as shown below :
import calendar year = int(input("Enter the year : ")) print(calendar.isleap(year))
The year entered is translated to True or False if the year is leap year or not respectively.
2. Using operators
This method we have to define our procedure on how to check whether a year is a leap or not.
First we have to get year from the user . Divide by 100 which means century year and divide also divide by 400.
if not divided by 100 means not a century year but year divided by 4 is a leap year.
A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400.
As shown below:
Some of the output:
Enter a year : 2030 2030 is not a leap year Enter a year : 2000 2000 is a leap year Enter a year : 1900 1900 is not a leap year Enter a year : 1904 1904 is a leap year
Now you know on how to check if a year is a leap year using two ways. Thank you for reading this article.
KEEP ON MOVING 👣👣👣👣
Top comments (0)