LearnFrenzy
Sign in Take a free test

Modules and Packages · Modules and Packages Coding Problems · Question 3

Days left until exams with datetime in Python Solution, output & explanation

  • Program
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

For a fixed "today" of 30 Nov 2026, print how many days and weeks remain until each exam, and the weekday of each exam.

Solution
main.py Run
from datetime import date

today = date(2026, 11, 30)
exams = {"Python": date(2026, 12, 18), "SQL": date(2027, 1, 7), "DSA": date(2027, 2, 1)}

for subject, day in sorted(exams.items(), key=lambda kv: kv[1]):
    left = (day - today).days
    weeks, days = divmod(left, 7)
    print(f"{subject:<7}{day:%d %b %Y} ({day:%A}): {left} days = {weeks} weeks {days} days")

Output Verified by running it on Python 3

Python 18 Dec 2026 (Friday): 18 days = 2 weeks 4 days
SQL    07 Jan 2027 (Thursday): 38 days = 5 weeks 3 days
DSA    01 Feb 2027 (Monday): 63 days = 9 weeks 0 days

Subtracting dates gives a timedelta; .days is an int. Format codes such as %d %b %Y and %A work directly in f-strings.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links