The program starts by importing the date and timedelta classes from the datetime module in the Python Standard Library. A variable named today is created using the date.today() method, which returns the current date.
The variable offset is calculated using the weekday() method, which returns an integer representing the day of the week (where Monday is 0 and Sunday is 6). The offset calculation uses the modulo operator % to find the number of days from today to the previous Tuesday (represented by 1 in the calculation), wrapping around to the previous week if necessary.
Finally, a variable named last_tue is created by subtracting the offset from today using a timedelta object. This gives us the date of the last Tuesday. The result is printed using the print function.
from datetime import date from datetime import timedelta today = date.today() offset = (today.weekday() - 1) % 7 last_tue = today - timedelta(days=offset) print(last_tue)
2022-09-20
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions