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

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.

âœĻ Check Solution and Practice

Summary

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

Other Python Tutorials you may like