Python Program for calendar pryear() Method with Examples

Calendar Module:

The calendar module allows you to output calendars like a program and includes extra calendar-related operations. Calendar module functions and classes make use of an idealized calendar, the current Gregorian calendar extended in both directions indefinitely.

pryear() Method:

The pryear() method prints the calendar for the complete year as returned by formatyear().

To generate plain text calendars, class calendar.TextCalendar(firstweekday=0) can be used.The pryear() method is one of the TextCalendar instance’s methods.

Syntax:

pryear(year, width=2, lines=1, c=6, m=3)

Parameter Values:

year: This is required. It is a number. It is the year for which the calendar should be created.

width: This is Optional. It is a number. The distance between two columns. 2 is the default value.

lines: This is Optional. It is a number. A blank line between two rows. 1 is the default value.

c: This is Optional. It is a number. It is the space between two months (column-wise). 6 is the default value.

m: This is Optional. It is a number. It is the number of months in a row. 3 is the default value.

Return Value: A m-column calendar for the whole year is returned.

Program for calendar pryear() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Example1:

Approach:

  • Import calendar module using the import keyword.
  • Give the year as static input and store it in a variable.
  • Give the width as static input and store it in another variable.
  • Call the TextCalendar() function by setting firstweekday=0 and store it in another variable.
  • Apply pryear() method to the above text calendar by passing the given year, width as the arguments and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as static input and store it in a variable.
gvn_yr = 2021
# Give the width as static input and store it in another variable.
gvn_widt = 4
# Call the TextCalendar() function by setting firstweekday=0 and store it in
# another variable.
txt_calndr = calendar.TextCalendar(firstweekday=0)
# Apply pryear() method to the above text calendar by passing the given year,
# width,as the arguments and store it in another variable.
rslt = txt_calndr.pryear(gvn_yr, gvn_widt)
# Print the above result.
print(rslt)

Output:

                                                       2021

             January                                 February                                 March
Mon  Tue  Wed  Thu  Fri  Sat  Sun       Mon  Tue  Wed  Thu  Fri  Sat  Sun       Mon  Tue  Wed  Thu  Fri  Sat  Sun
                      1    2    3         1    2    3    4    5    6    7         1    2    3    4    5    6    7
  4    5    6    7    8    9   10         8    9   10   11   12   13   14         8    9   10   11   12   13   14
 11   12   13   14   15   16   17        15   16   17   18   19   20   21        15   16   17   18   19   20   21
 18   19   20   21   22   23   24        22   23   24   25   26   27   28        22   23   24   25   26   27   28
 25   26   27   28   29   30   31                                                29   30   31

              April                                    May                                     June
Mon  Tue  Wed  Thu  Fri  Sat  Sun       Mon  Tue  Wed  Thu  Fri  Sat  Sun       Mon  Tue  Wed  Thu  Fri  Sat  Sun
                 1    2    3    4                                  1    2              1    2    3    4    5    6
  5    6    7    8    9   10   11         3    4    5    6    7    8    9         7    8    9   10   11   12   13
 12   13   14   15   16   17   18        10   11   12   13   14   15   16        14   15   16   17   18   19   20
 19   20   21   22   23   24   25        17   18   19   20   21   22   23        21   22   23   24   25   26   27
 26   27   28   29   30                  24   25   26   27   28   29   30        28   29   30
                                         31

               July                                   August                                September
Mon  Tue  Wed  Thu  Fri  Sat  Sun       Mon  Tue  Wed  Thu  Fri  Sat  Sun       Mon  Tue  Wed  Thu  Fri  Sat  Sun
                 1    2    3    4                                       1                   1    2    3    4    5
  5    6    7    8    9   10   11         2    3    4    5    6    7    8         6    7    8    9   10   11   12
 12   13   14   15   16   17   18         9   10   11   12   13   14   15        13   14   15   16   17   18   19
 19   20   21   22   23   24   25        16   17   18   19   20   21   22        20   21   22   23   24   25   26
 26   27   28   29   30   31             23   24   25   26   27   28   29        27   28   29   30
                                         30   31

             October                                 November                                December
Mon  Tue  Wed  Thu  Fri  Sat  Sun       Mon  Tue  Wed  Thu  Fri  Sat  Sun       Mon  Tue  Wed  Thu  Fri  Sat  Sun
                      1    2    3         1    2    3    4    5    6    7                   1    2    3    4    5
  4    5    6    7    8    9   10         8    9   10   11   12   13   14         6    7    8    9   10   11   12
 11   12   13   14   15   16   17        15   16   17   18   19   20   21        13   14   15   16   17   18   19
 18   19   20   21   22   23   24        22   23   24   25   26   27   28        20   21   22   23   24   25   26
 25   26   27   28   29   30   31        29   30                                 27   28   29   30   31

None

Note:

It should be noted that the earliest year for which a calendar can be 
prepared varies depending on the platform.

Example2:

Approach:

  • Import calendar module using the import keyword.
  • Give the year as static input and store it in a variable.
  • Give the width as static input and store it in another variable.
  • Give the no of lines as static input and store it in another variable.
  • Give the c value as static input and store it in another variable.
  • Give the m value as static input and store it in another variable.
  • Call the TextCalendar() function by setting firstweekday=0 and store it in another variable.
  • Apply pryear() method to the above text calendar by passing the given year, month, width, no of lines, c, m, values as the arguments and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as static input and store it in a variable.
gvn_yr = 2020
# Give the width as static input and store it in another variable.
gvn_widt = 4
# Give the no of lines as static input and store it in another variable.
gvn_lines = 1
# Give the c value as static input and store it in another variable.
gvn_c_val = 3
# Give the m value as static input and store it in another variable.
gvn_m_val = 2
# Call the TextCalendar() function by setting firstweekday=0 and store it in
# another variable.
txt_calndr = calendar.TextCalendar(firstweekday=0)
# Apply pryear() method to the above text calendar by passing the given year,
# width, lines, c, m values as the arguments and store it in another variable.
rslt = txt_calndr.pryear(gvn_yr, gvn_widt, gvn_lines, gvn_c_val, gvn_m_val)
# Print the above result.
print(rslt)

Output:

                                  2020

             January                              February
Mon  Tue  Wed  Thu  Fri  Sat  Sun    Mon  Tue  Wed  Thu  Fri  Sat  Sun
            1    2    3    4    5                               1    2
  6    7    8    9   10   11   12      3    4    5    6    7    8    9
 13   14   15   16   17   18   19     10   11   12   13   14   15   16
 20   21   22   23   24   25   26     17   18   19   20   21   22   23
 27   28   29   30   31               24   25   26   27   28   29

              March                                April
Mon  Tue  Wed  Thu  Fri  Sat  Sun    Mon  Tue  Wed  Thu  Fri  Sat  Sun
                                1                1    2    3    4    5
  2    3    4    5    6    7    8      6    7    8    9   10   11   12
  9   10   11   12   13   14   15     13   14   15   16   17   18   19
 16   17   18   19   20   21   22     20   21   22   23   24   25   26
 23   24   25   26   27   28   29     27   28   29   30
 30   31

               May                                  June
Mon  Tue  Wed  Thu  Fri  Sat  Sun    Mon  Tue  Wed  Thu  Fri  Sat  Sun
                      1    2    3      1    2    3    4    5    6    7
  4    5    6    7    8    9   10      8    9   10   11   12   13   14
 11   12   13   14   15   16   17     15   16   17   18   19   20   21
 18   19   20   21   22   23   24     22   23   24   25   26   27   28
 25   26   27   28   29   30   31     29   30

               July                                August
Mon  Tue  Wed  Thu  Fri  Sat  Sun    Mon  Tue  Wed  Thu  Fri  Sat  Sun
            1    2    3    4    5                               1    2
  6    7    8    9   10   11   12      3    4    5    6    7    8    9
 13   14   15   16   17   18   19     10   11   12   13   14   15   16
 20   21   22   23   24   25   26     17   18   19   20   21   22   23
 27   28   29   30   31               24   25   26   27   28   29   30
                                      31

            September                             October
