Concepts / Working with Built-in Functions

Working with Built-in Functions

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

  • Programming

Why Modules Matter

Python comes with a large library of pre-built functions organized into modules. A module is a container of 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 the modules you need.

The central pattern is import the module first, then use module_name.item_name to access something stored inside it.

Importing the Math Module

python

The import statement tells Python to load the math module and create an object that represents it in your program's memory. Python searches for a file or built-in module named math, loads its code, and binds the resulting module object to the name math in the current namespace. After this statement runs, math is the name you use to reach the module.

refers tocontainscontainsmathmath modulesqrtpi
What objects and names are created when Python runs import math?

Following Dot Notation

The dot operator tells Python to look inside an object and find the name that follows it. With a module, the general form is module_name.item_name. In math.sqrt, Python looks inside the math module for the item named sqrt. In math.pi, Python looks inside the same module for the item named pi.

math.sqrtmath.pimathsqrtpi
How does Python move from the module object named math to a function or constant?
ExpressionWhat it accessesHow it is used
math.sqrtThe sqrt function inside mathCall it with parentheses, such as math.sqrt(16)
math.piThe pi constant inside mathAccess it by name, such as math.pi

Dot notation identifies an item inside the math module.

Tracing a Complete Use

What do you think happens?

What happens when Python runs the following three lines?

  • It imports math, calculates the square root of 16, and prints pi
  • It imports math but cannot use sqrt because functions are not stored in modules
  • It prints the module object instead of calculating a square root
Reveal answer

Answer: Python imports math, math.sqrt(16) returns 4.0, and math.pi is printed as its floating-point value.

The first line creates access to the math module. The second line uses dot notation to find sqrt inside that module and calls it with 16. The third line uses dot notation to access the pi constant.

import math print(math.sqrt(16)) print(math.pi)

creates access tofindsreturnsimport mathmath modulemath.sqrt(16)4.0
What sequence does Python follow from importing math to using its members?

Dot Notation Mistakes

  • Trying to use an item from math before importing math

    Python has not yet been told to load the math module and bind a module object to the name math.

    Fix: Run import math before using math.sqrt or math.pi.

  • Leaving out the module name

    The dot operator needs an object or name on its left so Python knows where to look.

    Fix: Write math.sqrt(16) after importing the math module.

  • Using the function name without the module name

    The sqrt function is being accessed through the math module in the source pattern, so the module name identifies where Python should look.

    Fix: Write math.sqrt(16).

  • Adding parentheses when accessing the constant pi

    math.pi is a constant access. The source example accesses it by name and prints its value; it is not shown as a function call.

    Fix: Write math.pi.

  • Using an incorrect item name after the dot

    Dot notation asks Python to find the specific name that follows the dot. The source identifies the function as sqrt.

    Fix: Use math.sqrt(16).

Applying the Pattern

Choose the Correct Access Form

You need to use the square-root function and the pi constant from the math module. Which expressions match the role of each item?

Load the module: Begin with import math so Python loads the module and binds its module object to math.

Use the function: Use math.sqrt(16) because sqrt is a function and the parentheses provide its input.

Use the constant: Use math.pi because pi is accessed as a constant by placing its name after the dot.

The complete pattern is import math, followed by math.sqrt(16) for the function and math.pi for the constant.

MEDIUM

For each expression, decide whether it correctly uses the math module. If it is incorrect, rewrite it using the import-and-dot-notation pattern: math.sqrt(16), math.pi(), .sqrt(16), and sqrt(16).

Hints
  • Check whether math has been imported before the expression is used.
  • Check whether the module name appears before the dot.
  • Check whether the expression is accessing a function or a constant.

Key Takeaways

  1. import math tells Python to load the math module and create a module object.
  2. The name math is bound to that module object in the current namespace.
  3. A module is a container of organized functions, constants, and other tools.
  4. Dot notation uses the form module_name.item_name to find an item inside a module.
  5. Functions such as math.sqrt(16) are called with parentheses, while constants such as math.pi are accessed by name.

Key Takeaways

  • Importing math creates access to a module object and binds that object to the name math.
  • The math module organizes mathematical functions and constants.
  • The dot operator tells Python to look inside the module for a named item.
  • Use parentheses to call a function such as math.sqrt, but access a constant such as math.pi by name.
  • Most dot-notation errors can be checked by verifying the import, the module name, the item name, and whether parentheses are appropriate.