Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Engineering LibreTexts

7: Modules

( \newcommand{\kernel}{\mathrm{null}\,}\)

  • 7.0: Introduction
    This page emphasizes the importance of organizing code into modules for larger, complex programs. It covers how to define, import, and locate modules, and highlights Python's extensive standard library with over 200 built-in modules. A module is identified as a .py file containing function definitions, with its name matching the file name. While all programs can function as modules, not all are intended for direct execution, exemplified by the greetings.
  • 7.1: Module Basics
    This page discusses the goals and techniques for defining and importing Python modules, highlighted with an “area” module example for geometric area calculations. It explains usage in other programs and offers exercises for creating a conversion module for temperature and distance. There's also a task to write a program that combines this conversion module with user input for output results.
  • 7.2: Importing Names
    This page discusses learning objectives for importing functions in Python using the "from" keyword, emphasizing direct imports and name collision risks. It provides checkpoints and practice questions to strengthen understanding. The importance of careful naming to avoid unexpected behavior due to name conflicts, which do not generate errors in Python, is also highlighted.
  • 7.3: Top-level Code
    This page discusses Python module behavior, highlighting potential side effects from importing modules and the significance of the `if __name__ == "__main__"` statement. It explains that top-level code runs upon import, which can lead to unintended consequences, and provides examples to illustrate this. The text emphasizes the importance of using the conditional statement to control execution and prevent code from running prematurely. Exercises are included to reinforce the concepts presented.
  • 7.4: The Help Function
    This page introduces a Python module, colors.py, for handling HTML colors, explaining color specifications including standard names and hexadecimal formats. It details various color constants and conversion functions (between hexadecimal and RGB) as well as techniques for lightening and darkening colors. The help() function is emphasized for exploring module details and documentation. Additionally, practice questions are provided to reinforce the concepts discussed.
  • 7.5: Finding Modules
    This page outlines the Python Standard Library, highlighting built-in modules with over 200 standard functionalities, and third-party modules available on the Python Package Index (PyPI) for community sharing. It provides examples like `math`, `datetime`, and `requests`, while encouraging hands-on activities, such as calculating age with the `datetime` module, to reinforce learning.
  • 7.6: Chapter Summary
    This page covers organizing Python programs through the use of multiple .py files (modules) and the import system. It emphasizes the significance of the if __name__ == "__main__" statement for execution control. The help() function is introduced for accessing module documentation, and there are over 200 built-in modules available, along with many third-party options. The chapter includes examples of importing modules and accessing their documentation.


This page titled 7: Modules is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.

  • Was this article helpful?

Support Center

How can we help?