Concepts / Creating and Using Your Own Modules

Creating and Using Your Own Modules

Import the math module using import math to load it into your program and create a module object.

  • Programming

Why Modules Matter

Python provides many pre-built functions and constants, organized into modules by topic. A module is a file containing Python code that defines functions, constants, and other tools. The math module collects mathematical tools such as square root, sine, cosine, logarithm, pi, and e. Instead of loading every available tool into every program, Python lets you choose which modules to bring into your code.

The math module is a container of mathematical functions and constants. Before using its contents, your program must import it.

The Import State Change

The statement import math tells Python to load the math module into the program. Python searches for a file or built-in module named math, loads its code, and creates a module object. That object represents the module in the program's memory. The object is then bound to the name math in the current namespace.

python
executessearches forloads and createsbinds to nameProgramimport mathmath moduleModule objectmathCurrent namespacemath
What changes when the program executes import math?

The important state change is that the name math becomes available in the current namespace and refers to the module object. The module object acts as a container for the functions and constants defined in math.

Inside the Module Object

A module object contains names connected to the tools provided by that module. In the math module, names include functions such as sqrt, sine, cosine, and logarithm, as well as constants such as pi and e. The name math identifies the module object; the names after the dot identify specific contents inside that object.

containscontainscontainscontainscontainsmathmodule objectsqrtfunctionsinfunctioncosfunctionpiconstanteconstant
What names are connected to the math module object?

Dot notation uses the pattern module_name.item_name. The dot operator tells Python to look inside the module object for the specific function or constant named after the dot.

Reading Dot Notation

In math.sqrt, math identifies the module object and sqrt identifies the function inside that object. In math.pi, math again identifies the module object, while pi identifies the constant. The dot does not perform the mathematical operation by itself. It tells Python where to look for the named item.

dot lookupdot lookupcalled with 16accessesmathmodule objectsqrtfunction4.0piconstantπ value
How does an expression such as math.sqrt or math.pi move from the module object to a function or constant?

Using a Function and a Constant

Use the math module to calculate the square root of 16 and access pi.

Import: Execute import math so Python loads the module and binds the module object to the name math.

Find the function: Read math.sqrt as a request to find sqrt inside the math module.

Call the function: The expression math.sqrt(16) calls the square-root function with 16 and returns 4.0.

Find the constant: Read math.pi as a request to access the pi constant inside the math module.

The square-root expression produces 4.0, and math.pi provides the full floating-point value of pi.

python
Output
The first line prints 4.0. The second line prints the full floating-point value of math.pi.

Checking the Lookup

Incorrect dot notation usually means that one part of the lookup does not identify the intended module or member. Check the expression from left to right: first confirm the module name, then confirm the function or constant name after the dot. The correction is to use the module name and member name provided by the module.

looks insidefindslooks insidecannot find intended namemath.sqrt(9)mathmath.square(9)mathsqrtsquare
What is different between a valid module lookup and one that uses the wrong module or member name?
  • Using a member name that is not the intended name in the module

    The dot notation asks Python to find square inside math, while the source example identifies the square-root function as sqrt.

    Fix: Use math.sqrt(9) when you want the square-root function.

  • Changing the module name in the expression

    The imported module name in this lesson is math, so the expression no longer refers to that module object.

    Fix: Write import math first, then use math.sqrt(9).

  • Trying to use module contents before importing the module

    The program has not yet executed the import statement that loads math and binds the name math in the current namespace.

    Fix: Execute import math before using math.pi or another math member.

Practice the Trace

What do you think happens?

Before reading the explanation, predict what each line does: import math, math.sqrt(16), and math.pi.

  • The first line imports math; the second calculates a square root; the third accesses pi.
  • All three lines create new modules.
  • The dot operator imports a new module each time it appears.
Reveal answer

Answer: The import line loads the math module and binds its module object to math. The second expression accesses sqrt through dot notation, calls it with 16, and returns 4.0. The third expression accesses the pi constant through dot notation.

The first line creates access to the module object. The next two expressions use the name math to look inside that object for a function and a constant.

EASY

Write the correct expression for each request: access the pi constant in the math module; call the square-root function in the math module with 25; and repair the incorrect expression math.square(25).

Hints
  • Begin each expression with the imported module name math.
  • Use a dot before the member name.
  • The square-root member name used in this lesson is sqrt.

Key Takeaways

  1. Use import math to load the math module into a program.
  2. Importing creates a module object and binds that object to the name math in the current namespace.
  3. A module is a container of related functions, constants, and other tools.
  4. Use module_name.item_name dot notation to access a function or constant inside a module.
  5. When dot notation fails, check both the module name before the dot and the member name after it.

Key Takeaways

  • Importing math loads the module and creates a module object represented by the name math.
  • The module object contains mathematical functions and constants.
  • The dot operator directs Python to look inside the module object for a named member.
  • math.sqrt(16) accesses and calls the sqrt function, while math.pi accesses the pi constant.
  • Incorrect module or member names cause the lookup to fail; checking both sides of the dot helps locate the mistake.