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
What are the NHS values
The Next Crypto Bull Run (2022 Update)
Pollen Count Centreville Va
My Arkansas Copa
Dricxzyoki
Ffxiv Palm Chippings
News - Rachel Stevens at RachelStevens.com
Craigslist Motorcycles Jacksonville Florida
Comforting Nectar Bee Swarm
How to change your Android phone's default Google account
Hendersonville (Tennessee) – Travel guide at Wikivoyage
O'reilly's In Monroe Georgia
Category: Star Wars: Galaxy of Heroes | EA Forums
Skip The Games Norfolk Virginia
Paketshops | PAKET.net
Monticello Culver's Flavor Of The Day
Shemal Cartoon
Calmspirits Clapper
2021 Lexus IS for sale - Richardson, TX - craigslist
Jesus Calling Oct 27
Tcu Jaggaer
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Katherine Croan Ewald
Stardew Expanded Wiki
Labby Memorial Funeral Homes Leesville Obituaries
How to Watch the Fifty Shades Trilogy and Rom-Coms
Rqi.1Stop
Sea To Dallas Google Flights
Mybiglots Net Associates
27 Paul Rudd Memes to Get You Through the Week
January 8 Jesus Calling
FSA Award Package
Craigs List Tallahassee
Math Minor Umn
Chicago Pd Rotten Tomatoes
Morlan Chevrolet Sikeston
Log in or sign up to view
The Mad Merchant Wow
Grapes And Hops Festival Jamestown Ny
Is Arnold Swansinger Married
301 Priest Dr, KILLEEN, TX 76541 - HAR.com
Discover Things To Do In Lubbock
boston furniture "patio" - craigslist
About Us
Goats For Sale On Craigslist
Darkglass Electronics The Exponent 500 Test
552 Bus Schedule To Atlantic City
Used Sawmill For Sale - Craigslist Near Tennessee
Pilot Travel Center Portersville Photos
Kidcheck Login
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.