Using Python Check Leap Year | February 29 days

Sovary June 1, 2022 853
1 minute read

Today we will discuss how to know which year is leap or February is 29 days in Python. Basically a year have 365 days, each leap year have 366 days because additional day is added to February which is 29 days.

To able coding program we should know how algorithm work behind the scene. Please check below concept before write code.

 

 

Algorithm

Leap year is not satified condition as below

  1. If a year divided by 4 and having remainder. Ex: 2013
  2. If a year divided by 4 or 400 having remainder. Ex 1900

To implement this algorithm you have to understand following Python basic:

  • Python Operator
  • Python If-Else Condition
  • Python Module

Look at below example snippet below to understand: 

Example #1 - Using Algorithm

# Suppose user input 1900 as year to check
year = 1900

if (year % 4) == 0 and (year % 400) == 0:
  print(year,"is a leap year")
else:
  print(year,"is not a leap year")

 Output:

1900 is not a leap year

 

 

Example #2 - Using Module Calendar

# using module
import calendar

# Suppose user input 1900 as year to check
year = 2024
if calendar.isleap(year):
  print(year,"is a leap year")
else:
  print(year,"is not a leap year")

 Output:

2024 is a leap year

Hope you learn something in this page. Thank you.



You might Also Like:

Python 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him