livepython.net
CHAPTER 18Python Modules
18.5  PRACTICE

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 writeIt means
import mathload a module; then use math.sqrt(), math.pi
from math import sqrtbring in one name; then call sqrt() by itself
import math as mgive the module a nickname; then write m.sqrt()
greetings.pyyour own file is a module too; never name it math.py or random.py
math.sqrt(x), math.pisquare 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 - earliera 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

  1. 160.Python Program to Find the Square Root Using the math ModuleEasySolve
  2. 161.Python Program to Find the GCD and LCM of Two NumbersMediumSolve

The math module

  1. 162.Python Program to Find the Area and Circumference of a CircleEasySolve
  2. 163.Python Program to Find How Many Boxes You Need Using math.ceilEasySolve
  3. 164.Python Program to Find the Distance Between Two PointsMediumSolve

The random module

  1. 165.Python Program to Roll Two Dice Using random.seedEasySolve
  2. 166.Python Program to Make a Random Number Guessing GameMediumSolve

The datetime module

  1. 167.Python Program to Find the Number of Days Between Two DatesEasySolve
  2. 168.Python Program to Calculate Age from a Date of BirthMediumSolve
  3. 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.