Program to Check Leap Year in Python (2024)

22 Aug 2024

Beginner

16K Views

14 min read

Calculate the leap year in Python

The leap yearsareaspecial year in our calendar. It just adds an extra day to February every four years. But, not every year gets this special extra day! Now I am sure you have got one question about how to recognize whether the year is a leap year or not. That's why I brought you a Python program that will determine the leap year.

So, In this Python Tutorial, We will calculate a leap year using Python which will contain rules for determining if a year is a leap year, The leap-year program in Python in detail with code examples, how to check Leap Year Using Macros, So Let's see one by one.

How to check a leap year?

The logic :

Program to Check Leap Year in Python (1)

Now Look at the following program to understand its implementation in Python Compiler.

Example

def CheckLeap(Year): # Checking if the given year is leap year  if((Year % 400 == 0) or (Year % 100 != 0) and (Year % 4 == 0)): print("Yes! This Year is a leap Year"); # Else it is not a leap year  else: print ("This Year is not a leap Year") # Taking an input year from user Year = int(input("Enter the number here: ")) # Printing result CheckLeap(Year) 

Output

Enter the number here: 1700Yes! This year is not a leap Year 

Explanation

See, We have executed all three conditions that we have above in the program using the 'and' and 'or' keywords inside the if else condition. After getting the input from the user, the program first, and then we will go to the input part and check if the given year is a leap year. If the condition is satisfied, the program will print 'Leap Year'; else the program will print 'Not a leap year.

The Leap Year Program in Python: For Loop

Let's calculate Leap year program using Python For loop.

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 Falsestart_year = int(input("Enter the start year: "))end_year = int(input("Enter the end year: "))print("Leap years between", start_year, "and", end_year, "are:")for year in range(start_year, end_year + 1): if is_leap_year(year): print(year)

Output

Enter the start year: 2020Enter the end year: 2024Leap years between 2020 and 2024 are:20202024

Explanation

By applying the leap year logic and utilizing the Python code with the for loop provided, you'll be able to effortlessly determine whether a given year is a leap year or not.

Program to Check Leap Year: Macros

In Python, macros aren't a local concept like in languages such as C or C++. In any case, you'll achieve similar functionality utilizing functions or constants. Let's see how to create a Python program that checks for a leap year utilizing macros-like constants.

Creating Constants:

Now Let's define constants for the conditions of a leap year,

Leap_Condition1=4Leap_Condition2=100Leap_Condition3=400

Defining the Function of a Leap Year:

def is_leap year(year): if (year % Leap_Condition1 == 0 and year %Leap_Condition2 ! = 0 ) or ( year % Leap_Condition3 == 0 ):return True else : return False

Example

def is_leap_year(year): # Function to check if a year is a leap year if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): return True else: return Falseyears_to_check = [2000, 2018, 2100, 2024]for year in years_to_check: if is_leap_year(year): print(f"{year} is a leap year.") else: print(f"{year} is not a leap year.")

Output

2000 is a leap year 2018 is not a leap year2100 is not a leap year2424 is a leap year

By executing a leap year checking function with constants that mimic macros, you'll be able to effectively decide leap years in your Python programs.

Python Short Solution of Leap Year Program

Determining leap years in Python can be achieved through various concise methods. Let's explore each of these in detail, complete with explanations and illustrative examples.

1. Leap Year Program in Python: Nested-if Condition

year = 2022if 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.")

Output

2022 is not a leap year

The nested-if approach checks leap year conditions sequentially using nested if statements.

2. Leap Year Program in Python: if-else Condition

This method uses a single if-else statement to check leap year conditions directly.

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

3. Leap Year Program in Python Using the Calendar Module

The calendar module's isleap() function simplifies leap year determination.

import calendar year = 2000 if calendar.isleap(year): print(f"{year} is a leap year.") else : print(f" {year} is not a leap year.")

Leap Year Program in Python: While Loop

This determines if a particular year is a leap year or not. It continuously seeks the user for input using a Python while loop until they submit valid input. Here's a quick example:

