Leap Year Program in Python

Leap-year-or-not-using-python

Check if a Year is a Leap Year or Not in Python

Given an integer input year, the objective of the code is to Check if a Year is a Leap Year or Not in Python Language. To do so  we’ll check if the year input satisfies either of the two conditions of leap year.

Example
Input : 2020
Output : It's a Leap Year.

Check Whether a Year is a Leap Year or Not in Python

Given an integer input as the year, the objective is to Check if a Year is a Leap Year or Not in Python Language. To do so we’ll check each condition mentioned below in the blue box. It either of the conditions is satisfied, the year is a leap year. It’s not otherwise. Here are some methods to check whether or not it’s a leap year

  • Method 1: Using if-else statements 1
  • Method 2: Using if-else statements 2
  • Method 3 : Using Ternary Operator
  • Method 4 : Using Calendar Mode
  • Method 5 : Using Lamda Function

We’ll discuss all the above mentioned methods in detail in the upcoming sections. Before going for the approach read out the blue box below for better understanding of the concept.

leap year program in c
leap year program in c

Prime Course Trailer

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Related Banners

Method 1: Using if-else Statements 1

In this method we’ll use the if-else statements to check whether or not the input integer satisfies either of the conditions. To learn more about control statements in python, check out if-else control statements in Python.

Working

For a User Input year we do

  1. Check if the year variable is divisible by 400.
  2. Check if the year variable is divisible only by 4 and not 100.
  3. If the above mentioned conditions are satisfied, prints “Leap Year”, print “Not a Leap Year” otherwise.

Let’s implement the above logic in Python Language.

Python Code

Run
year = 2000
if (year%400 == 0) or (year%4==0 and year%100!=0): print("Leap Year") else: print("Not a Leap Year")

Output

Leap Year

Method 2: Using if-else Statements 2

In this method we’ll use the if-else statements to check whether or not the input integer satisfies either of the conditions. This method is a modified and simpler version of the previous method.

Working

For a User Input year we do

  1. The input is stored in an int type variable say year.
  2. year is checked for being a leap year or not with the following condition if( ((year % 4 == 0)&&(year % 100 != 0)) || (year % 400==0) )
  3. If the above condition is true then input is a leap year otherwise input is not a leap year.

Let’s implement the above logic in Python Language.

Python Code

Run
year = 2000
if( ((year % 4 == 0) and (year % 100 != 0)) or (year % 400==0) ):
    print("Leap Year")
else:
    print("Not leap Year")

Output:

Leap Year

Method 3: Using Ternary Operator

In this method we’ll use ternary operator in Python

Python Code

Run
def is_leap_year(year):
    return True if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) else False

# Test with year 2000
year = 2000
print(f"{year} is a leap year: {is_leap_year(year)}")

Output:

2000 is a leap year: True

Method 4: Using Calendar Module

In this method we’ll use calendar mode in python

Python Code

Run
import calendar

def is_leap_year(year):
    return calendar.isleap(year)

# Test with year 2000
year = 2000
print(f"{year} is a leap year: {is_leap_year(year)}")

Output:

2000 is a leap year: True

Explanation:

  • calendar.isleap(year) returns True if the year is a leap year, otherwise False.
  • This method is simple and reliable, as it uses Python’s built-in calendar utilities.

Method 5: Using Lamda Function

In this method we’ll use lamda function in python

Python Code

Run
# Lambda function to check leap year
is_leap_year = lambda year: True if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) else False

# Test with year 2000
year = 2000
print(f"{year} is a leap year: {is_leap_year(year)}")

Output:

2000 is a leap year: True

In closing

In this article, we explored multiple ways to check whether a given year is a leap year using Python. From basic if-else statements to more advanced methods like lambda functions and the calendar module, each approach ensures accurate results based on leap year rules.

Understanding leap year conditions is essential for date-related logic in programming. By mastering these Python techniques, you can build robust calendar-based applications with ease and precision.

FAQs

A year must be divisible by 400, or divisible by 4 but not by 100, to be considered a leap year.

Yes, Python’s built-in calendar.isleap(year) function directly returns whether a year is a leap year.

Yes, you can use a lambda function or ternary operator to create concise one-line checks for leap years.

Although both are divisible by 100, only 2000 is divisible by 400, making it a leap year while 1900 is not.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

43 comments on “Leap Year Program in Python”


  • methrisaroja0

    year = int(input(“enter the year:”))
    if (year%4 == 0 and year%100 != 0) or (year%400 == 0):
    print(“leap year”)
    else:
    print(“not a leap year”)


  • snegapriya1711sp

    import calendar as cal
    year = int(input(‘Enter a year : ‘))
    print(‘{} leap year’.format(f'{year} is a’ if cal.isleap(year) else f'{year} is not a’))


  • snegapriya1711sp

    year = int(input(‘Enter a year : ‘))
    print(‘{} leap year’.format(f'{year} is a’ if cal.isleap(year) else f'{year} is not a’))