Practice: Standard Library Essentials¶
View practice code on GitHub Get the Video Course
From the lesson
Practice problems for Section 11 Concepts. Build each yourself first, then expand the solution to check.
Four practice problems, one per concept step. The numbers line up. Build each yourself, then check python-practice/solutions. Some end with a short input() "Write a Program" exercise.
These problems continue one connected story: you are writing the store reporting tools for an online store. The math module does the number-crunching on totals, random picks the day's order count, the featured product and a fresh homepage order, datetime stamps order ship and arrival dates, and collections.Counter finds the best sellers.
Step-01: Import a Module and Use math¶
Concepts: import, math.sqrt, math.ceil, math.floor, math.pi, statistics.mean, input().
Problem-1: Store Report Numbers.
Write a program that, using the math module, prints these labeled lines:
- Task-1: the square root of 169 (use
math.sqrt). - Task-2: the rounded-up box count for 7.2 (use
math.ceil). - Task-3: the rounded-down full-pallet count for 5.8 (use
math.floor). - Task-4: the value of pi (use
math.pi).
square root of 169 is: 13.0
rounded-up boxes for 7.2 is: 8
rounded-down full pallets for 5.8 is: 5
pi is about: 3.141592653589793
Need a hint?
import math, then call math.sqrt(169), math.ceil(7.2), math.floor(5.8), and read the constant math.pi; each is its own labeled print("label:", value).
Show the solution
import math
# Problem-1 Task-1: print square root of 169
print("square root of 169 is:", math.sqrt(169))
# Problem-1 Task-2: print rounded-up boxes for 7.2
print("rounded-up boxes for 7.2 is:", math.ceil(7.2))
# Problem-1 Task-3: print rounded-down pallets for 5.8
print("rounded-down full pallets for 5.8 is:", math.floor(5.8))
# Problem-1 Task-4: print the value of pi
print("pi is about:", math.pi)
Problem-2: Average Order Total. Write a program that prints a labeled line with the mean (average) of the order totals 120, 80, and 250, using the statistics module (use statistics.mean).
Need a hint?
import statistics and pass the list [120, 80, 250] to statistics.mean(...); the mean of those three order totals is 150.
Show the solution
Problem-3 (Write a Program): Order Total Check. Write a program that asks the user for an order total and prints its square root.
Need a hint?
Wrap input(...) in int(...) first, because math.sqrt needs a number, not the text the user types.
Show the solution
Pages for this step: Concept | Practice | Workshop
Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions
Step-02: Random Values with random¶
Concepts: random.seed, randint, choice, shuffle.
Problem-1: Featured Product Picker.
Write a program that seeds random with 5 (use random.seed) so the output repeats, then prints these labeled lines:
- Task-1: the number of orders today, a whole number from 50 to 150 (use
random.randint). - Task-2: one featured product picked at random (use
random.choice) from the listLaptop,Headphones,Keyboard,Monitor. - Task-3: the list
Laptop,Headphones,Keyboard,Monitorshuffled in place for a fresh homepage order (userandom.shuffle).
Expected output (with seed 5):
orders today: 129
featured product: Keyboard
homepage order: ['Headphones', 'Laptop', 'Monitor', 'Keyboard']
Need a hint?
Call random.seed(5) once BEFORE the random calls so random.randint, random.choice, and random.shuffle replay the same sequence every run; random.shuffle changes the list in place and returns None.
Show the solution
import random
random.seed(5)
# Task-1: print random order count from 50 to 150
print("orders today:", random.randint(50, 150))
# Task-2: pick and print one featured product
featured_products = ["Laptop", "Headphones", "Keyboard", "Monitor"]
print("featured product:", random.choice(featured_products))
# Task-3: shuffle the list and print homepage order
homepage_lineup = ["Laptop", "Headphones", "Keyboard", "Monitor"]
random.shuffle(homepage_lineup)
print("homepage order:", homepage_lineup)
Pages for this step: Concept | Practice | Workshop
Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions
Step-03: Dates with datetime¶
Concepts: datetime(y, m, d), strftime, strptime, reading parts, timedelta.
Problem-1: Ship and Arrival Dates.
Write a program that builds a datetime ship_date for 5 March 2026 and an arrival_date 5 days after it (add a timedelta of 5 days), then prints these labeled lines:
- Task-1:
ship_dateformatted as%d %B %Y(usestrftime). - Task-2: the month of
ship_date(use.month). - Task-3:
arrival_dateformatted as%d %B %Y(usestrftime). - Task-4: the days in transit: subtract
ship_datefromarrival_dateand read.days.
Need a hint?
Build the date with datetime(2026, 3, 5), format with .strftime("%d %B %Y") (%B is the full month name), and add days with ship_date + timedelta(days=5). .month reads the month number straight off the date, and subtracting two datetimes gives a timedelta, so (arrival_date - ship_date).days is the last line.
Show the solution
from datetime import datetime, timedelta
ship_date = datetime(2026, 3, 5)
# Problem-1 Task-1: print ship_date formatted as day month year
print("ship date:", ship_date.strftime("%d %B %Y"))
# Problem-1 Task-2: print the month of ship_date
print("month:", ship_date.month)
arrival_date = ship_date + timedelta(days=5)
# Problem-1 Task-3: print arrival_date formatted as day month year
print("arrival date:", arrival_date.strftime("%d %B %Y"))
# Problem-1 Task-4: print the days in transit (arrival_date minus ship_date, read .days)
days_in_transit = arrival_date - ship_date
print("days in transit:", days_in_transit.days)
Problem-2: Sale Day From Text.
Write a program that parses the text "2026-11-27" (the store's sale day) into a date (use datetime.strptime with the format "%Y-%m-%d"), then prints these labeled lines:
- Task-1: the parsed date formatted as
%d %B %Y(usestrftime). - Task-2: the year of the parsed date (use
.year).
Warning
The format string in strptime must match the text exactly. "2026-11-27" uses dashes, so the format is "%Y-%m-%d". If the separators or order do not match the text, strptime raises a ValueError.
Need a hint?
datetime.strptime reads text into a date, so the format "%Y-%m-%d" must match the dashes and order of "2026-11-27" exactly or it raises ValueError.
Show the solution
Pages for this step: Concept | Practice | Workshop
Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions
Step-04: Count Items with collections.Counter¶
Concepts: Counter, most_common, input().
Problem (Write a Program): Best-Selling Letter.
Write a program that asks the user for a product name, then prints these labeled lines:
- Task-1: how many times each letter appears in the product name (use
Counter). - Task-2: the single most frequent letter (use
most_common(1)). - Task-3: the count of the letter
zin the name (index theCounterwith"z"). ACounterreturns0for a letter that is not there, instead of raising an error.
Enter a product name: sensors
letter counts: Counter({'s': 3, 'e': 1, 'n': 1, 'o': 1, 'r': 1})
most common: [('s', 3)]
count of z: 0
Need a hint?
Passing a product name to Counter counts each letter; most_common(1) returns a one-item list of (letter, count) tuples, not a single value. Indexing the Counter with a letter it never saw returns 0 instead of raising an error.
Show the solution
from collections import Counter
product_name = input("Enter a product name: ")
# Task-1: count and print how many times each letter appears
letter_tally = Counter(product_name)
print("letter counts:", letter_tally)
# Task-2: print the single most frequent letter
print("most common:", letter_tally.most_common(1))
# Task-3: a Counter returns 0 for a letter that never appeared, instead of an error
print("count of z:", letter_tally["z"])
Pages for this step: Concept | Practice | Workshop
Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions
Finished this section?
Check your work against the solutions above, then head back to the Section 11 lesson, or try the Section 11 workshop for more reps.
Prefer to learn by watching?
The full video course teaches every concept on screen and builds the projects with you, step by step.
Practice: Files JSON and Paths Next: Practice: Virtual Environments and Packaging