Write a Python program to print a calendar for month
Source Code
import calendar y = int(input("Enter the Year :")) m = int(input("Enter the Month of Number :")) print(calendar.month(y,m))Output
Enter the Year :2023 Enter the Month of Number :10 October 2023 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31To download raw file Click Here
- Previous
- Next
Here, in this page we will discuss the program to find the number of days in a given month of a given year in python . Number of days in any month of a year can vary specifically in
February as the cycle of leap year repeats in every 4 years when the year is leap February gives the count to 29 days but the when the year is not leap it gives count to 28 days and so no of days in a year varies from 365 to 366.
Method Discussed :
- Method 1: Using if-else ladder.
- Method 2 : Using Array
Method 1 :
- Check if the given month is February.
- If True Check if the year is a year leap or not.
- If year is a leap year Print 29 Days, Else Print 28 Days.
- If Condition in Step 3 is False Check the month.
- Print the number of days assigned to specific Month.
Method 1 : Code in Python
Run
month = 12 year=2012 if((month==2) and ((year%4==0) or ((year%100==0) and (year%400==0)))) : print("Number of days is 29"); elif(month==2) : print("Number of days is 28"); elif(month==1 or month==3 or month==5 or month==7 or month==8 or month==10 or month==12) : print("Number of days is 31"); else : print("Number of days is 30");Output
Number of days is 31Method 2 :
In this method instead of using if-else-if ladder or switch case, we use array.
Method 2 : Code in Python
Run
arr = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ] month = 12 year=2012 if(month==2 and ((year%400==0) or ((year%100!=0) and (year%4==0)))) : print("Number of days is ", arr[month-1]+1) else : print("Number of days is ", arr[month-1]);Output
Number of days is 31
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 150+ courses offered by PrepInsta in One Subscription
We can use the built-in function month() of Calendar module to print a Calendar in Python. The function month() requires two arguments to display a Calendar of given month, first argument is the year in four digits format for example, 2003, 1997, 2018 etc. and second is the month in two digit format, for example 01, 04, 12 etc.
This program prompts the user to enter the year and month and based on the input it calls the month() function which displays the Calendar of the given month for a given year.
# Program published on //beginnersbook.com # Python program to print Calendar using # Python built-in function import calendar # Enter in format - 2018, 1997, 2003 etc. year = int(input("Enter Year: ")) # Enter in format - 01, 06, 12 etc. month = int(input("Enter Month: ")) # printing Calendar print(calendar.month(year, month))Output:
Enter Year: 2018 Enter Month: 06 June 2018 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30Source Code in PyCharm IDE:
Related Python Examples:
- Python program to reverse a String using recursion
- Python program to check leap year
- Python program to check prime number
- Python program to add two matrices