diff --git a/README.md b/README.md index 56a42ef3..d5b975ce 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ In addition to these projects, I regularly share my insights and learnings on th ## :snake: Python - [Argument Parsing](python/argparse.ipynb): Master argument parsing using the `argparse` module. +- [Calendars](python/calendar.ipynb): Explore the `calendar` module in Python. - [Generators](python/generator.ipynb): A hands-on guide to understanding and using generators. - [Lambda](python/lambda.ipynb): Get introduced to lambda functions. - [Pattern Matching](python/match-case.ipynb): Learn pattern matching with the `match-case` statement. diff --git a/python/calendar.ipynb b/python/calendar.ipynb new file mode 100644 index 00000000..31af0d24 --- /dev/null +++ b/python/calendar.ipynb @@ -0,0 +1,379 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Click [here]() to access the associated Medium article." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Setup" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "import calendar\n", + "import locale" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Basic Calendar Operations\n", + "\n", + "## Displaying a Calendar for a Specific Month and Year" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " May 2024\n", + "Mo Tu We Th Fr Sa Su\n", + " 1 2 3 4 5\n", + " 6 7 8 9 10 11 12\n", + "13 14 15 16 17 18 19\n", + "20 21 22 23 24 25 26\n", + "27 28 29 30 31\n", + "\n" + ] + } + ], + "source": [ + "# Specify the year and month\n", + "year = 2024\n", + "month = 5\n", + "\n", + "# Generate the calendar\n", + "cal = calendar.month(year, month)\n", + "print(cal)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Determining the Day of the Week for a Given Date" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "May 15, 2024 is a Wednesday.\n" + ] + } + ], + "source": [ + "# Get the weekday for May 15, 2024\n", + "weekday_index = calendar.weekday(year, month, 15)\n", + "\n", + "# Convert the index to the actual day name\n", + "day_name = calendar.day_name[weekday_index]\n", + "print(f\"May 15, 2024 is a {day_name}.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Finding Leap Years" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2024 is a leap year.\n" + ] + } + ], + "source": [ + "# Check if 2024 is a leap year\n", + "is_leap = calendar.isleap(year)\n", + "if is_leap:\n", + " print(f\"{year} is a leap year.\")\n", + "else:\n", + " print(f\"{year} is not a leap year.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Working with Weekdays\n", + "\n", + "## Mapping Weekday Numbers to Day Names" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Monday is called Monday.\n" + ] + } + ], + "source": [ + "# Get the day name for Monday (weekday index 0)\n", + "monday_name = calendar.day_name[0]\n", + "print(f\"Monday is called {monday_name}.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Identifying Weekends" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Weekend\n" + ] + } + ], + "source": [ + "# Create a date object for May 5, 2024\n", + "date_for_checking = calendar.datetime.date(2024, 5, 5)\n", + "\n", + "# Check if the day is a weekend (Saturday or Sunday)\n", + "if date_for_checking.weekday() in [5, 6]:\n", + " print(\"Weekend\")\n", + "else:\n", + " print(\"Weekday\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Advanced Features\n", + "\n", + "## Handling Different Locales and Languages" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " décembre 2021\n", + "Lu Ma Me Je Ve Sa Di\n", + " 1 2 3 4 5\n", + " 6 7 8 9 10 11 12\n", + "13 14 15 16 17 18 19\n", + "20 21 22 23 24 25 26\n", + "27 28 29 30 31\n", + "\n" + ] + } + ], + "source": [ + "# Set the locale to French\n", + "locale.setlocale(locale.LC_TIME, 'fr_FR')\n", + "\n", + "# Generate a calendar for December 2021\n", + "cal = calendar.month(2021, 12)\n", + "print(cal)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Customizing the Calendar Display\n", + "\n", + "### First Day of the Week" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [], + "source": [ + "calendar.setfirstweekday(calendar.SUNDAY)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Practical Examples\n", + "\n", + "## Building a Simple Event Scheduler" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " May 2024\n", + "Mo Tu We Th Fr Sa Su\n", + " 1 2 3 4 5\n", + " 6 7 8 9 10 11 12\n", + "13 14 15 16 17 18 19\n", + "20 21 22 23 24 25 26\n", + "27 28 29 30 31\n", + "\n", + "Event on 5/5/2024\n", + "Event on 5/12/2024\n", + "Event on 5/20/2024\n" + ] + } + ], + "source": [ + "def display_events(year, month, event_dates):\n", + " cal = calendar.month(year, month)\n", + " print(cal)\n", + "\n", + " for day in event_dates:\n", + " print(f\"Event on {month}/{day}/{year}\")\n", + "\n", + "\n", + "# Example usage\n", + "event_dates = [5, 12, 20] # Dates with scheduled events\n", + "display_events(2024, 5, event_dates)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Analyzing Historical Data" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Friday the 13th occurred 172 times in the past century.\n" + ] + } + ], + "source": [ + "def count_friday_13(year_range):\n", + " count = 0\n", + " for year in range(*year_range):\n", + " for month in range(1, 13):\n", + " if calendar.weekday(year, month, 13) == calendar.FRIDAY:\n", + " count += 1\n", + " return count\n", + "\n", + "\n", + "# Example usage: Count Friday the 13ths from 1924 to 2024\n", + "friday_13_count = count_friday_13((1924, 2024))\n", + "print(f\"Friday the 13th occurred {friday_13_count} times in the past century.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Creating a Custom Holiday Tracker" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "December 25, 2024 is a holiday!\n" + ] + } + ], + "source": [ + "def is_holiday(date):\n", + " # Define your list of holiday dates (e.g., New Year's Day, Christmas, etc.)\n", + " holiday_dates = [(1, 1), (12, 25)] # Example: January 1st and December 25th\n", + "\n", + " month, day = date\n", + " return (month, day) in holiday_dates\n", + "\n", + "\n", + "# Example usage: Check if December 25, 2024 is a holiday\n", + "holiday_check = is_holiday((12, 25))\n", + "if holiday_check:\n", + " print(\"December 25, 2024 is a holiday!\")\n", + "else:\n", + " print(\"December 25, 2024 is not a holiday.\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/website/docs/python/calendar.md b/website/docs/python/calendar.md new file mode 100644 index 00000000..337fb0ff --- /dev/null +++ b/website/docs/python/calendar.md @@ -0,0 +1,308 @@ +--- +description: Learn how to use the `calendar` module in Python +tags: [calendar, python, scheduling, data science, coding tips] +--- + +# The `calendar` module + +The `calendar` module in Python is a versatile tool for handling dates, times, and calendars. Whether you're building a scheduling application, analyzing historical data, or simply curious about the days of the week, this module provides a rich set of functionalities. Let's dive into the details! + +## Introduction + +### What is the `calendar` Module? + +The `calendar` module allows you to work with various calendar-related operations. It provides functions to display calendars, determine weekdays, find leap years, and more. You can think of it as your trusty companion when dealing with date-related tasks. + +### Why Should You Use It? + +Here are a few reasons why the `calendar` module is worth exploring: + +- **Simplicity**: The module abstracts away the complexities of calendar calculations, making it easy for developers to perform common tasks without reinventing the wheel. + +- **Standardization**: Since it's part of Python's standard library, you can rely on its consistent behavior across different platforms and Python versions. + +- **Efficiency**: The `calendar` module efficiently handles date-related computations, saving you time and effort. + +### A Brief Overview of Its Capabilities + +Before we dive deeper, let's take a quick look at what the `calendar` module can do: + +- **Displaying Calendars**: You can generate calendars for specific months and years, complete with day names and week numbers. + +- **Weekday Calculations**: Need to know which day of the week a particular date falls on? The `calendar` module has you covered. + +- **Leap Years**: It provides functions to identify leap years (those extra days in February). + +In the upcoming sections, we'll explore these features in more detail. So buckle up, and let's explore the fascinating world of calendars in Python! + +## Basic Calendar Operations + +In this section, we'll explore the fundamental operations provided by the `calendar` module. Let's dive right in! + +### Displaying a Calendar for a Specific Month and Year + +The `calendar` module allows you to generate a calendar for a given month and year. You can display it as a grid, showing the days of the month along with their corresponding weekdays. Here's how you can do it: + +```python title="Python" +import calendar + +# Specify the year and month +year = 2024 +month = 5 + +# Generate the calendar +cal = calendar.month(year, month) +print(cal) +``` + +``` + May 2024 +Mo Tu We Th Fr Sa Su + 1 2 3 4 5 + 6 7 8 9 10 11 12 +13 14 15 16 17 18 19 +20 21 22 23 24 25 26 +27 28 29 30 31 +``` + +The output will be a neatly formatted calendar for May 2024, complete with day names and week numbers. + +### Determining the Day of the Week for a Given Date + +Sometimes you need to know which day of the week a specific date falls on. The `calendar` module provides a function called `weekday()` that returns the weekday index (0 for Monday, 6 for Sunday) for a given date. Here's an example: + +```python title="Python" +# Get the weekday for May 15, 2024 +weekday_index = calendar.weekday(year, month, 15) + +# Convert the index to the actual day name +day_name = calendar.day_name[weekday_index] +print(f"May 15, 2024 is a {day_name}.") +``` + +``` +May 15, 2024 is a Wednesday. +``` + +### Finding Leap Years + +Leap years have an extra day (February 29). To check if a year is a leap year, you can use the `isleap()` function: + +```python title="Python" +# Check if 2024 is a leap year +is_leap = calendar.isleap(year) +if is_leap: + print(f"{year} is a leap year.") +else: + print(f"{year} is not a leap year.") +``` + +``` +2024 is a leap year. +``` + +Stay tuned for more calendar magic in the upcoming sections! + +## Working with Weekdays + +In this section, we'll explore how to work with weekdays using the `calendar` module. Let's dive right in! + +### Mapping Weekday Numbers to Day Names + +The `calendar` module provides a convenient way to map weekday numbers (0 for Monday, 6 for Sunday) to their corresponding day names. Here's an example: + +```python title="Python" +import calendar + +# Get the day name for Monday (weekday index 0) +monday_name = calendar.day_name[0] +print(f"Monday is called {monday_name}.") +``` + +``` +Monday is called Monday. +``` + +You can use this mapping to display more user-friendly weekday names in your applications. + +### Identifying Weekends + +If you need to determine whether a specific date is a weekend (Saturday or Sunday), you can use the `weekday()` function: + +```python title="Python" +# Create a date object for May 5, 2024 +date_for_checking = calendar.datetime.date(2024, 5, 5) + +# Check if the day is a weekend (Saturday or Sunday) +if date_for_checking.weekday() in [5, 6]: + print("Weekend") +else: + print("Weekday") +``` + +``` +Weekend +``` + +Stay tuned for more weekday wisdom in the upcoming sections! + +## Advanced Features + +In this section, we'll explore some of the more advanced features offered by the `calendar` module. Let's dive in! + +### Handling Different Locales and Languages + +To work with different locales, you can use the `locale` module in conjunction with `calendar`. Once the locale is set, the `calendar` module will automatically use localized day and month names. Here's an example of setting the locale to French (fr_FR) and displaying a calendar for December 2021: + +```python title="Python" +import calendar +import locale + +# Set the locale to French +locale.setlocale(locale.LC_TIME, 'fr_FR') + +# Generate a calendar for December 2021 +cal = calendar.month(2021, 12) +print(cal) +``` + +``` + décembre 2021 +Lu Ma Me Je Ve Sa Di + 1 2 3 4 5 + 6 7 8 9 10 11 12 +13 14 15 16 17 18 19 +20 21 22 23 24 25 26 +27 28 29 30 31 +``` + +### Customizing the Calendar Display + +By default, the calendar starts with Monday (0) and ends with Sunday (6). To set Sunday as the first day of the week (starting with 0), use: + +```python title="Python" +calendar.setfirstweekday(calendar.SUNDAY) +``` + +Stay tuned for practical examples in the next section! + +## Practical Examples + +In this section, we'll dive into practical examples of how you can leverage the `calendar` module in your Python projects. Let's explore some real-world scenarios! + +### Building a Simple Event Scheduler + +Suppose you're creating an event management application, and you want to display upcoming events for a specific month. You can use the `calendar` module to generate a calendar and mark event dates. Here's a snippet to get you started: + +```python title="Python" +def display_events(year, month, event_dates): + cal = calendar.month(year, month) + print(cal) + + for day in event_dates: + print(f"Event on {month}/{day}/{year}") + +# Example usage +event_dates = [5, 12, 20] # Dates with scheduled events +display_events(2024, 5, event_dates) +``` + +``` + May 2024 +Mo Tu We Th Fr Sa Su + 1 2 3 4 5 + 6 7 8 9 10 11 12 +13 14 15 16 17 18 19 +20 21 22 23 24 25 26 +27 28 29 30 31 + +Event on 5/5/2024 +Event on 5/12/2024 +Event on 5/20/2024 +``` + +### Analyzing Historical Data + +Let's say you're curious about how many Fridays fell on the 13th in the past century. You can iterate through the years and months, checking each 13th day. Here's a simplified version: + +```python title="Python" +def count_friday_13(year_range): + count = 0 + for year in range(*year_range): + for month in range(1, 13): + if calendar.weekday(year, month, 13) == calendar.FRIDAY: + count += 1 + return count + +# Example usage: Count Friday the 13ths from 1924 to 2024 +friday_13_count = count_friday_13((1924, 2024)) +print(f"Friday the 13th occurred {friday_13_count} times in the past century.") +``` + +``` +Friday the 13th occurred 172 times in the past century. +``` + +### Creating a Custom Holiday Tracker + +Suppose you're building an application that reminds users of upcoming holidays. You can define a list of holiday dates and check if a given date matches any of them. Here's a basic implementation: + +```python title="Python" +def is_holiday(date): + # Define your list of holiday dates (e.g., New Year's Day, Christmas, etc.) + holiday_dates = [(1, 1), (12, 25)] # Example: January 1st and December 25th + + month, day = date + return (month, day) in holiday_dates + +# Example usage: Check if December 25, 2024 is a holiday +holiday_check = is_holiday((12, 25)) +if holiday_check: + print("December 25, 2024 is a holiday!") +else: + print("December 25, 2024 is not a holiday.") +``` + +``` +December 25, 2024 is a holiday! +``` + +Remember to customize the `holiday_dates` list according to your region and specific holidays. + +## Tips and Best Practices + +In this final section, let's explore some tips and best practices for effectively using Python's `calendar` module. Whether you're a beginner or an experienced developer, these insights will help you make the most of this powerful tool. + +### Efficiently Navigating the `calendar` Module + +1. **Know Your Functions**: + + - Familiarize yourself with the key functions provided by the module: `month()`, `weekday()`, `isleap()`, and others. + - Read the official Python documentation to understand their parameters and return values. + +2. **Use Constants for Days of the Week**: + + - Instead of hardcoding weekday indices (e.g., 0 for Monday), use the constants provided by the module (e.g., `calendar.MONDAY`, `calendar.FRIDAY`, etc.). + +3. **Customize the First Day of the Week**: + - Set the first day of the week according to your region or application requirements using `calendar.setfirstweekday()`. + +### Avoiding Common Pitfalls + +1. **Leap Year Gotchas**: + + - Remember that leap years occur every 4 years, except for years divisible by 100 (unless they are also divisible by 400). + - Use `isleap()` to check for leap years rather than manual calculations. + +2. **Locale Considerations**: + + - When working with different locales, ensure that you set the appropriate locale using `locale.setlocale(locale.LC_TIME, 'your_locale_here')`. + - Be aware that localized day and month names may vary significantly. + +3. **Handling Edge Cases**: + + - Test your code with edge cases (e.g., dates near the beginning or end of the calendar) to ensure correctness. + +Remember that the `calendar` module is a versatile tool, and mastering it will enhance your Python skills. + +Happy coding, and may your calendars always be organized!