In Python, it is very easy to check whether a given year is a leap year or not.
A leap year is a year that is divisible by 4 but not divisible by 100 unless it is also divisible by 400.
Here’s a simple Python program to check whether a given year is a leap year or not:
year = int(input("Enter a year: ")) if (year %% 4 == 0 and year %% 100 != 0) or (year %% 400 == 0): print(year, "is a leap year") else: print(year, "is not a leap year")
In this program, we first take input from the user as an integer value and store it in the variable year
.
We then use an if
statement to check whether the year is divisible by 4 and not divisible by 100, or it is divisible by 400. If the condition is true, then we print that the year is a leap year.
Otherwise, we print that it is not a leap year.
Let’s test this program with a few examples:
Example 1:
Enter a year: 2020 2020 is a leap year
Example 2:
Enter a year: 2021 2021 is not a leap year
Example 3:
Enter a year: 2000 2000 is a leap year
As we can see from the examples, the program works as expected.
In conclusion, checking whether a given year is a leap year or not is a simple task in Python, and we can use the modulo operator and an if
statement to accomplish this task.