Easy Tutorial
❮ Python Func Tuple Python Basic Syntax ❯

Python Calculation of Days in Each Month

Python3 Examples

The following code calculates the number of days in each month by importing the calendar module:

Example (Python 3.0+)

#!/usr/bin/python3
# author by : www.tutorialpro.org

import calendar
monthRange = calendar.monthrange(2016,9)
print(monthRange)

Executing the above code produces the following output:

(3, 30)

The output is a tuple where the first element represents the day of the week (0-6) for the first day of the month being queried, and the second element is the number of days in that month. The output above indicates that the first day of September 2016 was a Thursday, and the month had 30 days.

Python3 Examples

❮ Python Func Tuple Python Basic Syntax ❯