Editor

Days in a Month

#540 · Python Basics · Beginner · 30 XP · about 5 min

Write days_in_month(month, year) that returns how many days a month has. month is a number from 1 (January) to 12 (December).

  • April, June, September and November (months 4, 6, 9 and 11) have 30 days.
  • February (month 2) has 28 days, or 29 in a leap year.
  • Every other month has 31 days.

A year is a leap year when it is divisible by 4, except a year divisible by 100, which is not, unless it is also divisible by 400.

Examples

days_in_month(1, 2023)  # returns 31
days_in_month(4, 2023)  # returns 30
days_in_month(2, 2024)  # returns 29
days_in_month(2, 1900)  # returns 28

1900 is divisible by 100 but not by 400, so its February had 28 days.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.