Python Programs on Modules

Program 1: Write a Python program to display the date and time using the Time module.

Logic:

  1. The time module (import time) provides functions for working with time, including retrieving and formatting the current time.
  2. The calendar module (import calender) provides functions to generate and format calendars. calendar.month(year, month) generates a formatted string of the calendar for the specified month and year.

Program:



import time

# (a) Display current date and time using time module
current_time = time.localtime()
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", current_time)
print(f"Current Date and Time: {formatted_time}")

Output:

Current Date and Time: 2025-04-28 14:00:05

Program 2: Write a Python program that prints the calendar of a particular month.



import calendar
year = int(input("Enter year (e.g., 2025): "))
month = int(input("Enter month (1-12): "))
if 1 <= month <= 12:
    specific_month = calendar.month(year, month)
    print(f"\nCalendar for {calendar.month_name[month]} {year}:\n")
    print(specific_month)
else:
    print("Month must be between 1 and 12.")

Output:

Program on Strings in Python