Home › Python programs (Hindi)
Leap Year Program Python में — code, output और dry run
Problem statement
यह program बताता है कि दिया गया year leap है या नहीं। Rule तीन हिस्सों का है: 4 से भाग जाए तो leap, पर 100 से भी भाग जाए तो नहीं — सिवाय उस स्थिति के जब 400 से भी भाग जाए।Approach 1: simple loop
Write the rule as nested if statements, one layer per clause. This mirrors the calendar rule exactly, which makes it easy to check line by line.
year = 2024
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print("Leap year")
else:
print("Not a leap year")
else:
print("Leap year")
else:
print("Not a leap year")
# Output: Leap yearThree questions in order: does 4 divide it? does 100 divide it? does 400 divide it? Each answer sends you one level deeper or straight to the output.
Approach 2: reusable function
The same rule compresses into one boolean expression. Putting it in a function keeps the formula testable — and it can be checked against known years in one line each.
def is_leap(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
for y in [1900, 2000, 2024, 2023]:
print(y, is_leap(y))
# Output:
# 1900 False
# 2000 True
# 2024 True
# 2023 FalseRead the expression as the spoken rule: "divisible by 4, and either not divisible by 100 or divisible by 400." The parentheses are not decoration — they group the exception and its exception.
Approach 3: Pythonic alternative
The standard library already contains the rule — calendar.isleap() — and using it means the calendar authority, not your typing, is on the line.
import calendar print(calendar.isleap(2024)) # True print(calendar.isleap(1900)) # False
Know the manual version for exams and interviews; use this one in real projects.
Dry run
सबसे famous trap year 1900 से शुरू करें। 4 से भाग? हाँ। 100 से भाग? हाँ — यानी अब 400 वाला check तय करेगा। 1900 % 400 = 300 — भाग नहीं गया, इसलिए leap year नहीं। अब 2000: पहले दो जवाब same, पर 2000 % 400 = 0 → leap। 4 → 100 → 400 की यही सीढ़ी दोनों results समझाती है।Common errors
- सिर्फ 4 से भाग का check लिखना — इससे 1900 गलती से leap बन जाएगा।
- 100 और 400 के checks का क्रम बदल देना।
=और==का mix करना।- Boolean form में parentheses छोड़ना — grouping बदल जाती है।
- Sirf 2024 जैसे years से test करना — असली bug सौ साल वाले years (1900, 2100) में छुपा है।
Practice on PyDebug
Concept समझने के बाद इन free problems को browser में solve करें: