INTERACTIVE ONLINE COURSE

Python 3

FOR BEGINNERS


"The first Python course that simply amazed me. Very well explained and easy to understand." (Alexandru Cosmin)

"The best Python course in Romania." (Iulian Geană)


ALL REVIEWS
NEW APPLICATIONS
PAGE 1 / 1
Calendar module - Applications
Home >>> Online Lessons, Python 3
The calendar module is very useful when it comes to working with dates and calendars in programming. With it, you can perform various operations, such as displaying the current day, determining the day of the week for a specific date, generating events, and much more.

To start using the calendar module, you need to import it into your program. You can do this by adding the following line of code at the beginning of your program:

import calendar

Now that we have imported the module, let's see how we can use some of its functions.

Application 1. To display the calendar of an entire year, we can use the function calendar.calendar(year). This will generate a string representing the complete calendar for the specified year. For example, to display the calendar for the year 2023, we can use the following code:

import calendar
year = 2023
the_calendar = calendar.calendar(year)
print(the_calendar)


The result in the console is shown below:




Application 2. If we want to display just a single calendar for a specific month, we can use the function calendar.month(year, month). This will generate a string that represents the calendar for the specified month. For example, to display the calendar for July 2023, we can use the following code:

import calendar
year = 2024
month = 7
result = calendar.month(year, month)
print(result)






Application 3. If we want to find out the day of the week for a specific date, we can use the function calendar.weekday(year, month, day). This will return a number between 0 and 6, where 0 represents Monday, 1 represents Tuesday, and so on. For example, to find out the day of the week for July 23, 2023, we can use the following code:

import calendar
year = 2023
month = 7
day = 23
z = calendar.weekday(year, month, day)
print(z)





We can display the day of the week in text format, in English, with the help of a few lines of code:

import calendar
year = 2023
month = 7
day = 23
weekday = calendar.weekday(year, month, day)
if weekday == 0:
    weekday = "Monday"
elif weekday == 1:
    weekday = "Tuesday"
elif weekday == 2:
    weekday = "Wednesday"
elif weekday == 3:
    weekday = "Thursday"
elif weekday == 4:
    weekday = "Friday"
elif weekday == 5:
    weekday = "Saturday"
elif weekday == 6:
    weekday = "Sunday"
print(weekday)


Thus, the result looks more "human", as the program user may not know what the number 6 signifies in our developed code:




There are also other useful functions in this module, such as calendar.isleap(year) to check if a year is a leap year, or calendar.monthrange(year, month) to find out the number of days in a given month.

Proposed Exercises

1. Enter a year and a month from the keyboard, then display the calendar for that month.

2. Enter a year from the keyboard, then display whether that year is a leap year or not.

3. Ask the user to enter a start date and an end date, then display the number of working days (excluding weekends) in that interval. Hint: use the function calendar.weekday(year, month, day) and iterate through the days in the interval to count the working days.

4. Read a year and a list of birth dates (year, month, day) for a group of people. Display all the birthdays in that year and calculate how old the people will turn. Hint: you can use the function calendar.day_name to get the names of the days of the week and iterate through the birth dates to display and calculate the ages.

For the complete documentation of the module, be autodidactic - discover more functions [here].
This section is over.
 home   list  CONTENTS   perm_identity   arrow_upward