Workshop: Standard Library Essentials (Optional, Self-Study)¶
View workshop code on GitHub Get the Video Course
From the lesson
Workshop problems for Section 11 Concepts. Build each yourself first, then expand the solution to check.
Four optional workshop problems, one per concept step, in a car scenario. Self-study: check python-workshop/solutions.
Step-01: Import a Module and Use math¶
Concepts: math.sqrt, math.ceil, math.floor.
Write a program that, using the math module, prints these labeled lines:
- Task-1: the square root of 196 (use
math.sqrt). - Task-2: the ceiling of 9.1 (use
math.ceil). - Task-3: the floor of 7.9 (use
math.floor).
Need a hint?
math.sqrt returns a float (14.0), while math.ceil and math.floor round to whole int values up and down.
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, random.choice, random.randint.
Write a program that seeds random with 3 (use random.seed), then prints these labeled lines:
- Task-1: one car picked at random (use
random.choice) from the listMazda,Tesla,Honda,Kia. - Task-2: a lucky number from 1 to 100 (use
random.randint).
Need a hint?
Seed with random.seed(3) before random.choice(cars) and random.randint(1, 100) so the picked car and lucky number are reproducible.
Show the solution
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, timedelta.
Write a program that builds a datetime bought for 15 March 2024 and a service date 180 days after it (add a timedelta of 180 days), then prints these labeled lines:
- Task-1:
boughtformatted as%d %B %Y(usestrftime). - Task-2: the year of
bought(use.year). - Task-3:
serviceformatted as%d %B %Y(usestrftime).
Need a hint?
Both formatted lines use .strftime("%d %B %Y"), where %B is the full month name. Read .year directly off the datetime, and get the service date with bought + timedelta(days=180); timedelta handles the month rollover for you.
Show the solution
from datetime import datetime, timedelta
bought = datetime(2024, 3, 15)
# Task-1: print bought formatted as day month year
print("bought:", bought.strftime("%d %B %Y"))
# Task-2: print the year of bought
print("year:", bought.year)
service = bought + timedelta(days=180)
# Task-3: print service date formatted as day month year
print("service due:", service.strftime("%d %B %Y"))
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.
Write a program that, for the list of brands Mazda, Tesla, Mazda, Honda, Tesla, Mazda, prints these lines:
- Task-1: how many times each brand appears (use
Counter). - Task-2: the single most frequent brand (use
most_common(1)).
Need a hint?
Counter(brands) tallies repeated list items and prints sorted by frequency; most_common(1) returns a list with just the top (brand, count) pair.
Show the solution
from collections import Counter
brands = ["Mazda", "Tesla", "Mazda", "Honda", "Tesla", "Mazda"]
# Task-1: count and print how many times each brand appears
counts = Counter(brands)
print(counts)
# Task-2: print the single most frequent brand
print(counts.most_common(1))
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 practice 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.
Workshop: Files JSON and Paths Next: Workshop: Classes and OOP Basics