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 input
2year = int(input("Enter a year: "))
3
4# Check if leap year
5if (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 conditions
First condition: divisible by 4 but not by 100
Second condition: divisible by 400
Either condition makes it a leap year