Print Calendar without import module in python

How to print Calendar for a given year in Python without import its Module

Do you want to implement your own Calendar calculator?
Read up a bit about how Calendars work, their periodicity and the basis which you need to compute the day for any year.
Example:

actually I am new to python and
facing some problem to code the algorithm. can you please help

I can help. But i cannot spoonfeed you a solution, as that won’t really give you any learning. (if you want that, you might as well look into the library implementation of Calendar module)

So please share your attempts, your thought process, where you are stuck, what questions are you having etc. and would be happy to guide you on the next steps accordingly. (Text or video format both are okay)

f = open(“cal.txt” , “w”)
def month(m,y) :
yy = y
month = {1: ‘January’, 2: ‘February’, 3: ‘March’,
4: ‘April’, 5: ‘May’, 6: ‘June’, 7: ‘July’,
8: ‘August’, 9: ‘September’, 10: ‘October’,
11: ‘November’, 12: ‘December’}
mm = m

# code below for calculation of odd days
day = (yy - 1) % 400
day = (day // 100) * 5 + ((day % 100) - (day % 100) // 4) + ((day % 100) // 4) * 2
day = day % 7

nly = [31, 28, 31, 30, 31, 30,
       31, 31, 30, 31, 30, 31]
ly = [31, 29, 31, 30, 31, 30,
      31, 31, 30, 31, 30, 31]
s = 0

if yy % 4 == 0:
    for i in range(mm - 1):
        s += ly[i]
else:
    for i in range(mm - 1):
        s += nly[i]

day += s % 7
day = day % 7

# variable used for white space filling
# where date not present
space = ''
space = space.rjust(2, ' ')

# code below is to print the calendar
f.write(month[mm]+ "  "+str(yy))
f.write('\n')
f.write('Su'+ ' Mo'+ ' Tu'+ ' We'+ ' Th'+ ' Fr'+ ' Sa')
f.write('\n')

if mm == 9 or mm == 4 or mm == 6 or mm == 11:
    for i in range(31 + day):

        if i <= day:
            f.write(space +' ')
        else:
            f.write("{:02d}".format(i - day)+' ')
            if (i + 1) % 7 == 0:
                f.write("\n")
elif mm == 2:
    if yy % 4 == 0:
        p = 30
    else:
        p = 29

    for i in range(p + day):
        if i <= day:
            f.write(space+' ')
        else:
            f.write("{:02d}".format(i - day)+' ')
            if (i + 1) % 7 == 0:
                f.write('\n')
else:
    for i in range(32 + day):

        if i <= day:
            f.write(space+' ')
        else:
            f.write("{:02d}".format(i - day)+' ')
            if (i + 1) % 7 == 0:
                f.write('\n')
f.write('\n')
f.write('\n')

def calender(x) :

for i in range(12):
    month(i+1,x)

calender(2598)

Sir I make the code but I want the output as 4*3 calendar format and according to this code I get list of months in a single coloum, so please help to fix this problem

If the code is giving the right relationship between year,month,date and day and the only issue is of displaying in an incorrect format, it would be good to store all the data inside a data structure instead of a file on your computer.

Steps to follow:

  1. Understand requirements. You want a 4x3 format. that means you should 4 months together in a row, and also for a specific month, a row contains 7 days of the week (From Sunday to Saturday or Monday to Sunday as you like)
  2. Select a data structure based on requirements. In Python, a dictionary data structure is ideal for this. For example your dictionary can be called Calendar, where the keys of dictionary are months. Inside each of these Calendar[month] objects, you can store the information for that month.
  3. For example, you can have a 6x7 array for each month (6 rows are always enough to represent 31 days), which would resemble what you want to see in the output. Say you can initialize this array by “” (empty strings). You start inserting into this array from the 1st of each month, and go on from there sequentially. For example if your first column is Sunday and the 1st of April is on Tuesday, then you would insert 1 in 1st row 3rd column, 2 in 1st row 4th column, and so on.
  4. Once you have this data structure populated for all months, you can traverse 4 months at a time, and print their 1st row, then 2nd row, and so on, with appropriate spacing and indent as per your liking.

but I have to print the calendar in a file format its a task that has given to me

Yes. In step 4, you can write it in the file at the end instead of printing to output.

ok… thankyou so much