Counting number of days in a given month of a year
Detailed guide and Python implementation for the 'Counting number of days in a given month of a year' problem.
Problem Statement
Write a function days_in_month(month, year) that takes an integer month (1-12) and an integer year, and returns the number of days in that month.
Remember:
- Months with 31 days: January(1), March(3), May(5), July(7), August(8), October(10), December(12)
- Months with 30 days: April(4), June(6), September(9), November(11)
- February(2): 28 days normally, 29 days in a leap year
- Leap year: divisible by 4, but not by 100, unless also divisible by 400
- •1 <= month <= 12
- •1 <= year <= 9999
Examples
days_in_month(2, 2024)
29
2024 is divisible by 4 and not by 100, so it's a leap year. February has 29 days.
days_in_month(1, 2023)
31
January always has 31 days regardless of the year.
days_in_month(2, 1900)
28
1900 is divisible by 100 but not by 400, so it's NOT a leap year. February has 28 days.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Variables & Data Types Explained
Understand Python variables and core data types (strings, integers, floats, booleans). A complete beginner guide to memory assignment in Python.
How to Sort a List in Python
Learn how to sort a list in Python using the sort() method and the sorted() function. Discover custom key sorting and reverse order examples.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.