Leap Year Program in Python
Check if a Year is a Leap Year or Not in Python
Given an integer input year, the objective of the code is to Check if a Year is a Leap Year or Not in Python Language. To do so we’ll check if the year input satisfies either of the two conditions of leap year.
Example Input : 2020 Output : It's a Leap Year.
Check Whether a Year is a Leap Year or Not in Python
Given an integer input as the year, the objective is to Check if a Year is a Leap Year or Not in Python Language. To do so we’ll check each condition mentioned below in the blue box. It either of the conditions is satisfied, the year is a leap year. It’s not otherwise. Here are some methods to check whether or not it’s a leap year
- Method 1: Using if-else statements 1
- Method 2: Using if-else statements 2
- Method 3 : Using Ternary Operator
- Method 4 : Using Calendar Mode
- Method 5 : Using Lamda Function
We’ll discuss all the above mentioned methods in detail in the upcoming sections. Before going for the approach read out the blue box below for better understanding of the concept.
1. The year must be perfectly divisible by 400.
2. The number must be divisible by 4 but not by 100.
Method 1: Using if-else Statements 1
In this method we’ll use the if-else statements to check whether or not the input integer satisfies either of the conditions. To learn more about control statements in python, check out if-else control statements in Python.
Working
For a User Input year we do
- Check if the year variable is divisible by 400.
- Check if the year variable is divisible only by 4 and not 100.
- If the above mentioned conditions are satisfied, prints “Leap Year”, print “Not a Leap Year” otherwise.
Let’s implement the above logic in Python Language.
Python Code
year = 2000
if (year%400 == 0) or (year%4==0 and year%100!=0): print("Leap Year") else: print("Not a Leap Year")
Output
Leap Year
Method 2: Using if-else Statements 2
In this method we’ll use the if-else statements to check whether or not the input integer satisfies either of the conditions. This method is a modified and simpler version of the previous method.
Working
For a User Input year we do
- The input is stored in an int type variable say year.
- year is checked for being a leap year or not with the following condition if( ((year % 4 == 0)&&(year % 100 != 0)) || (year % 400==0) )
- If the above condition is true then input is a leap year otherwise input is not a leap year.
Let’s implement the above logic in Python Language.
Python Code
year = 2000 if( ((year % 4 == 0) and (year % 100 != 0)) or (year % 400==0) ): print("Leap Year") else: print("Not leap Year")
Output:
Leap Year
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Method 3: Using Ternary Operator
In this method we’ll use ternary operator in Python
Python Code
def check_leap(year): return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) year = int(input("Enter a year: ")) print(f"{year} is leap year" if check_leap(year) else f"{year} is not leap year")
Method 4: Using Calendar Module
In this method we’ll use calendar mode in python
Python Code
import calendar year = int(input("Enter a year: ")) if calendar.isleap(year): print(year, " is a leap year") else: print(year, "is not a leap year.")
Python Code
Method 5: Using Lamda Function
In this method we’ll use lamda function in python
is_leap_year = lambda year: (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) year = int(input("Enter a year: ")) if is_leap_year(year): print(year, " is a leap year") else: print(year, "is not a leap year.")
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
year = int(input(“Enter the year : “))
if (year % 4 == 0) & (year % 100 != 0) | (year % 400 == 0):
print(” This is a leap year”)
else:
print(“This is not a leap year”)
n=int(input(“Enter the year:”))
if((n%4==0)and(n%100!=0)or(n%400==0)):
print(n,”is Leap Year”)
else:
print(n,”is not a leap year”)
X=int(input(“Enter any Year: “))
if X%4==0 & X%400==0:
print(“This is Leap Year.”)
elif X%100==0:
print(“This is not a Leap Year.”)
else:
print(“This is not a leap Year..”)
More accurate code:
year = int(input(“Enter the year: “))
if ((year%4 == 0 and year%100 != 0) or (year%100 == 0 and year%400 == 0)):
print(year, ” is a leap year”)
else:
print(year, “is not a leap year”)
year = int(input(“enter year to check “))
if (year % 4 == 0 and year % 100 == 0) or (year % 400 != 0):
print(“{} is leap year”.format(year))
else:
print(“{} is not leap year”.format(year))
year = int(input(‘enter year: ‘))
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print(f’yes {year} is a leap year ‘)
else:
print(f’no {year} is not a leap year ‘)
else:
print(f’no {year} is a leap year ‘)
else:
print(f’no {year} is not a leap year ‘)
much simple code:
n = int(input(‘enter the year’))
if n % 4 == 0 and n % 100 != 0 or n % 400 == 0:
print(n, ‘is a leap year’)
else:
print(n, ‘is not a leap year’)
year=int(input(“enter a year:”)
if year%4==0 and year%100==0 and year%400==0:
print(“{} is a leap year”.format(year))
else:
print(“{} is not a leap year”.format(year))
Y=int(input(“Enter the year”))
if y%4==0 and y%100!=0:
Print(y,”is a leap year”)
elif y%400==0:
print(y,”is a leap year”)
else :
Print(y,”is not a leap year”)
Y=int(input(“Enter the year”))
if y%4==0 and y%100!=0:
Print(y,”is a leap year”)
elif y%400==0:
print(y,”is a leap year”)
else :
Print(y,”is not a leap year”)
Correct one:
year=int(input(“year: “))
if(year%4==0 and year%100!=0 or year%400==0)
print(“given year is leap year”):
else:
print(“not a leap year”)
Correct one:
year = int(input(“Enter a Year: “))
if year%4 == 0:
if year%100==0 and year%400 !=0:
print(year,” is not a Leap Year”)
else:
print(year,” is Leap Year”)
else:
print(year,” is not a Leap Year”)
Thanks Bhavantik for contributing the code
year=int(input(“enter year:”))
if(checkYear(year)):
print(“Leap Year”)
else:
print(“Not a Leap Year”)