Check if a Given Year is a Leap Year
Determine if a year is a leap year based on leap year rules.
BeginnerPhase 1 - Level 1: Simple ConditionsExample 5 of 10
check-if-a-given-year-is-a-leap-year.py
1# Take year as input2year = int(input("Enter a year: "))34# Check if leap year5if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):6 print("Leap year")7else:8 print("Not a leap year")
Output
Enter a year: 2024 Leap year Enter a year: 2023 Not a leap year Enter a year: 2000 Leap year
What's going on
Leap year rules:
1.Divisible by 4 AND not divisible by 100, OR
2.Divisible by 400
Key Concepts:
Use
or to combine two conditionsFirst condition: divisible by 4 but not by 100
Second condition: divisible by 400
Either condition makes it a leap year
🔗Related Content
- 📖
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- 📝
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- 💻
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum