Python - Find if given year is Leap Year in Georgian Calendar
Python - Check if year is Leap Year in Georgian Calendar
In this tutorial, we will write a Python function to check if the given year is a leap year in Georgian Calendar.
Following are the criteria for an year to be Leap year.
- The year can be evenly divided by 4, is a leap year, unless:
- The year can be evenly divided by 100, it is NOT a leap year, unless:
- The year is also evenly divisible by 400. Then it is a leap year.
- The year can be evenly divided by 100, it is NOT a leap year, unless:
You may combine all these conditions into a single boolean expression.
Consider that we have our year in variable year
.
To satisfy the first condition, "The year can be evenly divided by 4, is a leap year", following condition has to be satisfied.
year%4==0
To satisfy the second condition, "The year can be evenly divided by 100, it is NOT a leap year,", following condition has to be satisfied.
year%100==0
To satisfy the third condition, " The year is also evenly divisible by 400", following condition has to be satisfied.
year%400==0
By combining above three conditions, we get the following boolean expression.
year%4==0 and not (year%100==0 and not year%400==0)
unless in the given condition translates to and not
and hence the and not operators in the above condition.
Following is the example program, in which we check if a given year is a leap year or not.
Python Program
def is_leap(year):
return year%4==0 and not (year%100==0 and not year%400==0)
year = int(input())
print(is_leap(year))
Output
2004
True
When you run the program, input() waits for your input. Enter a number and our program shall check if the given number is a leap year or not.
Summary
In this tutorial, we learned how to check if given year is a leap year of Georgian Calendar or not.