Beginner Python Exercises

Check a Leap Year in Python

Leap years are divisible by four, except century years not divisible by 400.

What is Check a Leap Year?

Leap years are divisible by four, except century years not divisible by 400.

Apply Gregorian leap-year rules.

When should you use it?

  • Learn Python syntax through a practical example.
  • Build a foundation for larger programming problems.
  • Test an idea quickly without a local setup.

Example code

Run code →
main.py
year = 2024
# Century years must also be divisible by 400.
leap = year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)
print(f"{year} is {'a' if leap else 'not a'} leap year")

Expected output

2024 is a leap year

How it works

The boolean expression handles the century exception explicitly.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in Python IDE →