Check if a Date is a Weekday

PythonPythonBeginner
Practice Now

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

Introduction

In Python, you can use the datetime module to work with dates and times. One common task is to check if a given date is a weekday or a weekend. In this challenge, you will write a function that takes a date as input and returns True if it is a weekday, and False if it is a weekend.


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-13673{{"`Check if a Date is a Weekday`"}} python/booleans -.-> lab-13673{{"`Check if a Date is a Weekday`"}} python/tuples -.-> lab-13673{{"`Check if a Date is a Weekday`"}} python/function_definition -.-> lab-13673{{"`Check if a Date is a Weekday`"}} python/default_arguments -.-> lab-13673{{"`Check if a Date is a Weekday`"}} python/importing_modules -.-> lab-13673{{"`Check if a Date is a Weekday`"}} python/using_packages -.-> lab-13673{{"`Check if a Date is a Weekday`"}} python/standard_libraries -.-> lab-13673{{"`Check if a Date is a Weekday`"}} python/date_time -.-> lab-13673{{"`Check if a Date is a Weekday`"}} end

Check if a Date is a Weekday

Write a Python function called is_weekday() that takes a date as input and returns True if it is a weekday, and False if it is a weekend. If no date is provided, the function should use the current date.

To solve this problem, you can follow these steps:

  1. Import the datetime module.
  2. Define a function called is_weekday() that takes a date as input. If no date is provided, use the current date.
  3. Use the weekday() method of the datetime module to get the day of the week as an integer. The weekday() method returns an integer between 0 (Monday) and 6 (Sunday).
  4. Check if the day of the week is less than or equal to 4. If it is, return True, otherwise return False.
from datetime import datetime

def is_weekday(d = datetime.today()):
  return d.weekday() <= 4
from datetime import date

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

Summary

In this challenge, you learned how to use the datetime module to check if a given date is a weekday or a weekend. You also practiced defining functions and using conditional statements in Python.

Other Python Tutorials you may like