Modules & Packages: Practice Questions
100 questions. Try each one yourself before checking the answer.
Short on time? Filter by Must Do for the 25 questions that cover this topic on their own.
Import the math module and use it to print the square root of 144 and of 2.
Print math.pi, then use it to find the area and circumference of a circle of radius 5.
Use math.floor(), math.ceil() and round() on the same number and explain how the three differ.
Import just sqrt and pi from math so they can be used without the math. prefix.
Import math under a short alias and use it, then import a single function under a different name.
Show from math import * working, then explain in a comment why it is a bad habit.
Use random.randint() to print five dice rolls, and explain the range in a comment.
Show random.random() and random.uniform(), and say what each produces.
Use random.choice() to pick one item from a list, and random.choices() to pick several with repeats allowed.
Use random.shuffle() to reorder a list in place, and random.sample() to take several different items.
Use random.seed() to prove that the same seed always produces the same sequence.
Use dir() to list what a module contains, filtering out the private names.
Use help() on a single function, and print a function's docstring directly.
Print a module's __name__ and the first line of its __doc__.
Use the statistics module to find the mean, median and mode of a list of marks.
Use the string module's ready-made character sets instead of typing them out.
Use the sys module to print which Python version and platform you are running on.
Create your own module file and import a function from it.
Import specific names from your own module, and give one an alias.
Show that importing a module runs its top-level code, and that it runs only once however many times you import it.
Print __name__ in the running file and in an imported module, and explain the difference.
Write a module with a demo section guarded by if __name__ == "__main__", and show the demo does not run when the module is imported.
Use datetime to print today's date and the current year, formatted.
Use os to print the current working directory and join two path pieces correctly.
Use the time module to measure how long a piece of code takes.
Count the items in a list with collections.Counter, and show the loop it replaces.
Use Counter.most_common() to rank words by frequency, replacing the (-count, word) sorting trick.
Show that a Counter returns 0 for a missing key rather than raising KeyError, and use its arithmetic.
Use defaultdict(int) for counting and defaultdict(list) for grouping, replacing .get() and .setdefault().
Group records by a field with defaultdict(list) and print each group.
Replace a positional tuple record with a namedtuple, and show fields being read by name โ the fix promised in Topic 9.
Use a namedtuple's helper methods _fields, _asdict() and _replace().
Use copy.deepcopy() on a nested structure, and show copy.copy() is not enough โ the job Topic 8 and 11 had to do by hand.
Use math.gcd, math.factorial, math.log and math.hypot, comparing each with the version you wrote earlier.
Use the trigonometric functions with math.radians and math.degrees.
Use weighted random choices to simulate an unfair die, then count the results to prove the weighting worked.
Use statistics to report spread as well as average, and explain what the standard deviation tells you.
Use os.path to check whether files exist and how big they are โ the check Topic 12 could not make.
Show what sys.argv holds and how a script would read its command-line arguments.
Show sys.exit() raising SystemExit, and why that matters for cleanup.
Compare two ways of doing the same job using time.perf_counter().
Use the traceback module to record the full detail of an error โ the thing Topic 13 could only hint at.
Build a package โ a folder with __init__.py โ and import from it.
Use a package's __init__.py to re-export its contents, so users can import from the package directly.
Use __all__ to control what from module import * exposes.
Write a module with a docstring and documented functions, then read its help.
Write a configuration module holding constants, and import it from two places to show they share one copy.
Show importing inside a function rather than at the top of the file, and say when that is justified.
Compare a full word-frequency report written with Counter against the same report written by hand in Topic 11.
Show where Python looks for modules, and confirm that the current folder is searched first.
Build a password generator using random and string, guaranteeing at least one character of each required kind.
Simulate rolling two dice ten thousand times and report the distribution of totals.
Analyse a passage with Counter, reporting word counts, letter counts and words that appear only once.
Build a student report using namedtuples and the statistics module.
Build an index of words to the lines they appear on, using defaultdict(set).
Split a program into two modules โ one of helpers, one that uses them โ with a proper __main__ guard.
Build a quiz that shuffles both the questions and the options each run.
Build an undo feature using copy.deepcopy to snapshot state before each change.
Read numbers from a CSV and produce a full statistical summary.
Generate a file of realistic random test data using random and string.
Analyse the generated data with Counter and defaultdict together.
Build a three-module program where one module imports another.
Build an error logger that records full tracebacks to a file while keeping the program running.
Simulate a lottery draw and check tickets, using random.sample and set operations.
Report on a set of files using os.path, handling the ones that do not exist.
Simulate coin flips and show that the proportion settles towards 50% as the number of trials grows.
Build a package with three modules and use it as a small library.
Compare the speed of a list and a set for membership testing, proving the Topic 10 claim with real numbers.
Read a CSV straight into namedtuples and report on them.
Build a module that reads its own configuration and exposes a single tidy function.
Show how two star imports can silently replace each other's names, and how the same bug is impossible with plain imports.
Show what happens when your own file has the same name as a standard-library module.
Create a circular import, show how it fails, and fix it.
Show that module-level variables are shared by everyone who imports the module, and why that is a trap.
Show that reading a missing key from a defaultdict creates it, and when that causes a real bug.
Show three ways a Counter behaves differently from a plain dict.
Show what a namedtuple will and will not let you do, and the trap in its field names.
Show three levels of copying and exactly which one is safe for which structure.
Show why random must never be used for anything security-related, and name the module that should be.
Show why seeding is essential for a test and wrong for a game, using the same function both ways.
Show that editing a module file has no effect until the program restarts, and what sys.modules has to do with it.
Show what goes wrong in a module with no __main__ guard.
Show the difference between import module and from module import name when the module's contents change.
Show how sys.path decides which module wins, and add a folder to it at runtime.
Show the difference between absolute and relative imports inside a package, and when each fails.
Build a Password Toolkit as its own module: generate passwords to a policy, score their strength, and check them against a list of common ones.
Build a Text Analyser that uses Counter, defaultdict and statistics together to produce a full report on a file.
Build a Casino Simulation that plays many rounds of a dice game and reports the statistics of the outcome.
Build a utility package with several modules, a re-exporting __init__.py, and a demo guarded by __main__.
Build a Student Records System using namedtuples, statistics, Counter and JSON persistence.
Build a Random Quiz Generator that builds arithmetic questions, tracks results and reports statistics.
Build a Sales Data Analyser that reads a CSV into namedtuples and reports with Counter, defaultdict and statistics.
Build a configuration-driven application split across a config module, a logic module and a runner.
Build a Monte Carlo estimator for pi, showing accuracy improving with sample size.
Build a Log Analyser that uses Counter, defaultdict, statistics and traceback on a generated log file.
Explain exactly what Python does when it runs import something, and why each step matters.
Compare import module, from module import name and from module import *. Give a rule for choosing.
Explain if __name__ == "__main__" completely: what it is, what it does, and what breaks without it.
Give a tour of the standard library: which module solves which problem, and which of them replaced code you wrote by hand earlier in this course.
Capstone. Build a Module Toolkit Report. Create your own package, generate data with random, analyse it with collections and statistics, snapshot it with copy, persist it as JSON, time the work, and print one report proving every module played its part.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session