Introduction
A leap year comes once every four years. A leap year has a total of 366 days, unlike a regular year which has 365 days. This article will help you in implementing a program that determines whether a given year is a leap year in Python or not.
How to Check If a Given Year is a Leap Year in Python?
A year is determined as a leap year in Python if it meets the following conditions:
- If the given year is divisible by four, then it is a leap year. For example, 2004 is a leap year.
- If the given year is a multiple of 4 but not divisible by 100, it is a leap year. For example, 2012 is a leap year.
- If the given year is divisible by both 100 and 400, then it is a leap year. For example, 2000 is a leap year.
Learn data science course from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
Methods to Determine if a given Year is a Leap Year in Python
In this section, we will discuss the different methods to determine whether a given year is a leap year in Python or not.
Method 1: Determine Leap Year in Python using Math Module in the Program
import calendar
a = int(input(“ Enter the year that you want to check: ”)) num = calendar.isleap(a) if num == True: print(“The year entered %s is a Leap Year” %a) else: print(“The year entered %s is not a Leap Year” %a) |
Input: Enter the year that you want to check: 2012
Output: The year entered 2012 is a Leap Year
Method 2: Determine Leap Year in Python using Nested If Condition in the Program
a = int(input(“ Enter the year that you want to check: ”))
if (a% 4) == 0 if (a % 100) == 0: if (a % 400) == 0: print(“The year entered %s is a leap year” %a) else: print(“The year entered %s is not a leap year” %a) else: print(“The year entered %s is a leap year” %a) else: print(“The year entered %s is not a leap year” %a) |
Input: Enter the year that you want to check: 2005
Output: The year entered 2005 is not a leap year
Related Read: Python Program to add two numbers
Method 3: Determine Leap Year in Python using If Condition in the Program
a = int(input(“ Enter the year that you want to check: ”))
if (( a%400 == 0)or (( a%4 == 0 ) and ( a%100 != 0))): print(“The year entered %s is a leap year” %a) else: print(“The year entered %s is not a leap year” %a) |
Input: Enter the year that you want to check: 2004
Output: The year entered 2004 is a leap year
Checkout: Python Pattern Programs
upGrad’s Exclusive Data Science Webinar for you –
How upGrad helps for your Data Science Career?
Program Explanation
The above program allows the end-user to provide any year as input to the Python program. The program then checks whether the year entered by the user is a leap year or not. After validation, the program prints the output on the console.
Explore our Popular Data Science Online Courses
In the above program, we will check three conditions using logical “AND” and “OR” operators. The three conditions are as follows:
- year%400 == 0 – determines if the remainder is equal to 0 or not.
- year%4 == 0 – determines if the remainder is equal to 0 or not.
- year%100 != 0 – determines if the remainder is not equal to 0.
The second and third statement uses an AND operator, which means the result of these two statements should be True for a leap year. If either of the conditions is false, then the program stops because there is no point in checking further. The given year is not a leap year.
Top Data Science Skills to Learn to upskill
SL. No | Top Data Science Skills to Learn | |
1 | Data Analysis Online Courses | Inferential Statistics Online Courses |
2 | Hypothesis Testing Online Courses | Logistic Regression Online Courses |
3 | Linear Regression Courses | Linear Algebra for Analysis Online Courses |
The first and the other two conditions use an OR operator that means either of the results should be True. If either of the statements is true, it means the given year is a leap year in Python.
Check out Python Tutorials for free.
Conclusion
The code used in the article is only for explanatory purposes. You can modify the statements given in the examples as per your requirements. In this blog, we discussed the leap year in Python program implementation. You can try out the code to strengthen your Python knowledge.
If you are curious to learn about data science, check out IIIT-B & upGrad’s Executive PG Programme in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.