Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
Leap Year Program in Python (Check Leap Year or Not in Python)
In our journey through time, certain years possess a mystical quality known as a "leap year." These extraordinary occurrences, shrouded in mathematical precision, intrigue and captivate our imaginations. In computer programming, Python unveils its prowess yet again, providing us with the Python program to check leap year.
A leap year is a year that has an extra day, February 29th, instead of the usual 28 days. It occurs every four years to keep our calendar in sync with the Earth's orbit around the Sun. Leap years are necessary because the Earth's orbit takes around 365.25 days, so the extra day helps to make up for a fraction of a day.
By leveraging the power of coding, we can uncover the underlying principles behind leap years, exploring the intricacies of the algorithm and program on how to find leap year in Python. Through coding examples, step-by-step guidance, and insightful explanations, we will understand it in simple terms.
Within the vast landscape of time, understanding leap years becomes a valuable asset for various applications. From date calculations and event scheduling to financial and scientific computations, the ability to check leap year or not in Python brings new possibilities.
Python Program to Check Leap Year
Code
# Leap year program in Python using if else
year = int(input("Enter a year: "))
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
else:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
Output
Enter a year: 2022
2022 is not a leap year
Explanation
In this program, we first prompt the user to enter a year using the input() function. The input is converted to an integer using the int() function and stored in the variable year.
We then use a series of if statements to check whether it is a leap year or not in Python. The logic is as follows:
-
If the year is divisible by 4, it could potentially be a leap year.
-
If the year is divisible by 100, it must also be divisible by 400 to be considered a leap year.
-
If the year is divisible by 4 and not divisible by 100, it is a leap year.
Based on the conditions above, we print the corresponding message to indicate whether the year is a leap year or not.
When you run this program in Python compiler, it will prompt you to enter a year. After you provide the input, it will determine if the year is a leap year or not and display the result accordingly.
Leap Year Program in Python Using Function
Code
# Check leap year in Python using function
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
# Get input from the user
year = int(input("Enter a year: "))
# Call the function and check if it's a leap year
if is_leap_year(year):
print(year, "is a leap year")
else:
print(year, "is not a leap year")
Output
Enter a year: 2023
2023 is not a leap year
Explanation
Here, we define a function called is_leap_year that takes a year as an argument. The function checks the conditions for leap years as explained earlier and returns True if it's a leap year, and False otherwise.
We prompt the user to enter a year using the input() function, convert it to an integer using the int() function, and store it in the variable year.
We then call the is_leap_year function, passing in the year as an argument. Based on the return value of the function, we print the appropriate message to indicate whether the year is a leap year or not.
Leap Year Program in Python Using for loop
If you want to check multiple leap years or define a range of years to check whether it is a leap year or not in Python, then the for loop is used.
Code
# Leap year code in Python using for loop
def is_leap_year(year):
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
return True
else:
return False
# Get input from the user
start_year = int(input("Enter the starting year: "))
end_year = int(input("Enter the ending year: "))
# Check leap year for each year in the range
for year in range(start_year, end_year + 1):
if is_leap_year(year):
print(year, "is a leap year")
else:
print(year, "is not a leap year")
Output
Enter the starting year: 1999
Enter the ending year: 2023
1999 is not a leap year
2000 is a leap year
2001 is not a leap year
2002 is not a leap year
2003 is not a leap year
2004 is a leap year
2005 is not a leap year
2006 is not a leap year
2007 is not a leap year
2008 is a leap year
2009 is not a leap year
2010 is not a leap year
2011 is not a leap year
2012 is a leap year
2013 is not a leap year
2014 is not a leap year
2015 is not a leap year
2016 is a leap year
2017 is not a leap year
2018 is not a leap year
2019 is not a leap year
2020 is a leap year
2021 is not a leap year
2022 is not a leap year
2023 is not a leap year
Explanation
In this program, we define the is_leap_year function to find if a given year is a leap year or not.
We prompt the user to enter the starting and ending years of the range they want to check using the input() function, converting them to integers and storing them in start_year and end_year.
We then use a for loop with the range() function to iterate over each year in the specified range. For each year, we call the is_leap_year function and print the corresponding message indicating if it is a leap year or not.
Python Program for Leap Year Using nested if
Code
# How to find leap year using nested if
year = int(input("Enter a year: "))
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
else:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
Output
Enter a year: 2006
2006 is not a leap year
Explanation
In the above program, we prompt the user to enter a year using the input() function and convert it to an integer using the int() function, storing it in the variable year.
We then use nested if statements to check if the year is a leap year. The logic is as follows:
-
If the year is divisible by 4, it could potentially be a leap year.
-
If the year is divisible by 100, we further check if it's divisible by 400 to be considered a leap year.
-
If the year is divisible by 4 and not divisible by 100, it is a leap year.
Based on the conditions above, we print the corresponding message to indicate whether the year is a leap year or not.
One-line Python Code for Leap Year
Here is how to check leap year in Python in one line:
Code
year = int(input("Enter a year: "))
print(year, "is a leap year" if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) else year, "is not a leap year")
Output
Enter a year: 2001
2001 2001 is not a leap year
Explanation
In this one-liner, we prompt the user to enter a year using the input() function and convert it to an integer using the int() function, storing it in the variable year.
The print() statement uses a conditional expression (if-else shorthand) to check if the year is a leap year. If the condition (year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)) evaluates to True, it prints that the year is a leap year.
Otherwise, it prints that the year is not a leap year.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Program to Find Square Root of a Number
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program