Add and subtract counters

Add and subtract counters

EasyCollections Programs · 4 of 20

Write a Python program that combines two inventories with Counter arithmetic.

Output
Counter({'pen': 6, 'book': 5})
Counter({'pen': 10, 'book': 8, 'bag': 2})

Approach

  1. + adds counts; - subtracts and drops counts below 1.

Solution

Python
from collections import Counter stock = Counter(pen=10, book=5, bag=2)sold = Counter(pen=4, bag=2)print(stock - sold)print(stock + Counter(book=3))
Output
Counter({'pen': 6, 'book': 5})
Counter({'pen': 10, 'book': 8, 'bag': 2})

Learn the concept

New to this topic? Read the lesson, then come back.

More collections programs

← All 20 Collections Programs

Most common letters

Most common letters

EasyCollections Programs · 2 of 20

Write a Python program to find the 3 most common letters in a word.

Output
[('i', 4), ('s', 4), ('p', 2)]

Approach

  1. Counter(word).most_common(3).

Solution

Python
from collections import Counterprint(Counter("mississippi").most_common(3))
Output
[('i', 4), ('s', 4), ('p', 2)]

Learn the concept

New to this topic? Read the lesson, then come back.

More collections programs

← All 20 Collections Programs

Count words with Counter

Count words with Counter

EasyCollections Programs · 1 of 20

Write a Python program to count the words in a sentence with Counter.

Output
[('to', 2), ('be', 2), ('or', 1)]

Approach

  1. Counter(text.split()).

Solution

Python
from collections import Counter text = "to be or not to be that is the question"counts = Counter(text.split())print(counts.most_common(3))
Output
[('to', 2), ('be', 2), ('or', 1)]

Learn the concept

New to this topic? Read the lesson, then come back.

More collections programs

← All 20 Collections Programs

Print a month calendar

Print a month calendar

EasyDatetime Programs · 20 of 20

Write a Python program to print the calendar of a month.

Output
   November 2026
Mo Tu We Th Fr Sa Su
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30

Approach

  1. calendar.month.

Solution

Python
import calendarprint(calendar.month(2026, 11))
Output
   November 2026
Mo Tu We Th Fr Sa Su
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30

Learn the concept

New to this topic? Read the lesson, then come back.

More datetime programs

← All 20 Datetime Programs

Python Datetime Programs with Solutions

Python Practice › Datetime Programs

Python Datetime Programs with Solutions

20 programs18 easy2 medium0 hard

20 datetime programs: creating dates, formatting and parsing, date arithmetic, ages, weekdays, month ends, business days and time zones. Programs use fixed dates so their output is repeatable.

Before you start

Read the Datetime lesson first if the topic is new to you.

All programs

Countdown to an event

Countdown to an event

EasyDatetime Programs · 19 of 20

Write a Python program that prints the days, hours and minutes until an event.

Output
59 days, 13 hours, 59 minutes

Approach

  1. Split a timedelta with divmod.

Solution

Python
from datetime import datetime now = datetime(2026, 11, 2, 10, 0)event = datetime(2026, 12, 31, 23, 59)left = event - nowhours, rest = divmod(left.seconds, 3600)print(f"{left.days} days, {hours} hours, {rest // 60} minutes")
Output
59 days, 13 hours, 59 minutes

Learn the concept

New to this topic? Read the lesson, then come back.

More datetime programs

← All 20 Datetime Programs

Day of the year and week number

Day of the year and week number

EasyDatetime Programs · 17 of 20

Write a Python program to get the day of the year and ISO week number of a date.

Output
306 45

Approach

  1. timetuple().tm_yday and isocalendar().

Solution

Python
from datetime import date d = date(2026, 11, 2)print(d.timetuple().tm_yday, d.isocalendar().week)
Output
306 45

Learn the concept

New to this topic? Read the lesson, then come back.

More datetime programs

← All 20 Datetime Programs

Next Monday

Next Monday

EasyDatetime Programs · 18 of 20

Write a Python program to find the date of the next Monday after a given date.

Output
2026-11-02 -> 2026-11-09
2026-11-05 -> 2026-11-09

Approach

  1. Days to add = (7 – weekday) % 7, or a full 7 days if the date is already a Monday.

Solution

Python
from datetime import date, timedelta def next_monday(d):    return d + timedelta(days=(7 - d.weekday()) % 7 or 7) for d in [date(2026, 11, 2), date(2026, 11, 5)]:    print(d, "->", next_monday(d))
Output
2026-11-02 -> 2026-11-09
2026-11-05 -> 2026-11-09

Line by line

Line 4

For a Monday the formula gives 0, so or 7 moves to the following Monday.

Learn the concept

New to this topic? Read the lesson, then come back.

More datetime programs

← All 20 Datetime Programs

Sort date strings

Sort date strings

EasyDatetime Programs · 16 of 20

Write a Python program to sort a list of date strings in date order.

Input
["15-08-2026", "26-01-2026", "02-10-2026"]
Output
['26-01-2026', '15-08-2026', '02-10-2026']

Approach

  1. Sort with a key that parses each string.

Solution

Python
from datetime import datetime dates = ["15-08-2026", "26-01-2026", "02-10-2026"]print(sorted(dates, key=lambda s: datetime.strptime(s, "%d-%m-%Y")))
Output
['26-01-2026', '15-08-2026', '02-10-2026']

Line by line

Line 4

Sorting the raw strings would compare day numbers first and give the wrong order.

Learn the concept

New to this topic? Read the lesson, then come back.

More datetime programs

← All 20 Datetime Programs

Timestamps

Timestamps

EasyDatetime Programs · 14 of 20

Write a Python program to convert between a datetime and a Unix timestamp.

Output
1767225600.0
2026-01-01 00:00:00+00:00

Approach

  1. Use UTC to get the same result on every computer.

Solution

Python
from datetime import datetime, timezone dt = datetime(2026, 1, 1, tzinfo=timezone.utc)ts = dt.timestamp()print(ts)print(datetime.fromtimestamp(ts, tz=timezone.utc))
Output
1767225600.0
2026-01-01 00:00:00+00:00

Learn the concept

New to this topic? Read the lesson, then come back.

More datetime programs

← All 20 Datetime Programs