HomePythonPython Program to Check for Leap Year

Python Program to Check for Leap Year

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
  1. Here we give the user the option to enter the values and the input values are scanned using the input function and are stored in the variable year with the statements/strings ("Enter a year: ") ,we use the int function and declare the  input value as a integer value.
  2. In the STDIN section of the code editor the input values are entered.
  3. We declare the if statement with the conditions as (year % 4) == 0: , an if statement with the condition (year % 100) == 0: and an if  statement with the condition (year % 400) == 0:, each which are followed by a colon : .
  4. The print statements 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 of year where the format function helps in variable substitution and data formatting.
  5. The respective print function 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 the else statements.
  • 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.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...