← Back to Blog

Modules: Code That's Already Written

The problem...

You need a random number. You need today's date. You need the square root of something.

You could write the code yourself. But someone already did — and did it better.

The idea!

A module is a file that contains Python code — functions, variables, classes — that you can import and use in your own script.

Python ships with hundreds of them. They're installed, tested, and ready. You just need to know they exist and how to bring them in.

You've already used modules

You've been using modules without thinking about it:

  • random — random numbers, random choices
  • math — square roots, powers, constants
  • csv — reading and writing CSV files
  • datetime — dates, times, timestamps

Every time you wrote import random or from datetime import datetime — that was a module.

How import works

Two ways to bring a module in:

import math
print(math.sqrt(16))

The whole module comes in. You access everything through the module name.

from math import sqrt
print(sqrt(16))

Just one thing comes in. You use it directly — no prefix needed.

Both work. The first is safer when you use many things from the module. The second is cleaner when you only need one or two.

The standard library

Python's built-in collection of modules is called the standard library. It covers:

  • Math and statistics
  • Dates and times
  • File and folder operations
  • Data structures
  • Networking, encryption, compression
  • And much more

You don't install any of it. It's already there.

Third-party modules

Beyond the standard library, there are hundreds of thousands of modules written by other developers — installable with pip.

You've already worked with some of them without realizing it: Django, the framework behind redhorndev.com, is a third-party package.

For this chapter, we stay in the standard library. Everything covered here is already on your machine.

What's really happening

When you write import random, Python finds the random.py file in its standard library, runs it, and makes everything inside it available to your script.

That's it. No magic. Just files.

What you've already seen

Before moving forward — a quick reminder of what you already know.

random — used in Control Flow for random choices and number generation:

import random

random.randint(1, 10)        # random integer between 1 and 10
random.choice(["a", "b"])    # random element from a list
random.shuffle(my_list)      # shuffle a list in place
random.random()              # float between 0.0 and 1.0

math — used in Basics and Functions for mathematical operations:

import math

math.sqrt(16)        # 4.0 — square root
math.floor(3.7)      # 3   — round down
math.ceil(3.2)       # 4   — round up
math.pi              # 3.141592... — constant
math.pow(2, 8)       # 256.0 — power

You know these. They're in your kit. This chapter adds to them.

Heads up!

  • import module — whole module, access with module.name
  • from module import name — one thing, use directly
  • Don't name your files after modules — random.py would shadow Python's own random
  • Standard library modules are already installed — no pip needed

The mindset shift

Stop thinking: "I need to write everything myself."

Start thinking: "Does Python already have this? It probably does."

What you should understand now

  • A module is a file of Python code you can import and use
  • Python's standard library has hundreds of ready-to-use modules
  • import module brings in the whole module
  • from module import name brings in one specific thing
  • You've already used modules — random, math, csv, datetime
  • Third-party modules exist — but that's a later conversation
[ login to bookmark ] // copied! 18 views · 2 min
← prev The Foundation Is Set next → Dates, Times, and Timestamps in Python
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.