How to Check Whether a Year is a Leap Year in Python
In this tutorial, we will learn how to program "How to Check Whether a Year is a Leap Year in Python." The objective is to determine whether the entered year qualifies as a leap year. This tutorial will walk you through the implementation in Python, using simple conditional logic to check the necessary rules for identifying a leap year. By the end of this tutorial, you will clearly understand how to apply these conditions efficiently in your Python programs.
This topic is straightforward to understand. Just follow the instructions provided, and you will be able to complete it with ease. The program demonstrated will show you the correct and efficient way to check whether a year is a leap year. So, let’s dive into the coding process!
Getting Started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Creating Main Function
This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.- while True:
- print("\n============== Check Whether a Year is a Leap Year ==============\n")
- year=int(input("Enter a year: "))
- if(year%4==0 and year%100!=0 or year%400==0):
- print("The year is a leap year!")
- else:
- print("The year isn't a leap year!")
- opt = input("\nDo you want to try again?(yes/no): ")
- if opt.lower() == 'yes':
- ret=False
- elif opt.lower() == 'no':
- ret=True
- print("Exiting program....")
- else:
- print("Please enter yes/no:")
- break
- if ret == False:
- continue
This Python program checks whether a given year is a leap year. It prompts the user to enter a year and uses the standard leap year condition: a year is a leap year if it is divisible by 4 and not divisible by 100, unless it is also divisible by 400. The result is printed, and the program continues or exits based on the user’s input.
Output:

There you have it we successfully created How to Check Whether a Year is a Leap Year in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
More Tutorials for Python Language
Add new comment
- 13 views