while True: try: year = int(input("Enter a year: ")) if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): print(f"{year} is a leap year.") else: print(f"{year} is not a leap year.") break # Exit the loop if valid input is provided except ValueError: print("Wrong Year. Please enter a valid year.")

Output

Enter a year: 20242024 is a leap year.

Explanation

Here, the while loop keeps running until the user enters a valid value meaning year, represented by an integer. Using the leap year logic, it determines whether the current year is a leap year and outputs the appropriate result.

Conclusion

So We have learned about both basic and sophisticated approaches through our exploration of several programs for calculating leap years in Python. However, to simplify development and up your game, learn the intricacies of Python with Scholarhat and level up as a Python developer.Also if you are preparing for interview don't forget to revise this top 50 Interview Questions and Answers in Python.

FAQs

Q1. How to check leap Python?

We can check the leap year by checking the divisibility of the year with 4 and 400.

Q2. Is 3000 a leap year?

3000 is a four-digit number that may also be divided by 100. However, because 3000 is divisible by 400, it will not be a leap year.

Q3. What is leap year logic?

year % 4 == 0 && year % 100 != 0 || year % 400 == 0

Program to Check Leap Year in Python (2024)
Top Articles
Commodity Prices
Do Not Google "Goose Teeth"
Joliet Patch Arrests Today
Cars & Trucks - By Owner near Kissimmee, FL - craigslist
Katmoie
Tj Nails Victoria Tx
Comcast Xfinity Outage in Kipton, Ohio
414-290-5379
Craigslist Dog Kennels For Sale
Purple Crip Strain Leafly
Help with Choosing Parts
How to Store Boiled Sweets
Fredericksburg Free Lance Star Obituaries
Tracking Your Shipments with Maher Terminal
Letter F Logos - 178+ Best Letter F Logo Ideas. Free Letter F Logo Maker. | 99designs
Q33 Bus Schedule Pdf
Kiddle Encyclopedia
Conan Exiles: Nahrung und Trinken finden und herstellen
Palm Springs Ca Craigslist
Traveling Merchants Tack Diablo 4
Aerocareusa Hmebillpay Com
Glover Park Community Garden
Engineering Beauties Chapter 1
Silky Jet Water Flosser
What Individuals Need to Know When Raising Money for a Charitable Cause
Makemv Splunk
Pain Out Maxx Kratom
Delta Township Bsa
Cal State Fullerton Titan Online
Aes Salt Lake City Showdown
Hrconnect Kp Login
Combies Overlijden no. 02, Stempels: 2 teksten + 1 tag/label & Stansen: 3 tags/labels.
Rugged Gentleman Barber Shop Martinsburg Wv
Ff14 Sage Stat Priority
Kempsville Recreation Center Pool Schedule
Devotion Showtimes Near The Grand 16 - Pier Park
Aladtec Login Denver Health
Nobodyhome.tv Reddit
Raisya Crow on LinkedIn: Breckie Hill Shower Video viral Cucumber Leaks VIDEO Click to watch full…
Lyca Shop Near Me
NHL training camps open with Swayman's status with the Bruins among the many questions
Google Flights Orlando
Indio Mall Eye Doctor
Bartow Qpublic
The Attleboro Sun Chronicle Obituaries
Post A Bid Monticello Mn
Kaamel Hasaun Wikipedia
Hello – Cornerstone Chapel
Secrets Exposed: How to Test for Mold Exposure in Your Blood!
Horseneck Beach State Reservation Water Temperature
Houston Primary Care Byron Ga
Arre St Wv Srj
Latest Posts
Article information

Author: Clemencia Bogisich Ret

Last Updated:

Views: 6139

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Clemencia Bogisich Ret

Birthday: 2001-07-17

Address: Suite 794 53887 Geri Spring, West Cristentown, KY 54855

Phone: +5934435460663

Job: Central Hospitality Director

Hobby: Yoga, Electronics, Rafting, Lockpicking, Inline skating, Puzzles, scrapbook

Introduction: My name is Clemencia Bogisich Ret, I am a super, outstanding, graceful, friendly, vast, comfortable, agreeable person who loves writing and wants to share my knowledge and understanding with you.