Check Leap Year
Determine whether a given year is a leap year using the Gregorian calendar rules.
What You'll Learn
- Applying combined logical conditions
- Using modulo to test divisibility
- Implementing real calendar rules
Python Check Leap Year Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# Program to check if a year is a leap year
year = int(input("Enter a year: "))
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
print(year, "is a leap year")
else:
print(year, "is not a leap year")Enter a year: 2024 2024 is a leap year
Step-by-Step Breakdown
- 1Read the year from the user.
- 2Check divisibility by 400 first.
- 3Otherwise, check divisibility by 4 and not by 100.
- 4Print whether it is a leap year or not.
Understanding Check Leap Year
Leap year rules:
We encode this logic using a combination of modulo %, and, and or.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.
Practical Learning Notes for Check Leap Year
This Python program is part of the "Basic Python Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.