Python Tutorials

Python Programs

Python Check Leap Year


Python Check Leap Year

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.


What is a Leap Year

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.


Syntax

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")


Checking if a year is 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,

  1. Declare a variable year and assign it a value.
  2. Use an 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".
  3. Use an else statement to handle the case where the year does not meet the leap year conditions and print "Not a Leap Year".
  4. Print the result based on the condition met.

Python Program

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")

Output

Leap Year