Python ยท Advanced Python

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.

Q1importEasyMust Do

Import the math module and use it to print the square root of 144 and of 2.

Q2mathEasy

Print math.pi, then use it to find the area and circumference of a circle of radius 5.

Q3mathEasy

Use math.floor(), math.ceil() and round() on the same number and explain how the three differ.

Q4from importEasyMust Do

Import just sqrt and pi from math so they can be used without the math. prefix.

Q5AliasesEasyMust Do

Import math under a short alias and use it, then import a single function under a different name.

Q6from import *Easy

Show from math import * working, then explain in a comment why it is a bad habit.

Q7randomEasyMust Do

Use random.randint() to print five dice rolls, and explain the range in a comment.

Q8randomEasy

Show random.random() and random.uniform(), and say what each produces.

Q9randomEasy

Use random.choice() to pick one item from a list, and random.choices() to pick several with repeats allowed.

Q10randomEasy

Use random.shuffle() to reorder a list in place, and random.sample() to take several different items.

Q11randomEasy

Use random.seed() to prove that the same seed always produces the same sequence.

Q12dir()Easy

Use dir() to list what a module contains, filtering out the private names.

Q13help()EasyMust Do

Use help() on a single function, and print a function's docstring directly.

Q14Module AttributesEasy

Print a module's __name__ and the first line of its __doc__.

Q15statisticsEasy

Use the statistics module to find the mean, median and mode of a list of marks.

Q16stringEasy

Use the string module's ready-made character sets instead of typing them out.

Q17sysEasy

Use the sys module to print which Python version and platform you are running on.

Q18Own ModulesEasyMust Do

Create your own module file and import a function from it.

Q19Own ModulesEasy

Import specific names from your own module, and give one an alias.

Q20Own ModulesEasy

Show that importing a module runs its top-level code, and that it runs only once however many times you import it.

Q21__name__EasyMust Do

Print __name__ in the running file and in an imported module, and explain the difference.

Q22__name__ == "__main__"EasyMust Do

Write a module with a demo section guarded by if __name__ == "__main__", and show the demo does not run when the module is imported.

Q23datetimeEasy

Use datetime to print today's date and the current year, formatted.

Q24osEasy

Use os to print the current working directory and join two path pieces correctly.

Q25timeEasy

Use the time module to measure how long a piece of code takes.

Q26collections.CounterMediumMust Do

Count the items in a list with collections.Counter, and show the loop it replaces.

Q27collections.CounterMediumMust Do

Use Counter.most_common() to rank words by frequency, replacing the (-count, word) sorting trick.

Q28collections.CounterMedium

Show that a Counter returns 0 for a missing key rather than raising KeyError, and use its arithmetic.

Q29collections.defaultdictMediumMust Do

Use defaultdict(int) for counting and defaultdict(list) for grouping, replacing .get() and .setdefault().

Q30collections.defaultdictMedium

Group records by a field with defaultdict(list) and print each group.

Q31collections.namedtupleMediumMust Do

Replace a positional tuple record with a namedtuple, and show fields being read by name โ€” the fix promised in Topic 9.

Q32collections.namedtupleMedium

Use a namedtuple's helper methods _fields, _asdict() and _replace().

Q33copyMediumMust Do

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.

Q34mathMedium

Use math.gcd, math.factorial, math.log and math.hypot, comparing each with the version you wrote earlier.

Q35mathMedium

Use the trigonometric functions with math.radians and math.degrees.

Q36randomMedium

Use weighted random choices to simulate an unfair die, then count the results to prove the weighting worked.

Q37statisticsMedium

Use statistics to report spread as well as average, and explain what the standard deviation tells you.

Q38osMedium

Use os.path to check whether files exist and how big they are โ€” the check Topic 12 could not make.

Q39sysMedium

Show what sys.argv holds and how a script would read its command-line arguments.

Q40sysMedium

Show sys.exit() raising SystemExit, and why that matters for cleanup.

Q41timeMedium

Compare two ways of doing the same job using time.perf_counter().

Q42tracebackMedium

Use the traceback module to record the full detail of an error โ€” the thing Topic 13 could only hint at.

Q43PackagesMediumMust Do

Build a package โ€” a folder with __init__.py โ€” and import from it.

Q44PackagesMedium

Use a package's __init__.py to re-export its contents, so users can import from the package directly.

Q45__all__Medium

Use __all__ to control what from module import * exposes.

Q46Own ModulesMedium

Write a module with a docstring and documented functions, then read its help.

Q47Own ModulesMedium

Write a configuration module holding constants, and import it from two places to show they share one copy.

Q48Deferred ImportsMedium

Show importing inside a function rather than at the top of the file, and say when that is justified.

Q49collections.CounterMedium

Compare a full word-frequency report written with Counter against the same report written by hand in Topic 11.

Q50Module Search PathMedium

Show where Python looks for modules, and confirm that the current folder is searched first.

Q51Real-WorldMediumMust Do

