Python Modules Practice Questions for Beginners
Chapter 18 Recap and Practice
You can now use code that other people wrote, from Python's own standard library. Here is the chapter in one table:
| You write | It means |
|---|---|
import math | load a module; then use math.sqrt(), math.pi |
from math import sqrt | bring in one name; then call sqrt() by itself |
import math as m | give the module a nickname; then write m.sqrt() |
greetings.py | your own file is a module too; never name it math.py or random.py |
math.sqrt(x), math.pi | square root (a float), and the number pi |
math.ceil(x), math.floor(x) | always round up, always round down |
math.pow(2, 3), math.fabs(-7) | like ** and abs(), but always a float |
random.randint(1, 6) | a random whole number, both ends included |
random.choice(items) | one random item of a list |
random.shuffle(items) | mix the list itself; it gives back None |
random.random() | a random float from 0 up to (not including) 1 |
random.seed(7) | the same random numbers every run, for testing |
date(2025, 3, 14) | a date: year, month, day |
later - earlier | a timedelta; .days is the number of days |
day + timedelta(days=10) | a new date, 10 days later |
day.strftime("%d %B %Y") | a date as text, like 14 March 2025 |
date.fromisoformat("2025-03-14") | read a YYYY-MM-DD date from text |
And remember: random numbers and today's date change every run, so a program you want to test uses random.seed() and fixed dates.
Practice questions
Use input() with no prompt text: the tests type every line for you. When a question gives you a seed, call random.seed() with it before any random call, so your output matches the tests.
Importing modules
- 160.Python Program to Find the Square Root Using the math ModuleEasySolve
- 161.Python Program to Find the GCD and LCM of Two NumbersMediumSolve
The math module
- 162.Python Program to Find the Area and Circumference of a CircleEasySolve
- 163.Python Program to Find How Many Boxes You Need Using math.ceilEasySolve
- 164.Python Program to Find the Distance Between Two PointsMediumSolve
The random module
- 165.Python Program to Roll Two Dice Using random.seedEasySolve
- 166.Python Program to Make a Random Number Guessing GameMediumSolve
The datetime module
- 167.Python Program to Find the Number of Days Between Two DatesEasySolve
- 168.Python Program to Calculate Age from a Date of BirthMediumSolve
- 169.Python Program to Make a Library Due Date and Fine CalculatorHardSolve
Modules give you thousands of ready-made tools: import what you need. Next, you'll learn to read and write files.