Python Program to Check Leap Year?
Development teams using “Python Program to Check Leap Year?” in client or internal work can use the linked planning resource to document project effort and hand-offs, then assess the record against delivered functionality, tests and agreed outcomes.
Validate current syntax, security guidance and supported versions through Python.org before using the example in production.
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
- Get Last 2 Digits of Number in Python Tutorial Example
- How to Sum of First and Last Digit of Number in Python?
- How to Get First and Last Digit of Number in Python?
- Get the Last Digit of Number in Python Tutorial Example
- Get the First Digit of Number in Python Tutorial Example
- How to Remove All Decimals from Number in Python?
- How to Convert String to Float with 2 Decimal Places in Python?
- How to Format Number to 2 Decimal Places in Python?