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.
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:
- Use the
datetime.datetime.weekday()method to get the day of the week as an integer. - Check if the day of the week is greater than
4. If it is, returnTrue, otherwise returnFalse.
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.