🐍 Python for Everyone

← Back to exercises

Chapter 11 Practice Bank 60-80 min, hands-on

Practice Bank: Modules & Packages

A deeper set of practice problems, organized by topic, for extra reps beyond the main exercises — including scenario-based problems written in the same style you'll meet in real interviews. Uses everything from Chapters 1-10 plus this chapter's own toolkit: import x, from x import y, import x as y, from x import y as z, and the math, datetime, and random modules. No try/except, file I/O, classes, or any import beyond math/datetime/random.

A note on randomness and dates Any problem using random or datetime.date.today() prints a different result every time you run it — that's the point. solution.py checks a property of the result (like "is this in range") instead of one exact value, so your output won't match anyone else's line-for-line, and that's fine.
How to run these Download starter.py below, then run it from a terminal with python3 starter.py.

Topic 1: What Is a Module?

1 Using a module's constant

Find # TODO 1.1. Write pi_times_two(), a function that returns math.pi * 2.

2 Describing a module in your own words

Find # TODO 1.2. Write describe_module(name, purpose), returning a formatted sentence about what a module is for.

3 Hand-typed constant vs. module constant

Find # TODO 1.3. Compare a hand-typed 3.14159 against math.pi to see the difference in precision.

4 A real formula using a module

Find # TODO 1.4. Write circle_circumference(radius) using math.pi.

5 Debug the Code

Find # TODO 1.5. This function uses math.pi but the file never imports the math module, so calling it raises a NameError. Fix it.

💼 Scenario

Why real projects use modules

Find # TODO 1.A. Write explain_module_reuse(), explaining what a module is and why math/random/datetime are modules.

💼 Interview Prep

Why not reinvent pi?

Find # TODO 1.B. Write why_not_reinvent_pi(), explaining in a comment-worthy string why math.pi beats a hand-typed constant.

Topic 2: Import Variations

1 Plain import math

Find # TODO 2.1. Using the plain import math, write sqrt_via_plain_import(n).

2 from math import sqrt

Find # TODO 2.2. Add from math import sqrt, then write sqrt_via_from_import(n) calling sqrt(n) directly.

3 import math as m

Find # TODO 2.3. Add import math as m, then write pi_via_aliased_import() returning m.pi.

4 from random import randint as ri

Find # TODO 2.4. Add from random import randint as ri, then write a dice-roll function using ri(1, 6).

5 Debug the Code

Find # TODO 2.5. This function calls the nonexistent math.round() instead of math.ceil(), raising an AttributeError. Fix it.

💼 Scenario

Picking the right import style

Find # TODO 2.A. Write compare_import_styles(), explaining when you'd reach for each import style.

💼 Interview Prep

import x vs. from x import y

Find # TODO 2.B. Write explain_import_variations(), explaining the difference between the import styles and what as actually renames.

Topic 3: The math Module

1 Hypotenuse with math.sqrt

Find # TODO 3.1. Write hypotenuse(a, b) using math.sqrt(a ** 2 + b ** 2).

2 Rounding down and up

Find # TODO 3.2. Write round_down_and_up(n) returning a tuple of math.floor(n) and math.ceil(n).

3 Exponents with math.pow

Find # TODO 3.3. Write power_of(base, exponent) using math.pow.

4 Circle area with math.pi

Find # TODO 3.4. Write circle_area(radius) using math.pi * radius ** 2.

5 Debug the Code

Find # TODO 3.5. This container-count function uses math.floor instead of math.ceil, silently losing any leftover items. Fix it.

💼 Scenario

Shipping box calculator

Find # TODO 3.A. Write boxes_required(num_items, items_per_box), rounding up with math.ceil for a warehouse packing tool.

💼 Interview Prep

floor vs. ceil vs. round

Find # TODO 3.B. Write explain_floor_vs_ceil_vs_round(), explaining the difference between all three.

Topic 4: The datetime Module

1 Creating a date

Find # TODO 4.1. Write make_date(year, month, day) using datetime.date.

2 Formatting a date

Find # TODO 4.2. Write format_date_long(a_date) using .strftime("%B %d, %Y").

3 Difference between two dates

Find # TODO 4.3. Write days_between(date_a, date_b) using date subtraction and .days.

4 Today's date

Find # TODO 4.4. Write days_since_today() using datetime.date.today().

5 Debug the Code

Find # TODO 4.5. This deadline countdown subtracts in the wrong order, so a future deadline comes back negative instead of positive. Fix it.

💼 Scenario

Subscription renewal countdown

Find # TODO 4.A. Write days_until_renewal(signup_date, renewal_days=365) using datetime.timedelta.

💼 Interview Prep

How date subtraction works

Find # TODO 4.B. Write explain_date_arithmetic(), explaining what a timedelta is and where it comes from.

Topic 5: The random Module

1 Rolling a die

Find # TODO 5.1. Write roll_die() using random.randint(1, 6).

2 Picking a random item

Find # TODO 5.2. Write pick_random_name(names) using random.choice.

3 Shuffling a copy

Find # TODO 5.3. Write shuffled_copy(items) using random.shuffle on a copy, leaving the original list untouched.

4 A random percentage

Find # TODO 5.4. Write random_percentage() using random.randint(0, 100).

5 Debug the Code

Find # TODO 5.5. This card picker calls random.choice() with no arguments at all, raising a TypeError. Fix it.

💼 Scenario

Randomized A/B test assignment

Find # TODO 5.A. Write assign_ab_group(), modeling how a feature-flag system randomly assigns users to a test group.

💼 Interview Prep

Testing random code

Find # TODO 5.B. Write explain_random_vs_deterministic(), explaining why random code needs property-based checks instead of exact-value assertions.

Topic 6: Bringing It Together — Modules in Production

1 Generating an order ID

Find # TODO 6.1. Write generate_order_id(order_number), combining datetime and string formatting.

2 Distance between two points

Find # TODO 6.2. Write distance_between_points(x1, y1, x2, y2) using math.sqrt.

3 A daily special picker

Find # TODO 6.3. Write pick_daily_special(menu_items) using random.choice.

4 Subscription status check

Find # TODO 6.4. Write subscription_status(start_date, length_days=30) using datetime.

5 Debug the Code

Find # TODO 6.5. This discount function adds the discount instead of subtracting it, making the "discounted" price higher than the original. Fix it.

💼 Scenario

Production order confirmation builder

Find # TODO 6.A. Write build_order_confirmation(order_number, price_per_item, quantity), combining math, datetime, and a dict return value.

💼 Interview Prep

Standard library in production

Find # TODO 6.B. Write explain_stdlib_value(), explaining how math/random/datetime show up in real production code.

Checking your work Compare your output against solution.py. Your exact wording doesn't need to match — the goal is that your program runs without errors and does what each TODO asks. Remember that any random or "today's date" output will legitimately differ each time you run it.
← Exercises Challenges →