Saturday, 4 April 2026

πŸš€ Day 12/150 – Check Leap Year in Python

 

Welcome back to the 150 Days of Python series! 🎯
Today, we’re solving a real-world logic problem — checking whether a year is a leap year.

This is a great exercise to understand:

  • Conditional statements
  • Logical operators (and, or)
  • Writing clean, readable code

 Problem Statement

Write a Python program to check whether a given year is a Leap Year or Not.


 Understanding Leap Year Logic

Before coding, let’s understand the rules:

A year is a leap year if:

✔ Divisible by 4
❌ But NOT divisible by 100
✔ EXCEPTION: If divisible by 400, then it is a leap year


Year Result
2024 ✅ Leap Year
1900 ❌ Not Leap Year
2000 ✅ Leap Year


 Method 1 – Using if-elif-else

year = 2024 if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): print("Leap Year") else: print("Not a Leap Year")








 Code Explanation:

  • year % 4 == 0
    πŸ‘‰ Checks if year is divisible by 4
  • year % 100 != 0
    πŸ‘‰ Ensures it is NOT divisible by 100
  • year % 400 == 0
    πŸ‘‰ Special case: makes century years valid
  • and → both conditions must be true
  • or → at least one condition must be true

πŸ‘‰ This full condition ensures accurate leap year calculation

 Method 2 – Taking User Input

year = int(input("Enter a year: ")) if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): print("Leap Year") else: print("Not a Leap Year")






πŸ” What’s new here?
  • input() → takes user input
  • int() → converts string input into integer

πŸ‘‰ Always convert input when dealing with numbers!

Method 3 – Using a Function

def check_leap_year(year): if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): return "Leap Year" else: return "Not a Leap Year" print(check_leap_year(2024))







πŸ” Why use a function?
  • Code becomes reusable
  • Cleaner structure
  • Easy to test

πŸ‘‰ You can call this function anytime with different values


Method 4 – Using Python calendar Module

import calendar year = 2024 if calendar.isleap(year): print("Leap Year") else: print("Not a Leap Year")








πŸ” Explanation:
  • calendar.isleap(year)
    πŸ‘‰ Built-in Python function
    πŸ‘‰ Returns True or False

πŸ‘‰ This is the simplest and most reliable method

Important Things to Remember

✔ Always use correct logical condition
✔ Don’t forget parentheses ( ) in conditions
✔ Convert input using int()
✔ Built-in functions save time and reduce errors


Summary

MethodConcept
if-elseBasic logic
User InputReal-world usage
FunctionReusability
calendar modulePythonic approach

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (234) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (28) Azure (10) BI (10) Books (262) Bootcamp (2) C (78) C# (12) C++ (83) Course (87) Coursera (300) Cybersecurity (30) data (5) Data Analysis (29) Data Analytics (20) data management (15) Data Science (337) Data Strucures (16) Deep Learning (142) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (68) Git (10) Google (51) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (275) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (13) PHP (20) Projects (32) pytho (1) Python (1281) Python Coding Challenge (1118) Python Mistakes (50) Python Quiz (462) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (48) Udemy (18) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)