Check leap year (Python Programming)
Learn Check leap year (Python Programming) step by step with clear examples and exercises.
Why This Matters
Understanding how to check for leap years in Python is crucial for several reasons:
- Interviews and Assessments: Many programming interviews include questions related to checking leap years as part of the evaluation process.
- Real-world Applications: Leap years are essential when dealing with financial transactions, scheduling events that span multiple years, or calculating dates in various industries like finance, healthcare, and astronomy.
- Debugging: Being able to check for leap years can help you debug issues related to date calculations in your code.
- Education: Learning how to write a Python program to check for leap years helps reinforce fundamental programming concepts such as conditional statements and modulus operators.
Prerequisites
To follow this lesson, you should have a basic understanding of the following Python concepts:
- Python Operators: Arithmetic operators (e.g., +, -, *, /), comparison operators (e.g., ==, !=, <, >) and logical operators (e.g., and, or, not).
- if...else Statement: The conditional statement that allows you to execute different blocks of code based on a condition.
- Modulus Operator: The modulus operator (
%) returns the remainder when one number is divided by another. - Variables and Assignment: Understanding how to create and use variables in Python, such as
year.
Core Concept
A leap year is a year that is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400. For example, 2017 is not a leap year, while 2012 and 2000 are leap years.
Here's a simple Python program to check if a given year is a leap year or not:
year = 2000
Check if the year is a century year (ends with 00)
if year % 100 == 0:
If it's a century year, check if it's divisible by 400
if year % 400 == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
If the year is not a century year, check if it's divisible by 4
else:
if year % 4 == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
Worked Example
Let's walk through the code line by line:
year = 2000: This line assigns the value 2000 to the variableyear.if year % 100 == 0:: This checks if the given year is a century year (ends with 00).if year % 400 == 0:: If it's a century year, this checks if it's divisible by 400, making it a leap year.print("{0} is a leap year".format(year)): If the conditions are met, this line prints that the given year is a leap year.else:: This starts the block of code for non-century years (not divisible by 100).if year % 4 == 0:: This checks if the given year is divisible by 4, making it a leap year for non-century years.print("{0} is a leap year".format(year)): If the condition is met, this line prints that the given year is a leap year.else:: This starts the block of code for non-leap years (not divisible by 4 or 100).print("{0} is not a leap year".format(year)): If no conditions are met, this line prints that the given year is not a leap year.
Common Mistakes
Here are some common mistakes to avoid when writing your own program to check for leap years:
- Forgetting to handle century years: Remember that only century years divisible by 400 are leap years.
- Not considering non-century years: Don't forget to check if a year is divisible by 4 for non-century years.
- Incorrectly handling negative or zero years: These years should not be considered as leap years, so make sure your program handles them appropriately.
- Not using proper indentation: Python uses indentation to determine the structure of the code, so ensure that your if...else statements are properly indented.
Practice Questions
- Write a Python function that takes a year as an argument and returns True if it's a leap year and False otherwise.
- Modify the given code to handle negative or zero years correctly.
- Extend the program to check for valid input (e.g., checking if the user entered an integer).
- Write a function that checks whether a given month has 29 days in a leap year or not.
- Create a program that calculates the number of leap years between two given dates.
FAQ
- Why is 1900 not a leap year?: Because it's not divisible by 400, and 1900 is neither divisible by 4 nor 100.
- Is every fourth year a leap year?: No, only century years (years ending with 00) that are divisible by 400 are leap years.
- What's the next leap year after 2020?: The next leap year after 2020 is 2024.
- How many leap years are there in a century?: There are 97 leap years in a normal century (100 years) and 100 leap years in a leap century (400 years).
- Why do we have leap years?: Leap years help keep our calendar year synchronized with the solar year, which has approximately 365.24 days. A standard year of 365 days is about 11 minutes shorter than a solar year, so adding an extra day every four years (leap year) compensates for this discrepancy.