Managing Specific Dates in a Year

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to print all the Saturdays in a given year using Python. This project is designed to help you understand how to work with dates and times in Python, and how to use the datetime module to perform date-related operations.

👀 Preview

$ python3 Saturday.py
2021-01-02
2021-01-09
2021-01-16
2021-01-23
2021-01-30
2021-02-06
2021-02-13
2021-02-20
2021-02-27
2021-03-06
2021-03-13
2021-03-20
2021-03-27
2021-04-03
2021-04-10
2021-04-17
2021-04-24
2021-05-01
2021-05-08
...
2021-10-16
2021-10-23
2021-10-30
2021-11-06
2021-11-13
2021-11-20
2021-11-27
2021-12-04
2021-12-11
2021-12-18
2021-12-25

🎯 Tasks

In this project, you will learn:

  • How to create a function to print all the Saturdays in a given year
  • How to use the datetime module to work with dates
  • How to iterate over months and days to find the Saturdays

🏆 Achievements

After completing this project, you will be able to:

  • Write a Python script to print all the Saturdays in a given year
  • Understand how to use the datetime module to perform date-related operations
  • Gain experience in working with dates and times in Python

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/break_continue("`Break and Continue`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/ErrorandExceptionHandlingGroup -.-> python/catching_exceptions("`Catching Exceptions`") python/PythonStandardLibraryGroup -.-> python/date_time("`Date and Time`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-302743{{"`Managing Specific Dates in a Year`"}} python/variables_data_types -.-> lab-302743{{"`Managing Specific Dates in a Year`"}} python/numeric_types -.-> lab-302743{{"`Managing Specific Dates in a Year`"}} python/conditional_statements -.-> lab-302743{{"`Managing Specific Dates in a Year`"}} python/for_loops -.-> lab-302743{{"`Managing Specific Dates in a Year`"}} python/break_continue -.-> lab-302743{{"`Managing Specific Dates in a Year`"}} python/tuples -.-> lab-302743{{"`Managing Specific Dates in a Year`"}} python/function_definition -.-> lab-302743{{"`Managing Specific Dates in a Year`"}} python/importing_modules -.-> lab-302743{{"`Managing Specific Dates in a Year`"}} python/standard_libraries -.-> lab-302743{{"`Managing Specific Dates in a Year`"}} python/catching_exceptions -.-> lab-302743{{"`Managing Specific Dates in a Year`"}} python/date_time -.-> lab-302743{{"`Managing Specific Dates in a Year`"}} python/build_in_functions -.-> lab-302743{{"`Managing Specific Dates in a Year`"}} end

In this step, you will create the print_saturdays function that will print all the Saturdays in a given year.

  1. Open the Saturday.py file in your text editor.
  2. Add the following code to define the print_saturdays function:
import datetime

def print_saturdays(year):
    """
    Print all Saturdays in a given year.

    Args:
        year (int): The year for which Saturdays need to be printed.

    Returns:
        None
    """
    ## Iterate over all the months in the given year
    for month in range(1, 13):
        ## Iterate over all the days in the month
        for day in range(1, 32):
            try:
                ## Create a datetime object for the current date
                date = datetime.datetime(year, month, day)

                ## Check if the current date is a Saturday
                if date.weekday() == 5:
                    print(date.strftime("%Y-%m-%d"))

            except ValueError:
                ## Skip if the day is invalid for the current month
                continue

This function takes a year as input and prints all the Saturdays in that year. It uses the datetime module to create a datetime object for each day in the year, and then checks if the weekday of the date is Saturday (represented by the number 5).

  1. Save the Saturday.py file.

In this step, you will call the print_saturdays function to print all the Saturdays in the year 2021.

  1. Add the following code at the end of the Saturday.py file:
## Call the print_saturdays function for the year 2021
print_saturdays(2021)

This will call the print_saturdays function with the year 2021 as the argument, and print all the Saturdays in that year.

  1. Save the Saturday.py file.

Run the Script

In this step, you will run the Saturday.py script and observe the output.

  1. Open a terminal or command prompt and navigate to the directory where the Saturday.py file is located.
  2. Run the script using the following command:
python3 Saturday.py

You should see the output similar to the following:

2021-01-02
2021-01-09
2021-01-16
2021-01-23
2021-01-30
2021-02-06
2021-02-13
2021-02-20
2021-02-27
2021-03-06
2021-03-13
2021-03-20
2021-03-27
2021-04-03
2021-04-10
2021-04-17
2021-04-24
2021-05-01
2021-05-08
...
2021-10-16
2021-10-23
2021-10-30
2021-11-06
2021-11-13
2021-11-20
2021-11-27
2021-12-04
2021-12-11
2021-12-18
2021-12-25

This output shows all the Saturdays in the year 2021, one date per line.

Congratulations! You have successfully completed the project.

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like