In this tutorial, we will learn how to check if a year is a leap year in Python. We will cover the basic conditional statements and the rules for determining leap years.
A leap year is a year that is divisible by 4, but not by 100, unless it is also divisible by 400. This ensures that the year has an extra day, February 29, to keep the calendar year synchronized with the astronomical year.
The syntax to check if a year is a leap year in Python is:
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap Year")
else:
print("Not a Leap Year")
We can use conditional statements to check if a given year is a leap year based on the leap year rules.
For example,
year
and assign it a value.if
statement to check if the year is divisible by 4 and not divisible by 100, or if it is divisible by 400. If true, print "Leap Year".else
statement to handle the case where the year does not meet the leap year conditions and print "Not a Leap Year".year = 2024
# Check if the year is a leap year
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap Year")
else:
print("Not a Leap Year")
Leap Year