Write a python program to print all the months of given year.

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 31

To 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.

Write a python program to print all the months of given year.

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 31

Method 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

Get PrepInsta Prime & get Access to all 150+ courses offered by PrepInsta in One Subscription

Write a python program to print all the months of given year.

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 https://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 30

Source Code in PyCharm IDE:

Write a python program to print all the months of given year.

Related Python Examples:

  1. Python program to reverse a String using recursion
  2. Python program to check leap year
  3. Python program to check prime number
  4. Python program to add two matrices

How do I print the month of the year in Python?

“how to print month in python” Code Answer's.
import datetime..
date = '2021-05-21 11:22:03'.
datem = datetime. datetime. strptime(date, "%Y-%m-%d %H:%M:%S").
print(datem. day) # 25..
print(datem. month) # 5..
print(datem. year) # 2021..
print(datem. hour) # 11..
print(datem. minute) # 22..

How do I print a whole year calendar in Python?

Source Code: import calendar //import the calendar module def printcalendar(yr): # printing calendar print(calendar. calendar(yr)) # driver program yr = 2019 printcalendar(yr) Output: The whole calendar year of 2019 will be displayed as the output.

How do you write months in Python?

To get the month name from a number in Python, the easiest way is with strftime() and passing “%M”. You can also use the calendar module and the month_name() function.

How do you write a calendar program in Python?

It is simple in python programming to display calendar. To do so, you need to import the calendar module which comes with Python..
import calendar..
# Enter the month and year..
yy = int(input("Enter year: ")).
mm = int(input("Enter month: ")).
# display the calendar..
print(calendar. month(yy,mm)).