Using Python Check Leap Year | February 29 days

Sovary June 1, 2022 896
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

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him