Check Leap Year
Determine whether a given year is a leap year using the Gregorian calendar rules.
BeginnerTopic: Basic Python Programs
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")Output
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:
•If a year is divisible by 400 → leap year.
•Else if divisible by 4 but not by 100 → leap year.
•Otherwise → not a leap year.
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.