Python Program To Display Calendar Using Loop Last Updated : 13 Feb, 2024 Comments Improve Suggest changes Like Article Like Report Python is an object-oriented, high-level, interpreted, and simple programming language. In Python, we are going to use the "Calendar" module to display a calendar. In this article, we are going to cover all the possible ways to display a calendar using loops i.e., while loop, for loop, etc., and various operations on the calendar, in tabular format such as days and months of the whole year. Display Calendar Using Loop in PythonBelow are some examples by which we can display the calendar using a loop in Python: Python Program To Display Calendar For LoopIn this example, a calendar matrix for February 2024 is generated using Python's `calendar` module. The matrix is printed in a tabular format with days of the week headers, where each day's number is displayed right-aligned within a two-character space using for loop. Python3 import calendar year = 2024 month = 2 #matrix format cal = calendar.monthcalendar(year, month) print("Mo Tu We Th Fr Sa Su") for week in cal: for day in week: if day == 0: print(" ", end=" ") else: print(f"{day:2} ", end=" ") print() OutputMo 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 Python Program To Display Calendar Using While LoopIn this example, a calendar matrix for February 2024 is created using Python's `calendar` module. The days of the week are displayed in a tabular format with the corresponding day numbers aligned within a two-character space. The variable `head` represents the header with week names, and the matrix is printed using nested loops to iterate through each week and day. Python3 import calendar year = 2024 month = 2 #using to represent matrix form cale = calendar.monthcalendar(year, month) #print the week names head = "Mo Tu We Th Fr Sa Su" print(head) for week in cale: day_index = 0 while day_index < len(week): day = week[day_index] if day == 0: print(" ", end=" ") else: print(f"{day:2d} ", end=" ") day_index += 1 print() OutputMo 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 Comment More infoAdvertise with us Next Article Python Program To Display Calendar Using Loop G geethabtrweh Follow Improve Article Tags : Python Python Programs Practice Tags : python Similar Reads Python Program to Display Calendar of a Given Month Displaying a calendar for a given month is a common task in programming, and Python provides several methods to achieve this. In this article, we will explore different approaches to creating a Python program that displays the calendar of a specified month. Whether using built-in libraries or third- 4 min read Python program to print calendar of given year Given a valid year as input, write a Python program to print the calendar of given year. In order to do this, we can simply use calendar module provided in Python. For more details about calendar module, refer this article. Python3 1== # Python program to print calendar for given year # importing ca 3 min read Python program to print current year, month and day In this article, the task is to write a Python Program to print the current year, month, and day. Approach: In Python, in order to print the current date consisting of a year, month, and day, it has a module named datetime. From the DateTime module, import date classCreate an object of the date clas 1 min read Python program to print Calendar without calendar or datetime module Given the month and year. The task is to show the calendar of that month and in the given year without using any module or pre-defined functions. Examples: Input : mm(1-12) :9 yy :2010 Output : September 2010 Su Mo Tu We Th Fr Sa 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 3 min read Python Program to check date in date range Given a date list and date range, the task is to write a Python program to check whether any date exists in the list in a given range. Example: Input : test_list = [datetime(2019, 12, 30), datetime(2018, 4, 4), datetime(2016, 12, 21), datetime(2021, 2, 2), datetime(2020, 2, 3), datetime(2017, 1, 1)] 10 min read Like