Python Program to Check Leap Year

Checking whether a year is a leap year is a classic programming problem that helps beginners understand conditional logic and real-world rules. Leap years are important in calendar systems because they help keep our calendar aligned with Earth's orbit around the Sun. In this tutorial, we will explore how to check leap years in Python using different methods and understand the logic behind it.

What is a Leap Year?

A leap year is a year that contains an extra day (February 29) to make the calendar year align with the solar year. Normally, a year has 365 days, but a leap year has 366 days.

Leap Year Rules

A year is considered a leap year if it follows these rules: - It is divisible by 4 - But not divisible by 100, unless it is also divisible by 400 Examples: - 2024 → Leap Year - 1900 → Not Leap Year - 2000 → Leap Year

Why Learn This Program?

This program helps you understand nested conditions, logical operators, and real-world decision-making in programming. It is also commonly asked in coding interviews.

Step-by-Step Algorithm

  • Start the program
  • Input a year from the user
  • Check if the year is divisible by 4
  • If yes, check if divisible by 100
  • If divisible by 100, check if divisible by 400
  • Decide leap year or not
  • Display result
  • End the program

Python Program Using Conditional Statements

Python
# Check leap year

year = int(input("Enter a year: "))

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
    print("Leap Year")
else:
    print("Not a Leap Year")
Sample Output:

Enter a year: 2024
Leap Year

Code Explanation

In this program, we use conditional statements to check the leap year rules. The condition ensures that the year is divisible by 4 and not by 100 unless it is divisible by 400.

Using Function

Python
def is_leap_year(year):
    return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)

year = int(input("Enter a year: "))

if is_leap_year(year):
    print("Leap Year")
else:
    print("Not Leap Year")

Using Built-in Module

Python
import calendar

year = int(input("Enter a year: "))

if calendar.isleap(year):
    print("Leap Year")
else:
    print("Not Leap Year")

Handling Invalid Input

Python
try:
    year = int(input("Enter a year: "))
    if year < 0:
        print("Invalid year")
    elif (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        print("Leap Year")
    else:
        print("Not Leap Year")
except ValueError:
    print("Please enter a valid number")

Real-World Applications

  • Calendar applications
  • Date validation systems
  • Event scheduling software
  • Time-based calculations
  • Financial and payroll systems

Common Mistakes to Avoid

  • Ignoring century rule (100 and 400)
  • Incorrect logical conditions
  • Not validating input
  • Using wrong operators
  • Not handling negative years

Advanced Enhancements

  • Build a full calendar application
  • Create a date calculator
  • Develop scheduling system
  • Add GUI using Tkinter
  • Integrate with web apps

Practice Exercises

  • Find all leap years in a range
  • Count leap years between two years
  • Build a calendar viewer
  • Validate full date input
  • Create a time calculator

Conclusion

The leap year program is a simple yet important example of applying real-world rules in programming. It helps you understand conditional logic and improves problem-solving skills. By practicing variations of this program, you can strengthen your coding fundamentals.

Note: Note: Always remember the 100 and 400 year rules when working with leap year calculations.