Python program to check leap year
Description: Write a Python program to check whether a year is a leap year.
Rules and hints:
- A year is a leap year if it is divisible by 4 but not by 100.
- A year divisible by 400 is also a leap year.
Python program to check a leap year
def is_leap(year):
return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
for year in (2024, 1900, 2000, 2023):
if is_leap(year):
print(year, "is a leap year")
else:
print(year, "is not a leap year")
Output:
Tutorials Class - Output Window
2024 is a leap year
1900 is not a leap year
2000 is a leap year
2023 is not a leap year
How it works
- 2024 is divisible by 4 and not by 100, so it is a leap year.
- 1900 is divisible by 100 but not by 400, so it is not a leap year.
- 2000 is divisible by 400, so it is a leap year.
- 2023 is not divisible by 4.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |