GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — Python Program to Display Calendar
Python Program to Display CalendarThis article is created to cover some programs in Python, to display calendar of a month or all months (whole year). Here are the list of programs:
Display Calendar of a MonthTo display calendar in Python, you have to ask from user to enter the Year and a month. The month must be entered as its number. For example, to display calendar of January, 2021, then enter 2021 as year input and 1 as month input like shown in the program and its output given below: import calendar print("Enter Year: ") yy = input() print("\nEnter Month Number (1-12): ") mm = input() y = int(yy) m = int(mm) print("\n", calendar.month(y, m)) Here is the initial output produced by this Python program: Now supply the input say 2021 as year, press Note - The calendar module in Python allows us to work with calendar. That is, calendar.month() prints calendar of a month. For example, calendar.month(2021, 4) prints calendar of fourth month of year 2021. Modified Version of Previous ProgramThis is the modified version of previous program. The end= used in this program to skip inserting an automatic newline using print(). This program also uses try-except to handle with invalid input. import calendar print("Enter Year: ", end="") try: yy = int(input()) print("\nEnter Month Number (1-12): ", end="") try: mm = int(input()) if mm>=1 and mm<=12: print("\n", calendar.month(yy, mm)) else: print("\nInvalid Month Number!") except ValueError: print("\nInvalid Input!") except ValueError: print("\nInvalid Input!") Here is its sample run with user input, 2021 as year and 2 as month number: In above program, the following code: if mm>=1 and mm<=12: is used to check whether the value of month number entered by user is a valid input or not. That is, if user enters month number from 1 to 12, then it is a valid month number, otherwise will treated as an invalid input. Display All Months (Whole Year) CalendarNow this program receives an input as year, and prints calendar of all months of given year. For example, if user enters 2022 as year, then whole year's calendar gets printed like shown in the program and its output given below: import calendar print("Enter Year: ", end="") try: yy = int(input()) print() mm = 1 while mm<=12: print(calendar.month(yy, mm)) mm = mm+1 except ValueError: print("\nInvalid Input!") Here is its sample run with year input 2021: The snapshot given above is upper part of complete output. Here is the lower part of complete output produced by above program with exactly same user input, that is 2021: The dry run of following block of code (from above program): mm = 1 while mm<=12: print(calendar.month(yy, mm)) mm = mm+1 goes like:
« Previous Program Next Program » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |