In this Python lesson, we will practice how to write a program that will check if a year is a leap year or not. A year is a leap year if the year is a multiple of 400 or the year is a multiple of 4 and not a multiple of 100.
Example: Check Leap Year Using Nested if…else
# request year input from user
year = int(input('Enter a year: '))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print('{0} is a leap year'.format(year))
else:
print('{0} is not a leap year'.format(year))
else:
print('{0} is a leap year'.format(year))
else:
print('{0} is not a leap year'.format(year))
Output
Enter a year: 2032
2032 is a leap year
Here, we created a program that checks if a year is a leap year or not using the nested if-else statement. You can test for other years to know which year is a leap or not.