In this python tutorial, you will learn how to Check for Leap Year using the if, else, the == equality operator, modulus operator % and print function of the python programming language.
A leap year is exactly divisible by 4 except for century years , that is the years ending with double zero (eg: 1900), the century year is a leap year only if it is perfectly divisible by 400(eg: 2000)
How to Check for Leap Year?
Let’s take a look at the source code , here the values are given as input by the user in the code, the if and else statements along with the == equality operator and modulus operator % carry out the function.
RUN CODE SNIPPET# Python program to check if year is a leap year or not
year = int(input("Enter a year: "))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("\n{0} is a leap year".format(year))
else:
print("\n{0} is not a leap year".format(year))
else:
print("\n{0} is a leap year".format(year))
else:
print("\n{0} is not a leap year".format(year))INPUT:
2000
OUTPUT:
Enter a year: 2000 is a leap year
- Here we give the user the option to enter the values and the input values are scanned using the
inputfunction and are stored in the variableyearwith the statements/strings("Enter a year: "),we use theintfunction and declare the input value as a integer value. - In the STDIN section of the code editor the input values are entered.
- We declare the
ifstatement with the conditions as(year % 4) == 0:, anifstatement with the condition(year % 100) == 0:and anifstatement with the condition(year % 400) == 0:, each which are followed by a colon:. - The
printstatements display the("\n{0} is a leap year".format(year))or the statement("\n{0} is a leap year".format(year)), the variables{0}will hold the value ofyearwhere theformatfunction helps in variable substitution and data formatting. - The respective
printfunction with statements("\n{0} is a leap year".format(year))or("\n{0} is a not a leap year".format(year))will be displayed according to the satisfied conditions given below, with respect to theelsestatements.
- If a year is evenly divisible by 4 means having no remainder, it moves to the next step. If it is not divisible by 4, it is not a leap year. (Eg: 1997 )
- If a year is divisible by 4, but not by 100,( Eg: 2012) it is a leap year. If a year is divisible by both 4 and 100,it moves to the next step.
- If a year is divisible by 100, but not by 400( Eg: 1900)then it is not a leap year, if a year is divisible by both, then it is a leap year. So 2000 is a leap year.
NOTE:
- The == equality is a comparison operator which returns True is the two items are equal and returns False if not equal.
- The modulus operator % is used to compute the reminder.
- The input() function allows a user to insert a value into a program, it returns a string value.
- The if and else statements evaluates whether an expression is true or false. If a condition is true, the “if” statement is executed otherwise, the “else” statement is executed.
- The colon : at the end of the if and else statement tells Python that the next line of code should only be run if the condition is true.
- The statement for the input function are enclosed in single quotes and parenthesis.
- The \n in the code indicates a new line or the end of a statement line or a string.
- The print statement/string to be displayed in enclosed in double quotes.