Academy
ProgrammingPython IntermediateModules & Imports

Modules & Imports

You Don't Have to Write Everything

Python ships with an enormous standard library — hundreds of modules covering mathematics, dates and times, file formats, networking, data compression, and much more. Before writing any utility function, it's worth asking: does Python already have this?

A module is simply a file of Python code. When you import it, everything defined in that file becomes available to you.

The import Statement

This loads the entire math module. You then access its contents using dot notation:

math.pi is a constant. math.sqrt(), math.floor(), math.ceil(), math.log() are functions. The module name acts as a namespace — it prevents name collisions between your code and the library.

Importing Specific Names

If you only need one or two things from a module, import them directly:

Now sqrt and pi are available directly — no math. prefix needed. Use this when you use a function frequently and the name is unambiguous.

The random Module

random is used in games, simulations, testing, and anywhere you need unpredictable values. shuffle() modifies the list in place — it doesn't return a new one.

The datetime Module

date.today() gives today's date. datetime.now() gives the current date and time. The :02d format specifier pads minutes with a leading zero — so 9 minutes displays as 09 rather than 9.

Calculating with dates:

timedelta represents a duration. Adding it to a date gives a new date. Subtracting two dates gives a timedelta — access .days for the integer count.

The collections Module

collections contains specialised container types that extend the built-in ones.

Counter counts occurrences of items:

defaultdict is a dictionary that never raises KeyError — it creates a default value automatically for missing keys:

Without defaultdict, you'd need to check whether the key exists before appending — a small but repetitive annoyance. defaultdict(list) handles it automatically.

The string Module

Useful for validation, password generation, and any task involving character sets.

Import Style — A Note

You may see this pattern in other code:

from math import *

This imports everything from a module into your current namespace. Avoid it. It pollutes your namespace with dozens of names and makes it impossible to tell where a function came from when reading the code later.

Prefer explicit imports:

from math import sqrt, pi          # Access as sqrt(), pi

The third form — importing with an alias — is standard practice for large libraries like NumPy, Pandas, and Matplotlib, which you'll encounter in the advanced tier.

What You Have Learned

Python's standard library is one of its greatest strengths — most common tasks have a ready-made solution.

The key ideas:

  • import module loads a module; access its contents with module.name
  • from module import name imports a specific name directly
  • math — mathematical functions and constants
  • random — random numbers, choices, and shuffling
  • datetime — dates, times, and durations
  • collectionsCounter for counting, defaultdict for safer dictionaries
  • Never use from module import * — always be explicit

In the next lesson, you enter the most significant conceptual territory of the intermediate tier: object-oriented programming — how to define your own data types with both data and behaviour bundled together.