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__.pyfile. - 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).
- 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:import mathbrings the entire math toolbox into your script.from math import sqrtreaches into the math toolbox and pulls out only thesqrttool.
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.
2. The random Module
The random module provides tools to generate pseudo-random numbers and make random selections from collections.
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.
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: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