Python Examples

Python Program to Check Leap Year

Leap year Python is the Python code through which it can check whether the year input by the user is a leap year or not. A leap year is a year that comes in every year. The year is recognised by having 366 days instead of 365 days. The one extra day is added in the month of February, making it 29 days long. 

leap year python

                                                                                                                                                                                  Source

A leap year can be checked by dividing the year by 4. If the year is completely divisible, then it is a leap year. A century year on the other hand should be exactly divisible by 400 to be a leap year. 

To check if any century year is a leap year, then instead of checking its divisibility by 4, first the number is divided by 100. Then, the quotient obtained is again divided by 4. If the remainder thus obtained is 0, then the century year will be considered a leap year. 

Source Code 

The source code for leap year Python will use the modulus or remainder operator that is represented by ‘%’. When divided by 4, if the remainder provided is zero, then it means that the year is a leap year. 

Along with the operator, if…else condition will be used to check if the condition mentioned above is true or false. 

                    

year = int (input (“Enter any year that is to be checked for leap year: “))

if (year % 4) == 0:

              if (year % 100) == 0:

                             if (year % 400) == 0:

                                            print (“The given year is a leap year”)

                             else:

                                            print (“It is not a leap year”)

              else:

                             print (“It is not a leap year”)

else:     

            print (“It is not a leap year”)

In the source code, the program first checks whether the given year is divisible by 4. If the condition is true, it will then check if the year is divisible by 100. Again, if true, it will then check the divisibility of the year with 400. If these conditions are satisfied, then the program will provide the output that displays that the given year is a leap year.  

Note that here the equal to operator, that is, == is used and not the assigning operator. The assigning operator, represented by = assigns a value to a variable and is different from the mathematical = sign. 

Frequently Asked Questions

How do you do a leap year in Python?

A leap year Python is found by checking the divisibility of the year with 4 and 400. If a year is perfectly divisible by 4, then it is a leap year. However, if it is a century year (ending with 00), then it will be checked with 400. 

Why is 2020 not a leap year?

2020 is a leap year. It had 366 days with a February of 29 days. Also, the year 2020 is perfectly divisible by 4. It is, thus, a leap year. 

Is Python a leap?

Python does not have an in-built function to check whether a year is a leap or not. One will need to use the modulus operator to test a year for a leap. 

Is 2032 a leap year?

When dividing the number 2038 by 4, we obtain 508 as quotient and 0 as remainder. Thus, it is perfectly divisible by 4, making it a leap year.  

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.