Python · Advanced Python

Modules & Packages — Practice Questions

50 questions. Try each one yourself before checking the answer.

Q1importEasy

Import the math module and print the value of math.pi.

Q2from importEasy

Import just sqrt from math using from ... import ..., then use it directly (without the math. prefix).

Q3Built-in ModulesEasy

Import the random module and print a random integer between 1 and 10 using random.randint().

Q4AliasesEasy

Import math with the alias m, then use m.sqrt() to compute a square root.

Q5from importEasy

Import just choice from random, then use it to pick a random item from ["red", "green", "blue"].

Q6importEasy

Import both math and random in a single line, then use one function from each.

Q7Built-in ModulesEasy

Import datetime and print today's date using datetime.date.today().

Q8Built-in ModulesEasy

Import os and print the current working directory using os.getcwd().

Q9Built-in ModulesEasy

Import Path from pathlib, create a Path object for "notes.txt", and print its .name and .suffix.

Q10`__name__`Easy

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.

Q11randomMedium

Simulate rolling a six-sided die 5 times using random.randint(1, 6) inside a loop.

Q12randomMedium

Given deck = ["A", "K", "Q", "J", "10"], shuffle it in place using random.shuffle() and print the result.

Q13randomMedium

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().

Q14mathMedium

Print math.floor(4.7), math.ceil(4.2), and math.trunc(4.9), and explain in a comment how each rounds differently.

Q15mathMedium

Print math.factorial(5) and math.gcd(48, 18).

Q16mathMedium

Compare math.pow(2, 10) to the ** operator's 2 ** 10, printing both and their type().

Q17import *Medium

Demonstrate from math import *, then use sqrt() and pi directly without a prefix. Add a comment about why this style is generally discouraged.

Q18randomMedium

Given names = ["Alice", "Bob", "Carol", "Dave"], use random.sample() to pick 2 unique names without repeats.

Q19datetimeMedium

Print the current year, month, and day separately by accessing attributes on datetime.date.today().

Q20pathlibMedium

Using pathlib.Path, join a folder name and a file name into a full path (using /), and print the result.

Q21Real-WorldMedium

Build a "Dice Roller". Ask the user how many dice to roll, then print each roll's result using random.randint().

Q22Real-WorldMedium

Build an "OTP Generator" that creates a random 6-digit numeric code using random.randint().

Q23Real-WorldMedium

Build a "Circle Calculator" using math.pi to compute the area and circumference from a radius entered by the user.

Q24Real-WorldMedium

Build a "Password Generator" using random.choice() combined with string.ascii_letters and string.digits to create an 8-character password.

Q25Real-WorldMedium

Build a "Magnitude Estimator" using math.log10() to print how many digits a given positive integer has.

Q26Real-WorldMedium

Build a "Coin Flip Simulator" that flips a coin 10 times using random.choice(["Heads", "Tails"]) and counts how many of each outcome occurred.

Q27Real-WorldMedium

Build a "File Extension Checker" using pathlib.Path to print whether a given filename ends in .py.

Q28Real-WorldMedium

Build a "Directory Lister" using os.listdir() to print all file/folder names in the current working directory.

Q29DebuggingMedium

The following code crashes with an AttributeError. Find and fix the bug.

import math
print(math.squareroot(16))
Q30FormattingMedium

Print today's date (datetime.date.today()) formatted as DD-MM-YYYY using .strftime().

Q31Creating ModulesHard

Create a custom module mymath.py containing a function square(n), then import and use it from a separate script.

Q32Creating ModulesHard

Import your custom mymath module (from Q31) using an alias, mm, and call square() through it.

Q33Creating ModulesHard

Add a if __name__ == "__main__": guard to mymath.py so it prints a test message ONLY when run directly, not when imported.

Q34PackagesHard

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.

Q35randomHard

Use random.seed(42) before generating random numbers, and demonstrate that seeding makes the "random" sequence reproducible across runs.

Q36mathHard

Demonstrate math.isclose() for comparing floating-point results safely, revisiting the 0.1 + 0.2 == 0.3 gotcha from the Operators module.

Q37mathHard

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.

Q38AliasesHard

Import datetime.date directly with an alias Date, and use it to create a specific date object, then print it.

Q39Circular ImportsHard

Explain (with commented pseudo-file-layout) what a circular import is, and how splitting shared logic into a third module avoids it.

Q40DebuggingHard

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'
Q41Mini-ProjectMini-Project

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).

Q42Mini-ProjectMini-Project

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.

Q43Mini-ProjectMini-Project

Build a reusable "Unit Converter" module with functions for length and weight conversions, then use it from a main script.

Q44Mini-ProjectMini-Project

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.

Q45Mini-ProjectMini-Project

Build a "File Organizer Preview" that uses pathlib to list all files in the current directory along with their extensions, without actually moving anything.

Q46InterviewInterview

Explain the difference between a "module" and a "package" in Python, demonstrating both with a short folder-layout comment.

Q47InterviewInterview

Explain why random should never be used for security-sensitive values (like password reset tokens), and demonstrate the secrets module as the correct alternative.

Q48InterviewInterview

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.

Q49InterviewInterview

Explain why from module import * can silently break code, demonstrating a concrete name collision between a custom variable and something pulled in from math.

Q50CapstoneInterview

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