You can check if a year is a leap year in Python using the following logic:
def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
else:
return False
# Test the function
year = int(input("Enter a year: "))
if is_leap_year(year):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
Code language: Python (python)
This code defines a function is_leap_year
that takes a year as input and returns True
if it’s a leap year and False
if it’s not. The logic checks whether the year is divisible by 4 and not divisible by 100, or if it’s divisible by 400. If either of these conditions is met, the year is considered a leap year.
What is the formula for leap year in Python?
The formula to determine if a year is a leap year can be expressed in Python as follows:
def is_leap_year(year):
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
Code language: Python (python)
This concise formula uses the same logic as mentioned earlier: a leap year is either divisible by 4 and not divisible by 100, or it is divisible by 400. The is_leap_year
function will return True
if the year is a leap year and False
otherwise.
Is 1992 a leap year, use Python?
Yes, 1992 is a leap year in Python, as well as in the Gregorian calendar. You can use the is_leap_year
function mentioned earlier to confirm this:
def is_leap_year(year):
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
year = 1992
if is_leap_year(year):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
Code language: Python (python)
When you run this code, it will print: “1992 is a leap year.” This is because 1992 satisfies the conditions for a leap year (divisible by 4 and not divisible by 100).
Read More;
- What Famous Things (Companies) Use Python?
- How to Use Python to Earn Money?
- What are local variables and global variables in Python with example?
- What Is Python Boto3 With Example
- Is Python Case-sensitive When Dealing With Identifiers
- How To Check If The List Is Empty In Python
- What Is PythonPath With Example
- What Is Python Wheel?
- Python For ‘int’ Object Is Not Iterable [Solution]
- What is elif Statement in Python With Example
- How Do You Write Q-learning in Python?
- What is nested loop in Python with example?