What You'll Learn
- •Applying combined logical conditions
- •Using modulo to test divisibility
- •Implementing real calendar rules
Python
# Program to check if a year is a leap year
year = int(input("Enter a year: "))
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
print(year, "is a leap year")
else:
print(year, "is not a leap year")Output
Enter a year: 2024 2024 is a leap year
Leap year rules:
- If a year is divisible by 400 → leap year.
- Else if divisible by 4 but not by 100 → leap year.
- Otherwise → not a leap year.
We encode this logic using a combination of modulo %, and, and or.
Step-by-Step Breakdown
- 1Read the year from the user.
- 2Check divisibility by 400 first.
- 3Otherwise, check divisibility by 4 and not by 100.
- 4Print whether it is a leap year or not.