Build a password generator using random and string, guaranteeing at least one character of each required kind.

Q52Real-WorldMedium

Simulate rolling two dice ten thousand times and report the distribution of totals.

Q53collections.CounterMediumMust Do

Analyse a passage with Counter, reporting word counts, letter counts and words that appear only once.

Q54collections.namedtupleMedium

Build a student report using namedtuples and the statistics module.

Q55collections.defaultdictMedium

Build an index of words to the lines they appear on, using defaultdict(set).

Q56Own ModulesMediumMust Do

Split a program into two modules โ€” one of helpers, one that uses them โ€” with a proper __main__ guard.

Q57Real-WorldMedium

Build a quiz that shuffles both the questions and the options each run.

Q58copyMedium

Build an undo feature using copy.deepcopy to snapshot state before each change.

Q59statisticsMediumMust Do

Read numbers from a CSV and produce a full statistical summary.

Q60Real-WorldMedium

Generate a file of realistic random test data using random and string.

Q61collections.CounterMedium

Analyse the generated data with Counter and defaultdict together.

Q62Own ModulesMedium

Build a three-module program where one module imports another.

Q63tracebackMedium

Build an error logger that records full tracebacks to a file while keeping the program running.

Q64Real-WorldMedium

Simulate a lottery draw and check tickets, using random.sample and set operations.

Q65osMedium

Report on a set of files using os.path, handling the ones that do not exist.

Q66Real-WorldMedium

Simulate coin flips and show that the proportion settles towards 50% as the number of trials grows.

Q67PackagesMedium

Build a package with three modules and use it as a small library.

Q68Real-WorldMedium

Compare the speed of a list and a set for membership testing, proving the Topic 10 claim with real numbers.

Q69collections.namedtupleMediumMust Do

Read a CSV straight into namedtuples and report on them.

Q70Own ModulesMedium

Build a module that reads its own configuration and exposes a single tidy function.

Q71Anti-PatternsHardMust Do

Show how two star imports can silently replace each other's names, and how the same bug is impossible with plain imports.

Q72ShadowingHardMust Do

Show what happens when your own file has the same name as a standard-library module.

Q73Circular ImportsHard

Create a circular import, show how it fails, and fix it.

Q74Module StateHard

Show that module-level variables are shared by everyone who imports the module, and why that is a trap.

Q75collections.defaultdictHardMust Do

Show that reading a missing key from a defaultdict creates it, and when that causes a real bug.

Q76collections.CounterHard

Show three ways a Counter behaves differently from a plain dict.

Q77collections.namedtupleHard

Show what a namedtuple will and will not let you do, and the trap in its field names.

Q78copyHard

Show three levels of copying and exactly which one is safe for which structure.

Q79randomHard

Show why random must never be used for anything security-related, and name the module that should be.

Q80randomHard

Show why seeding is essential for a test and wrong for a game, using the same function both ways.

Q81Module CachingHard

Show that editing a module file has no effect until the program restarts, and what sys.modules has to do with it.

Q82__name__ == "__main__"Hard

Show what goes wrong in a module with no __main__ guard.

Q83ImportsHard

Show the difference between import module and from module import name when the module's contents change.

Q84Module Search PathHard

Show how sys.path decides which module wins, and add a folder to it at runtime.

Q85PackagesHard

Show the difference between absolute and relative imports inside a package, and when each fails.

Q86Mini-ProjectMini-Project

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.

Q87Mini-ProjectMini-Project

Build a Text Analyser that uses Counter, defaultdict and statistics together to produce a full report on a file.

Q88Mini-ProjectMini-Project

Build a Casino Simulation that plays many rounds of a dice game and reports the statistics of the outcome.

Q89Mini-ProjectMini-ProjectMust Do

Build a utility package with several modules, a re-exporting __init__.py, and a demo guarded by __main__.

Q90Mini-ProjectMini-ProjectMust Do

Build a Student Records System using namedtuples, statistics, Counter and JSON persistence.

Q91Mini-ProjectMini-Project

Build a Random Quiz Generator that builds arithmetic questions, tracks results and reports statistics.

Q92Mini-ProjectMini-Project

Build a Sales Data Analyser that reads a CSV into namedtuples and reports with Counter, defaultdict and statistics.

Q93Mini-ProjectMini-Project

Build a configuration-driven application split across a config module, a logic module and a runner.

Q94Mini-ProjectMini-Project

Build a Monte Carlo estimator for pi, showing accuracy improving with sample size.

Q95Mini-ProjectMini-Project

Build a Log Analyser that uses Counter, defaultdict, statistics and traceback on a generated log file.

Q96InterviewInterview

Explain exactly what Python does when it runs import something, and why each step matters.

Q97ImportsInterview

Compare import module, from module import name and from module import *. Give a rule for choosing.

Q98__name__ == "__main__"Interview

Explain if __name__ == "__main__" completely: what it is, what it does, and what breaks without it.

Q99Standard LibraryInterview

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.

Q100CapstoneInterviewMust Do

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