Placement Prep

Leap Year Program in Python: 3 Methods with Code

Three Python methods to check if a year is a leap year: calendar module, nested if statements, and a single boolean condition. Verified code and edge cases included.

By FACE Prep Team 5 min read
python leap-year python-programs if-else calendar-module placement-prep coding-practice

A leap year program in Python applies three divisibility rules in sequence: divisible by 4, except for century years, unless also divisible by 400.

The US Naval Observatory documents these as the standard Gregorian calendar rules for determining which years receive an extra day in February. All three Python methods below implement the same logic. The choice between them depends on what the coding platform allows.

The leap year rule

A standard calendar year has 365 days. A leap year adds one day to February, giving it 29 days instead of 28, for a total of 366 days. Three conditions determine whether a year qualifies:

  1. The year is divisible by 4.
  2. Exception: if also divisible by 100, it is NOT a leap year.
  3. Exception to the exception: if also divisible by 400, it IS a leap year.

Working through the standard test cases: 2024 is divisible by 4 but not by 100, so it is a leap year. The year 1900 is divisible by 4 and by 100 but not by 400, so it is not. The year 2000 is divisible by 400, so it is. Placement coding problems use 1900 and 2000 as test inputs because they expose whether the solver has handled all three conditions, not just the first.

Method 1: Python calendar module

Python’s standard library includes calendar.isleap(year), which encapsulates all three conditions in one call. Import the module, call the function, check the boolean return value.

import calendar

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

if calendar.isleap(year):
    print(f"{year} is a Leap Year")
else:
    print(f"{year} is not a Leap Year")

Sample runs:

  • Input: 2024 → Output: 2024 is a Leap Year
  • Input: 1900 → Output: 1900 is not a Leap Year
  • Input: 2000 → Output: 2000 is a Leap Year

Use this method when the problem does not restrict built-in functions. It is the shortest, most readable implementation. The function handles negative years and other boundary inputs that the manual methods do not account for.

Method 2: Nested if statements

The nested approach traces the three divisibility conditions one at a time, making each decision point visible. This is the version to reach for when a placement problem says “without using built-in functions.”

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

if year % 4 == 0:
    if year % 100 == 0:
        if year % 400 == 0:
            print(f"{year} is a Leap Year")
        else:
            print(f"{year} is not a Leap Year")
    else:
        print(f"{year} is a Leap Year")
else:
    print(f"{year} is not a Leap Year")

Step-by-step trace for 2024:

  • 2024 % 4 == 0 is True, enter the outer if
  • 2024 % 100 == 0 is False (remainder is 24), take the else branch
  • Output: 2024 is a Leap Year

Step-by-step trace for 1900:

  • 1900 % 4 == 0 is True, enter the outer if
  • 1900 % 100 == 0 is True, enter the middle if
  • 1900 % 400 == 0 is False (remainder is 300), take the innermost else
  • Output: 1900 is not a Leap Year

Step-by-step trace for 2000:

  • 2000 % 4 == 0 is True
  • 2000 % 100 == 0 is True
  • 2000 % 400 == 0 is True
  • Output: 2000 is a Leap Year

Each level of nesting introduces one new constraint. The if fires when the constraint is met; the else handles the complement. If an interviewer asks you to trace the execution by hand, this structure is the clearest to walk through. The same nested-if pattern appears in the greatest of three numbers in Python program, where conditions narrow candidates one check at a time.

Method 3: Single boolean condition

Method 3 collapses all three rules into one boolean expression using Python’s and and or operators. No module import required.

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

if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
    print(f"{year} is a Leap Year")
else:
    print(f"{year} is not a Leap Year")

Reading the expression left to right: if year % 400 == 0, the year qualifies immediately. If that fails, the second clause checks year % 4 == 0 and year % 100 != 0, which handles the standard four-year case while excluding century years. Python’s short-circuit evaluation stops at the first True.

Sample runs:

  • Input 2024: 2024 % 400 == 0 is False; 2024 % 4 == 0 and 2024 % 100 != 0 evaluates to True and True, so True. Output: 2024 is a Leap Year
  • Input 1900: 1900 % 400 == 0 is False; 1900 % 4 == 0 and 1900 % 100 != 0 evaluates to True and False, so False. Output: 1900 is not a Leap Year
  • Input 2000: 2000 % 400 == 0 is True. Short-circuit fires. Output: 2000 is a Leap Year

The % modulo operator is the same tool that Armstrong number checks use to test divisibility of each digit. The operator and reasoning are identical; only the threshold values differ.

Verifying the edge cases

These five years appear in AMCAT, TCS NQT, and Infosys InfyTQ coding rounds as standard test inputs for the leap year problem:

YearDiv by 4?Div by 100?Div by 400?Leap year?
2024YesNoNoYes
2025NoNoNoNo
1900YesYesNoNo
2000YesYesYesYes
2100YesYesNoNo

The two years that catch solvers are 1900 and 2100. Both are divisible by 4 and by 100, which fools a solution that only checks the first condition. The 400-year override makes 2000 a leap year while correctly excluding 1900 and 2100.

A solution that returns “Leap Year” for 1900 is missing the century-year exception. A solution that returns “Not a Leap Year” for 2000 is applying the century rule without the 400-year override. Both are easy to reproduce with all three methods shown above; verify before submitting.

Which method to use

SituationRecommended method
Built-ins allowed, production-style codeMethod 1 (calendar module)
Built-ins banned by the problem statementMethod 2 or Method 3
First time learning the conditionsMethod 2 (nested if, most explicit)
Compact single-expression answer requiredMethod 3
Reusable helper inside a larger programWrap Method 3 in a def function

The Python basic programs collection groups this type of if-condition exercise with factorial, prime check, Fibonacci, and Armstrong number programs. Practicing them together builds the condition-writing pattern recognition that placement rounds test across problem types.

Method 3’s single boolean expression is a direct application of composing and and or without nesting. That same skill, writing compact boolean logic for rule-based decisions, appears in production Python code as much as it does in placement tests. If you want to move that condition-chaining discipline from coding exercises to building AI-powered tools, TinkerLLM offers a hands-on environment at ₹299 where FACE Prep readers apply the same control-flow patterns to real API filters, validators, and agent rules.

Primary sources

Frequently asked questions

What is the rule for checking a leap year in Python?

A year is a leap year if divisible by 400, or if divisible by 4 but not by 100. In Python: (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0).

Is 2000 a leap year in Python?

Yes. 2000 % 400 == 0 is True, so all three methods return Leap Year for 2000. The year 1900 returns Not a Leap Year because it is divisible by 100 but not by 400.

Is 2100 a leap year?

No. 2100 is divisible by 4 and 100, but not by 400, so it is not a leap year. A century year is only a leap year when divisible by 400.

Does Python's calendar module handle the 400-year rule correctly?

Yes. calendar.isleap() implements all three Gregorian rules: divisible by 4, except centuries, except 400-year marks. You do not need to write the conditions manually when using this function.

Which method do placement tests expect for a leap year program?

When the problem says 'without using built-in functions', use Method 2 or Method 3. When no restriction applies, Method 3 is the most compact and readable choice.

Can I write a reusable Python function to check leap years?

Yes. Wrap Method 3 in a def block: def is_leap(year): return (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0). Call is_leap(2024) and it returns True.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF