Python program- Writing a program to input a year and check whether the year is leap year or not. This python project is useful for beginners and CBSE KV Computer Science practical file. The step wise details with input source file and out put.
What is Leap Year ?
Leap years are years where an extra day is added to the end of February. February 29, is commonly referred to as leap day. Leap years have 366 days instead of the usual 365 days and occur almost every four years.
Rules to calculate Leap Year:
- The year must be divisible by 4;
- If the year can also be evenly divided by 100, it is not a leap year;
- unless…The year is also evenly divisible by 400. Then it is a leap year.
Make a Python Program for Leap Year
Now we are going to make a program in Python to check whether the year is leap year or not using the above leap year calculation rule.
y=int(input(“Enter the year: “)) #the year is entered
if y%400==0: # checked if it is divisible by 400 %checks for remainder
print(“The year is a Century Leap Year”) # prints if remainder is zero
elif y%100!=0 and y%4==0: # check not divisible by 100 but divisible by 4
print(“Year is a Leap year”) # if above condition is truth then year is a leap year is printed
else: # else it is printed year is not a leap year
print(“Year is not a leap year”)
y=int(input(“Enter the year: “)) #the y here is a variable and the input is taken in integer
Test the Leap Year Program
Run (F5) the program and enter any year. In sample case we entered 2020. The program will run as following :-
- Check if if is divided by 400. condition is not true.
- Now it will check (elif) not divide by 100. condition matched. Therefore, it will print statement “Year is a leap year”
Success. You are able to a make a basic Python Program. Keep on going.