Skip to main content

Using Built-in Modules

Python comes with a rich set of built-in modules called the Standard Library that are ready to use immediately without installing anything from the internet. This design philosophy is often referred to as “batteries included.”

Understanding the Terminology

Let’s clarify what these terms mean:
  • Module: A single Python file containing code (like math.py).
  • Package: A directory (folder) containing multiple modules and an __init__.py file.
  • Function: A reusable block of code that performs a specific action (like sqrt()).
  • Class: A template/blueprint for creating objects (we’ll cover this later).
Think of it like this:
  • A module is like a toolbox.
  • A package is like a garage containing multiple toolboxes.
  • A function is like a specific tool (a hammer or a screwdriver).
  • A class is like a blueprint for building custom tools.

Import Patterns Explained

To use a module, you must first import it. The two most common ways to import a module are:
What’s happening:
  • import math brings the entire math toolbox into your script.
  • from math import sqrt reaches into the math toolbox and pulls out only the sqrt tool.

Python’s Standard Library: Common Built-in Modules

Let’s look at four of the most commonly used standard library modules: math, random, datetime, and os.

1. The math Module

The math module provides access to mathematical functions for trigonometric, logarithmic, and rounding operations.
Remember that math.sqrt() always returns a floating-point number (e.g., 4.0), whereas operators like ** or functions like math.floor() return integers depending on their input.

2. The random Module

The random module provides tools to generate pseudo-random numbers and make random selections from collections.
Security Warning: The random module is not cryptographically secure. For security-sensitive applications (such as password generation or security tokens), use Python’s built-in secrets module instead.

3. The datetime Module

The datetime module offers classes for manipulating dates and times in both simple and complex ways.

Formatting Dates as Strings (strftime)

To convert a datetime object into a readable string format, use .strftime() (string format time):

Parsing Strings into Dates (strptime)

To convert a date string back into a datetime object, use .strptime() (string parse time):

4. The os Module

The os module provides a way of interacting with your operating system, allowing you to manage files, folders, and file paths.
Always use os.path.join() instead of manually concatenating paths with string operations (like "folder/" + "file.txt"). This guarantees that your code will run correctly on Windows, macOS, and Linux without modification.

5. string Module

The string module provides useful predefined string constants that are commonly used for text processing, validation, and random string generation.

Example

Generate a Random Password


Import Methods Recap

Here are the different ways you can import built-in modules:
Avoid from module import * as it can cause naming conflicts and makes code harder to understand.

Practice & Exercises

To reinforce what you’ve learned in this section (import patterns, math constants, random selections, date manipulations, and operating system directories), practice with these interactive notebooks:

Follow-Along Practice

Practice importing specific functions, utilizing ceil/floor and trigonometry functions, generating random bounds, executing date arithmetic, formatting/parsing datetimes, and building file system paths.💻 VS Code | 🚀 Colab | 📥 Download

Practice Exercises

Test your knowledge with hands-on exercises on calculating cosine of 60 degrees, picking random leaders and unique helpers, calculating days left in the year, and building directories with joined output logs.💻 VS Code | 🚀 Colab | 📥 Download

What’s next?

Now that you know how to use Python’s built-in modules, let’s learn how to read, write, and process text, JSON, and CSV data formats using only Python’s built-in tools.

Working with Data

Learn to process text, JSON, and CSV data files