If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In Python, you can determine if a given year is a leap year or not using conditional statements. A leap year is a year that is exactly divisible by 4, except for years that are divisible by 100. However, years that are divisible by 400 are considered leap years. Here's how you can check if a year is a leap year:
pythonCopy code
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 # Example usage: year = 2024 if is_leap_year(year): print(f"{year} is a leap year.") else: print(f"{year} is not a leap year.")
In this example, the
is_leap_year()
function takes a year as input and uses nestedif
statements to check the conditions for leap years. If the year is exactly divisible by 4, it enters the first level of theif
statement. If it is divisible by 100, it checks if it is also divisible by 400 to determine if it is a leap year. If all conditions are met, the function returnsTrue
, indicating that the year is a leap year. Otherwise, it returnsFalse
.You can call the
is_leap_year()
function with different years to check if they are leap years or not. The function will returnTrue
for leap years andFalse
for non-leap years.
Comments: 0