In this program, you will learn to check whether a year is leap year or not. We will use nested if…else to solve this problem.
To understand this example, you should have the knowledge of the following Python programming topics:
A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400. For example,
2017 is not a leap year 1900 is a not leap year 2012 is a leap year 2000 is a leap year
Source Code
# Python program to check if year is a leap year or not
year = 2000
# To get year (integer input) from the user
# year = int(input("Enter a year: "))
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
2000 is a leap year
You can change the value of year in the source code and run it again to test this program.
Related posts:
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Program to Find Numbers Divisible by Another Number
Python String endswith()
Python Program to Check If a String Is a Number (Float)
Python String split()
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python Program to Get the File Name From the File Path
Python Program to Count the Number of Occurrence of a Character in String
How to get current date and time in Python?
Python Set symmetric_difference()
Python Exception Handling Using try, except and finally statement
Python Type Conversion and Type Casting
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python round()
Python String isalpha()
Python Set issuperset()
Python Program to Find Hash of File
Python Program to Return Multiple Values From a Function
Python Program to Iterate Through Two Lists in Parallel
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python String encode()
Python Program to Make a Flattened List from Nested List
Python Program to Find the Factorial of a Number
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python Program to Check if a Key is Already Present in a Dictionary
Python Deep Learning Cookbook - Indra den Bakker
Python Set intersection_update()
Python Dictionary popitem()
Python Set union()
Python File I/O Operation
Python String isupper()
Python timestamp to datetime and vice-versa