codescracker


python

Python Program to Check Leap Year or Not



« Previous Program Next Program »


Check Leap Year or Not in Python

To check whether the given year is a leap year or not a leap year in python, you have to ask from user to enter a year to check for leap year as shown in the program given here.

Python Programming Code to Check Leap Year or Not

Following python program ask from user to enter year to check whether it is a leap year or not:

# Python Program - Check Leap Year or Not

print("Enter 'x' for exit.");
num = input("Enter year: ");
if num == 'x':
    exit();
try:
    year = int(num);
except ValueError:
    print("Please, enter year...exiting...");
else:
    if((year%4 == 0) and (year%100 != 0)):
        print(year, "is a Leap Year.");
    elif((year%100 == 0) and (year%400 == 0)):
    	print(year, "is a Leap Year.");
    elif(year%400 == 0):
    	print(year, "is a Leap Year.");
    else:
    	print(year, "is not a Leap Year.");

Here is the sample run of the above Python program demonstrating how to check for leap year:

Below is the sample initial output asking from user to enter year to check it out:

check leap year python

Now let's enter any year, say 2012. As it is a leap year, therefore here is the output you will see:

check leap year or not python

Re-run the same program and check for a year, that is not a leap year, say 2017 as shown in the screenshot of sample run given below:

python check leap year

Here is the same program written and run on python shell:

python check leap year

Program Video with Compile and Run

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:


« Previous Program Next Program »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test