Python Program to Check Leap Year?

17-Oct-2022

.

Admin

Python Program to Check Leap Year?

Hi Dev,

Here, I will show you how to works Python Program to Check Leap Year. This tutorial will give you simple example of Program to check if a given year is leap year. I explained simply step by step Check if a Year is a Leap Year or Not in Python. Here you will learn Leap Year Program in Python. you will do the following things for Python Program to Check whether Year is a Leap Year or not.

Leap year program in python; Through this tutorial, you will learn how to check leap year program in python.

let's see below simple example with output:

Example 1: Python program to check whether a given year is a leap year or not


# Python program to check the given year is leap or not

n = input("Please enter year")

year = int (n)

if (( year%400 == 0)or (( year%4 == 0 ) and ( year%100 != 0))):

print("%d is a Leap Year" %year)

else:

print("%d is Not the Leap Year" %year)

Output:

Please enter year 2021

2021 is Not the Leap Year

Example 2: Leap year program in python using elif:

# Python program to check the given year is leap or not

n = input("Please enter year")

year = int (n)

if (year%400 == 0):

print("%d is a Leap Year" %year)

elif (year%100 == 0):

print("%d is Not the Leap Year" %year)

elif (year%4 == 0):

print("%d is a Leap Year" %year)

else:

print("%d is Not the Leap Year" %year)

Output:

Please enter year 2020

2020 is a leap year

Example 3: leap year program in python using nested if

# Python program to check the given year is leap or not

n = input("Please enter year")

year = int (n)

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:

Please enter year 2020

2020 is a leap year

Example 4: Python program to check leap year using function

# Python program to check the given year is leap or not

# using math calender module in Python

#here import calender module

import calendar

n = input("Please enter year")

year = int (n)

# calling isleap() method to check for Leap Year

val = calendar.isleap(year)

if val == True:

print("% s is a Leap Year" % year)

else:

print("% s is not a Leap Year" % year)

Output:

Please enter year 2019

2019 is not a Leap Year

I hope it can help you...

#Python