Skip to content

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).
Expected output
sqrt 196: 14.0
ceil 9.1: 10
floor 7.9: 7
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
s11_workshop01_sol_math_module.py
import math

# Task-1: print the square root of 196
print("sqrt 196:", math.sqrt(196))
# Task-2: print the ceiling of 9.1
print("ceil 9.1:", math.ceil(9.1))
# Task-3: print the floor of 7.9
print("floor 7.9:", math.floor(7.9))

View the full solution file on GitHub

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 list Mazda, Tesla, Honda, Kia.
  • Task-2: a lucky number from 1 to 100 (use random.randint).
Expected output
picked: Tesla
lucky number: 76
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
s11_workshop02_sol_random_module.py
import random

random.seed(3)
# Task-1: pick and print one random car
cars = ["Mazda", "Tesla", "Honda", "Kia"]
print("picked:", random.choice(cars))
# Task-2: print a lucky number from 1 to 100
print("lucky number:", random.randint(1, 100))

View the full solution file on GitHub

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: bought formatted as %d %B %Y (use strftime).
  • Task-2: the year of bought (use .year).
  • Task-3: service formatted as %d %B %Y (use strftime).
Expected output
bought: 15 March 2024
year: 2024
service due: 11 September 2024
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
s11_workshop03_sol_datetime_module.py
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"))

View the full solution file on GitHub

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)).
Expected output
Counter({'Mazda': 3, 'Tesla': 2, 'Honda': 1})
[('Mazda', 3)]
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
s11_workshop04_sol_collections_counter.py
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))

View the full solution file on GitHub

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.

Get the Video Course

Workshop: Files JSON and Paths Next: Workshop: Classes and OOP Basics

🎉 New Course
Python for Absolute Beginners
$15.99 $84.99 81% OFF
Get the Video Course