28/08/2023
Create a compact year calendar
Code :
from datetime import datetime as dt
# Mapping of the months
monthsMapping = ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May',
'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
cal_body = [[1,8,15,22,29,'Sun','Mon','Tue','Wed','Thu','Fri','Sat'],
[2,9,16,23,30,'Mon','Tue','Wed','Thu','Fri','Sat','Sun'],
[3,10,17,24,31,'Tue','Wed','Thu','Fri','Sat','Sun','Mon'],
[4,11,18,25,' ','Wed','Thu','Fri','Sat','Sun','Mon','Tue'],
[5,12,19,26,' ','Thu','Fri','Sat','Sun','Mon','Tue','Wed'],
[6,13,20,27,' ','Fri','Sat','Sun','Mon','Tue','Wed','Thu'],
[7,14,21,28,' ','Sat','Sun','Mon','Tue','Wed','Thu','Fri']]
# Initialize monthColumns with empty lists
monthColumns = [[] for _ in range(7)]
year = input('Year = ')
if not year:
year = dt.now().year
else:
year = int(year)
print()
# Populate monthColumns using list comprehension
for i in range(1, 13):
day1 = dt(year, i, 1)
monthColumns[(day1.weekday() - 6) % 7].append(day1.month)
# Print the calendar
for i in range(4):
for t in range(5):
if i == 0 and t == 2:
print(f"{year}\t", end="")
else:
print("*\t", end="")
for j in range(7):
monthColumns[j][i]:
try:
print(f"{monthsMapping[monthColumns[j][i]]}\t", end="")
:
except:
print(" |\t", end="")
print()
for _ in range(12):
print("===\t", end="")
print()
for i in range(7):
for j in range(12):
print(f"{cal_body[i][j]}\t", end="")
print()