Mon  Tue  Wed  Thu  Fri  Sat  Sun    Mon  Tue  Wed  Thu  Fri  Sat  Sun
       1    2    3    4    5    6                     1    2    3    4
  7    8    9   10   11   12   13      5    6    7    8    9   10   11
 14   15   16   17   18   19   20     12   13   14   15   16   17   18
 21   22   23   24   25   26   27     19   20   21   22   23   24   25
 28   29   30                         26   27   28   29   30   31

             November                             December
Mon  Tue  Wed  Thu  Fri  Sat  Sun    Mon  Tue  Wed  Thu  Fri  Sat  Sun
                                1           1    2    3    4    5    6
  2    3    4    5    6    7    8      7    8    9   10   11   12   13
  9   10   11   12   13   14   15     14   15   16   17   18   19   20
 16   17   18   19   20   21   22     21   22   23   24   25   26   27
 23   24   25   26   27   28   29     28   29   30   31
 30

None

Method #2: Using Built-in Functions (User Input)

Example1:

Approach:

  • Import calendar module using the import keyword.
  • Give the year as user input using the int(input()) function and store it in a variable.
  • Give the width as user input using the int(input()) function and store it in another variable.
  • Call the TextCalendar() function by setting firstweekday=0 and store it in another variable.
  • Apply pryear() method to the above text calendar by passing the given year, width as the arguments and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as user input using the int(input()) function and store it in a variable.
gvn_yr =  int(input("Enter some random year = "))
# Give the width as user input using the int(input()) function and store it in another variable.
gvn_widt =  int(input("Enter some random number = "))
# Call the TextCalendar() function by setting firstweekday=0 and store it in
# another variable.
txt_calndr = calendar.TextCalendar(firstweekday=0)
# Apply pryear() method to the above text calendar by passing the given year,
# width,as the arguments and store it in another variable.
rslt = txt_calndr.pryear(gvn_yr, gvn_widt)
# Print the above result.
print(rslt)

Output:

Enter some random year = 2017 
Enter some random number = 3
                                             2017

          January                          February                          March
Mon Tue Wed Thu Fri Sat Sun      Mon Tue Wed Thu Fri Sat Sun      Mon Tue Wed Thu Fri Sat Sun
                          1                1   2   3   4   5                1   2   3   4   5
  2   3   4   5   6   7   8        6   7   8   9  10  11  12        6   7   8   9  10  11  12
  9  10  11  12  13  14  15       13  14  15  16  17  18  19       13  14  15  16  17  18  19
 16  17  18  19  20  21  22       20  21  22  23  24  25  26       20  21  22  23  24  25  26
 23  24  25  26  27  28  29       27  28                           27  28  29  30  31
 30  31

           April                             May                              June
Mon Tue Wed Thu Fri Sat Sun      Mon Tue Wed Thu Fri Sat Sun      Mon Tue Wed Thu Fri Sat Sun
                      1   2        1   2   3   4   5   6   7                    1   2   3   4
  3   4   5   6   7   8   9        8   9  10  11  12  13  14        5   6   7   8   9  10  11
 10  11  12  13  14  15  16       15  16  17  18  19  20  21       12  13  14  15  16  17  18
 17  18  19  20  21  22  23       22  23  24  25  26  27  28       19  20  21  22  23  24  25
 24  25  26  27  28  29  30       29  30  31                       26  27  28  29  30

            July                            August                         September
Mon Tue Wed Thu Fri Sat Sun      Mon Tue Wed Thu Fri Sat Sun      Mon Tue Wed Thu Fri Sat Sun
                      1   2            1   2   3   4   5   6                        1   2   3
  3   4   5   6   7   8   9        7   8   9  10  11  12  13        4   5   6   7   8   9  10
 10  11  12  13  14  15  16       14  15  16  17  18  19  20       11  12  13  14  15  16  17
 17  18  19  20  21  22  23       21  22  23  24  25  26  27       18  19  20  21  22  23  24
 24  25  26  27  28  29  30       28  29  30  31                   25  26  27  28  29  30
 31

          October                          November                         December
