MGU Computer Science Series — Programming with Python
Part III: Functions and Program Design
Modules · Imports · Standard Library · Documentation
Chapter 15 of 30
15
MGU Computer Science Series  ·  Programming with Python  ·  Library Textbook Edition

Chapter 15:
Modules, Imports, and
Standard Library Basics

Reusing reliable tools — the import statement, namespaces, the math, random, and datetime modules, reading documentation, and writing your own modules

■ Part III of VII ■

■ 10 Sections  •  Chapter 15 of 30 ■

Builds on Chapters 11–14 — functions, parameters, return values, scope, and decomposition

Chapter Introduction and Learning Objectives

Every professional programmer uses tools that other programmers have already written and tested. Python ships with a large collection of these tools built in — the standard library — organized into modules that you can load into any program when you need them. Learning to find, import, and use modules correctly is one of the most practical skills in Python programming.

This chapter explains what modules are, how the import statement works, and how to use several genuinely useful standard library modules: math for mathematical operations, random for randomness, and datetime for dates and times. It also teaches you how to begin reading Python documentation so you can keep learning on your own, and how to write your own simple module.

Opening Question

Suppose you need to calculate the distance between two GPS coordinates, simulate a thousand dice rolls, or figure out how many days remain until a deadline. None of these problems require you to invent new mathematics, randomness, or calendar logic from scratch — they have already been solved, tested, and packaged for you inside Python itself. The only question is whether you know how to find the right tool and use it correctly. By the end of this chapter, you will.

10Sections
11Objectives
11Key Terms
30Total Chapters

Learning Objectives

By the end of this chapter, you should be able to:

  1. 1Explain what a module is and why Python organizes functionality into the standard library rather than loading everything by default.
  2. 2Use the import statement correctly and call module functions using dot notation.
  3. 3Explain what a namespace is and why namespaces prevent naming collisions between your code and module code.
  4. 4Distinguish between import module_name and from module import name, and explain why this textbook prefers the former.
  5. 5Use the math module for square roots, rounding, powers, logarithms, trigonometry, and constants.
  6. 6Use the random module to generate random integers and floats, make random choices, and shuffle a list.
  7. 7Use the datetime module to get the current date and time, create specific dates, format dates as strings, and calculate the difference between dates.
  8. 8Read a simple documentation entry for a function and identify its name, arguments, return value, and special behavior.
  9. 9Use the interactive shell and the help() function to explore unfamiliar module functions.
  10. 10Write a simple module of your own and import it from another file.
  11. 11Explain what responsible library use means and why the standard library should be the first choice before installing external packages.

Sections at a Glance