Python program that checks if a given year is a leap year:

Created with Sketch.

Python program that checks if a given year is a leap year:

year = int(input("Enter a year: "))

# Check if the year is a leap 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.")

The program prompts the user to enter a year using the input() function, and stores the value in the variable “year”. It then uses an if-else statement to check if the year is a leap year. A leap year is a year that is divisible by 4, except for end-of-century years which must be divisible by 400. So, it checks if the year is divisible by 4 and not divisible by 100 or divisible by 400. If the year is divisible by 4 and not divisible by 100 or divisible by 400, then it is a leap year, otherwise it is not a leap year.

Another way to check if a year is a leap year is by using the calendar module:

import calendar
year = int(input("Enter a year: "))
if calendar.isleap(year):
    print(year,"is a leap year.")
else:
    print(year,"is not a leap year.")

Here, the isleap() function from the calendar module is used to check if a year is a leap year.

Both the methods will give you the same output which is whether the year is leap year or not. You can use any method that suits your need or preference.

Leave a Reply

Your email address will not be published. Required fields are marked *