Mon Tue Wed Thu Fri Sat Sun      Mon Tue Wed Thu Fri Sat Sun      Mon Tue Wed Thu Fri Sat Sun
                          1                1   2   3   4   5                        1   2   3
  2   3   4   5   6   7   8        6   7   8   9  10  11  12        4   5   6   7   8   9  10
  9  10  11  12  13  14  15       13  14  15  16  17  18  19       11  12  13  14  15  16  17
 16  17  18  19  20  21  22       20  21  22  23  24  25  26       18  19  20  21  22  23  24
 23  24  25  26  27  28  29       27  28  29  30                   25  26  27  28  29  30  31
 30  31

None

Example2:

Approach:

  • Import calendar module using the import keyword.
  • Give the year as user input using the int(input()) function and store it in a variable.
  • Give the width as user input using the int(input()) function and store it in another variable.
  • Give the no of lines as user input using the int(input()) function and store it in another variable.
  • Give the c value as user input using the int(input()) function and store it in another variable.
  • Give the m value as user input using the int(input()) function and store it in another variable.
  • Call the TextCalendar() function by setting firstweekday=0 and store it in another variable.
  • Apply pryear() method to the above text calendar by passing the given year, month, width, no of lines, c, m, values as the arguments and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as user input using the int(input()) function and store it in a variable.
gvn_yr =  int(input("Enter some random year = "))
# Give the width as user input using the int(input()) function and store it in another variable.
gvn_widt =  int(input("Enter some random number = "))
# Give the no of lines as user input using the int(input()) function and store it in another variable.
gvn_lines = int(input("Enter some random number = "))
# Give the c value as user input using the int(input()) function and store it in another variable.
gvn_c_val = int(input("Enter some random number = "))
# Give the m value as user input using the int(input()) function and store it in another variable.
gvn_m_val = int(input("Enter some random number = "))
# Call the TextCalendar() function by setting firstweekday=0 and store it in
# another variable.
txt_calndr = calendar.TextCalendar(firstweekday=0)
# Apply pryear() method to the above text calendar by passing the given year,
# width, lines, c, m values as the arguments and store it in another variable.
rslt = txt_calndr.pryear(gvn_yr, gvn_widt, gvn_lines, gvn_c_val, gvn_m_val)
# Print the above result.
print(rslt)

Output:

Enter some random year = 2005
Enter some random number = 2 
Enter some random number = 1 
Enter some random number = 2 
Enter some random number = 2

                   2005

      January               February
Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
                1  2      1  2  3  4  5  6
 3  4  5  6  7  8  9   7  8  9 10 11 12 13
10 11 12 13 14 15 16  14 15 16 17 18 19 20
17 18 19 20 21 22 23  21 22 23 24 25 26 27
24 25 26 27 28 29 30  28
31

       March                 April
Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
    1  2  3  4  5  6               1  2  3
 7  8  9 10 11 12 13   4  5  6  7  8  9 10
14 15 16 17 18 19 20  11 12 13 14 15 16 17
21 22 23 24 25 26 27  18 19 20 21 22 23 24
28 29 30 31           25 26 27 28 29 30

        May                   June
Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
                   1         1  2  3  4  5
 2  3  4  5  6  7  8   6  7  8  9 10 11 12
 9 10 11 12 13 14 15  13 14 15 16 17 18 19
16 17 18 19 20 21 22  20 21 22 23 24 25 26
23 24 25 26 27 28 29  27 28 29 30
30 31

        July                 August
Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
             1  2  3   1  2  3  4  5  6  7
 4  5  6  7  8  9 10   8  9 10 11 12 13 14
11 12 13 14 15 16 17  15 16 17 18 19 20 21
18 19 20 21 22 23 24  22 23 24 25 26 27 28
25 26 27 28 29 30 31  29 30 31

     September              October
Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
          1  2  3  4                  1  2
 5  6  7  8  9 10 11   3  4  5  6  7  8  9
12 13 14 15 16 17 18  10 11 12 13 14 15 16
19 20 21 22 23 24 25  17 18 19 20 21 22 23
26 27 28 29 30        24 25 26 27 28 29 30
                      31

      November              December
Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
    1  2  3  4  5  6            1  2  3  4
 7  8  9 10 11 12 13   5  6  7  8  9 10 11
14 15 16 17 18 19 20  12 13 14 15 16 17 18
21 22 23 24 25 26 27  19 20 21 22 23 24 25
28 29 30              26 27 28 29 30 31

None