Master Leap Year Program in Python with W3Schools Tutorial

Logan Jul 13, 2026

Leap years are an integral part of our Gregorian calendar, occurring every four years, with a few exceptions. Python, a widely-used programming language, provides an avenue for us to explore leap years programmatically. W3Schools, a popular online learning resource, offers a comprehensive guide to Python that includes a section dedicated to leap year calculation. Let's delve into the world of Python and leap years, with a helping hand from W3Schools.

How to find leap year in Python?
How to find leap year in Python?

Understanding leap years begins with knowing the rules that govern them. According to the Gregorian calendar, a year is considered a leap year if it is divisible by 4. However, there are exceptions to this rule. Years that are divisible by 100 are only leap years if they are also divisible by 400. This ensures that the calendar stays well-aligned with Earth's rotation around the Sun, which is approximately 365.2422 days long.

Leap Year Program in Python πŸ“… | Beginner Logic Explained
Leap Year Program in Python πŸ“… | Beginner Logic Explained

Calculating Leap Years in Python

Now that we understand the rules of leap years, let's see how we can calculate them using Python. W3Schools provides a simple and efficient method to check if a given year is a leap year or not.

Python Program to Check whether Year is a Leap Year or not
Python Program to Check whether Year is a Leap Year or not

Here's a basic function that checks if a year is a leap year according to the rules mentioned above:

Basic Leap Year Function

30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast

```python def is_leap_year(year): if year % 4 == 0: if year % 100 == 0: if year % 400 == 0: return True else: return False else: return True else: return False ```

In this function, we first check if the year is divisible by 4. If it is, we then check if it's divisible by 100. If it is, we check if it's also divisible by 400. If all these conditions are met, the year is a leap year and the function returns `True`; otherwise, it returns `False`.

Testing the Function

To use this function, you can simply call it with a year as an argument:

Best Free Websites to Learn Python for Beginners
Best Free Websites to Learn Python for Beginners

```python print(is_leap_year(2020)) # Outputs: True print(is_leap_year(2100)) # Outputs: False ```

Leap Years in a Range

While our initial function serves its purpose, it's not always efficient to check years one by one. Python's inbuilt range function allows us to loop through a range of years and check them all at once.

7 Career Opportunities After Learning Python
7 Career Opportunities After Learning Python

Here's how you can print all leap years in a given range:

Printing Leap Years in a Range

10 Fun Python Exercises for Kids
10 Fun Python Exercises for Kids
OOP in Python ✨ | Classes, Objects & 4 Pillars
OOP in Python ✨ | Classes, Objects & 4 Pillars
9 Python Projects for Beginners (Build Your Portfolio Fast)
9 Python Projects for Beginners (Build Your Portfolio Fast)
Python For Everything – Top Libraries & What They’re Used For
Python For Everything – Top Libraries & What They’re Used For
Python Programming, Python
Python Programming, Python
30 Python Projects For Beginners and Advanced Learners (2023)
30 Python Projects For Beginners and Advanced Learners (2023)
the top 10 python tricks every beginer should know
the top 10 python tricks every beginer should know
Python Learning Path for Beginners 2026 πŸš€
Python Learning Path for Beginners 2026 πŸš€
PYTHON PROJECTS FOR BEGINNERS
PYTHON PROJECTS FOR BEGINNERS
Learn Python in 10 Days | Complete Python Roadmap for Beginners | Python Learning Guide
Learn Python in 10 Days | Complete Python Roadmap for Beginners | Python Learning Guide
Learn and practice Python programming (Python Coding)
Learn and practice Python programming (Python Coding)
For Loop & While Loop Explained | Python for Beginners
For Loop & While Loop Explained | Python for Beginners
Python for Beginners: 8 Critical Mistakes to AVOID! πŸπŸ›‘
Python for Beginners: 8 Critical Mistakes to AVOID! πŸπŸ›‘
Python Programming Training
Python Programming Training
DAY 2 | Python Variables, Data Types & User Input
DAY 2 | Python Variables, Data Types & User Input
the top 5 python project ideas
the top 5 python project ideas
25 Python Project Ideas for Beginners (With Tech Stack)
25 Python Project Ideas for Beginners (With Tech Stack)
Python Lists – Master the Most Used Data Structure
Python Lists – Master the Most Used Data Structure
What Is the Best First Programming Language?
What Is the Best First Programming Language?
Python Roadmap for Beginners 2026  β€” Step by Step Guide
Python Roadmap for Beginners 2026 β€” Step by Step Guide

```python start_year = 2000 end_year = 2050 for year in range(start_year, end_year + 1): if is_leap_year(year): print(year) ```

This script loops through all years in the range 2000 to 2050 (inclusive), and prints the ones that are leap years.

Optimizing the Leap Year Function

However, looping through years and using the `is_leap_year` function for each one can be slow for large ranges. We can optimize our function by only checking the divisibility by 400 once. Here's an optimized version:

```python def is_leap_year_optimized(year): if year % 400 == 0: return True elif year % 100 == 0: return False else: return year % 4 == 0 ```

This version of the function first checks if the year is divisible by 400. If it is, it returns `True` immediately. If the year is not divisible by 400, it then checks if it's divisible by 100. If it's not, it checks if it's divisible by 4. This way, we avoid unnecessary divisions and conditional checks, making our function much faster.

As we wrap up our exploration of leap years in Python, let's note that understanding and working with leap years can be both interesting and practical. It's a great example of how simple calculations and logical operations in programming can help us with real-world problems, and it's a testament to the power and versatility of Python. If you're interested in learning more about Python or algorithms in general, W3Schools is an excellent resource to explore further. Happy coding!