English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Função do Python

Tipos de Dados do Python

Python file operations

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Python program to check leap year

Python example collection

In this program, you will learn to check if a year is a leap year. We will use nested if ... else to solve this problem.

To understand this example, you should understand the followingPython programmingTopic:

Leap years can be4divisible, except for century years (years ending in 00). Only those that can be completely400 divisible century years are leap years. For example,

2017 is not a leap year
1900 is not a leap year
2012 is a leap year
2000 is a leap year

Source code

# Python program to check if a year is a leap year
year = 2000
# Get year from user (integer input)
# year = int(input("Enter 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 result

2000 is a leap year

You can change the value of the year in the source code, then run the program again to test it.

Python example collection