Modules & Packages — Practice Questions
50 questions. Try each one yourself before checking the answer.
Import the math module and print the value of math.pi.
Import just sqrt from math using from ... import ..., then use it directly (without the math. prefix).
Import the random module and print a random integer between 1 and 10 using random.randint().
Import math with the alias m, then use m.sqrt() to compute a square root.
Import just choice from random, then use it to pick a random item from ["red", "green", "blue"].
Import both math and random in a single line, then use one function from each.
Import datetime and print today's date using datetime.date.today().
Import os and print the current working directory using os.getcwd().
Import Path from pathlib, create a Path object for "notes.txt", and print its .name and .suffix.
Print the value of __name__ when a script is run directly, and explain in a comment what it would be if this file were imported instead.
Simulate rolling a six-sided die 5 times using random.randint(1, 6) inside a loop.
Given deck = ["A", "K", "Q", "J", "10"], shuffle it in place using random.shuffle() and print the result.
Print a random floating-point number between 0.0 and 1.0 using random.random(), and a random float between 10 and 20 using random.uniform().
Print math.floor(4.7), math.ceil(4.2), and math.trunc(4.9), and explain in a comment how each rounds differently.
Print math.factorial(5) and math.gcd(48, 18).
Compare math.pow(2, 10) to the ** operator's 2 ** 10, printing both and their type().
Demonstrate from math import *, then use sqrt() and pi directly without a prefix. Add a comment about why this style is generally discouraged.
Given names = ["Alice", "Bob", "Carol", "Dave"], use random.sample() to pick 2 unique names without repeats.
Print the current year, month, and day separately by accessing attributes on datetime.date.today().
Using pathlib.Path, join a folder name and a file name into a full path (using /), and print the result.
Build a "Dice Roller". Ask the user how many dice to roll, then print each roll's result using random.randint().
Build an "OTP Generator" that creates a random 6-digit numeric code using random.randint().
Build a "Circle Calculator" using math.pi to compute the area and circumference from a radius entered by the user.
Build a "Password Generator" using random.choice() combined with string.ascii_letters and string.digits to create an 8-character password.
Build a "Magnitude Estimator" using math.log10() to print how many digits a given positive integer has.
Build a "Coin Flip Simulator" that flips a coin 10 times using random.choice(["Heads", "Tails"]) and counts how many of each outcome occurred.
Build a "File Extension Checker" using pathlib.Path to print whether a given filename ends in .py.
Build a "Directory Lister" using os.listdir() to print all file/folder names in the current working directory.
The following code crashes with an AttributeError. Find and fix the bug.
import math
print(math.squareroot(16))Print today's date (datetime.date.today()) formatted as DD-MM-YYYY using .strftime().
Create a custom module mymath.py containing a function square(n), then import and use it from a separate script.
Import your custom mymath module (from Q31) using an alias, mm, and call square() through it.
Add a if __name__ == "__main__": guard to mymath.py so it prints a test message ONLY when run directly, not when imported.
Describe (with a folder layout comment) and demonstrate importing from a simple PACKAGE: a folder shapes/ containing __init__.py and circle.py (with a function area(radius)), imported as shapes.circle.
Use random.seed(42) before generating random numbers, and demonstrate that seeding makes the "random" sequence reproducible across runs.
Demonstrate math.isclose() for comparing floating-point results safely, revisiting the 0.1 + 0.2 == 0.3 gotcha from the Operators module.
Print math.inf, -math.inf, and math.nan, and demonstrate the surprising fact that math.nan == math.nan is False, using math.isnan() for a proper check instead.
Import datetime.date directly with an alias Date, and use it to create a specific date object, then print it.
Explain (with commented pseudo-file-layout) what a circular import is, and how splitting shared logic into a third module avoids it.
The following code raises ModuleNotFoundError when run as python main.py from the project root, even though helpers.py exists in a utils/ subfolder. Explain the fix.
# --- File: main.py ---
import helpers # ModuleNotFoundError: No module named 'helpers'Build a "Dice Game". Two players each roll a die using random.randint(1, 6); print both rolls and announce the winner (or a tie).
Build a configurable "Password Generator Tool" that asks for a desired length and whether to include digits/symbols, then builds the password using random and string.
Build a reusable "Unit Converter" module with functions for length and weight conversions, then use it from a main script.
Build a "Quiz Question Picker" that stores a list of trivia questions and uses random.choice() to present a random one each time the program runs.
Build a "File Organizer Preview" that uses pathlib to list all files in the current directory along with their extensions, without actually moving anything.
Explain the difference between a "module" and a "package" in Python, demonstrating both with a short folder-layout comment.
Explain why random should never be used for security-sensitive values (like password reset tokens), and demonstrate the secrets module as the correct alternative.
Explain what happens the SECOND time a module is imported in the same program (Python caches it), demonstrating with a module-level print() that would only fire once even across multiple import statements.
Explain why from module import * can silently break code, demonstrating a concrete name collision between a custom variable and something pulled in from math.
Build a "Modules Mastery Report" — the most complete demonstration in this module. Create a custom module report_tools.py with a random_greeting() function (using random), then in a main script also use math for a calculation and datetime for today's date, printing everything together in a labeled report.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session