Determine If Date Is Weekend

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Python, the datetime module provides classes for working with dates and times. One common task is to check whether a given date is a weekend or not. In this challenge, you will write a function that takes a date as input and returns True if it is a weekend, and False otherwise.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/date_time("`Date and Time`") subgraph Lab Skills python/comments -.-> lab-13674{{"`Determine If Date Is Weekend`"}} python/booleans -.-> lab-13674{{"`Determine If Date Is Weekend`"}} python/tuples -.-> lab-13674{{"`Determine If Date Is Weekend`"}} python/function_definition -.-> lab-13674{{"`Determine If Date Is Weekend`"}} python/default_arguments -.-> lab-13674{{"`Determine If Date Is Weekend`"}} python/importing_modules -.-> lab-13674{{"`Determine If Date Is Weekend`"}} python/using_packages -.-> lab-13674{{"`Determine If Date Is Weekend`"}} python/standard_libraries -.-> lab-13674{{"`Determine If Date Is Weekend`"}} python/date_time -.-> lab-13674{{"`Determine If Date Is Weekend`"}} end

Date is Weekend

Write a function is_weekend(d) that takes a date object as input and returns True if the given date is a weekend, and False otherwise. If no argument is provided, the function should use the current date.

To solve this problem, you can follow these steps:

  1. Use the datetime.datetime.weekday() method to get the day of the week as an integer.
  2. Check if the day of the week is greater than 4. If it is, return True, otherwise return False.
from datetime import datetime

def is_weekend(d = datetime.today()):
  return d.weekday() > 4
from datetime import date

is_weekend(date(2020, 10, 25)) ## True
is_weekend(date(2020, 10, 28)) ## False

Summary

In this challenge, you have learned how to write a Python function that checks whether a given date is a weekend or not. You have used the datetime module to get the day of the week as an integer, and checked if it is greater than 4 to determine whether the date is a weekend or not.

Other Python Tutorials you may like