Concepts / Working with Built-in Functions

Working with Built-in Functions

Python comes with a large library of pre-built functions organized into modules.

  • 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.

Key point

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.

mathname in the currentnamespacemath modulemodule objectsqrtfunctionpiconstantrefers tocontainscontains
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.

mathmodule objectsqrtfunctionpiconstantmath.sqrtmath.pi
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.

Code walkthrough
import math
print(math.sqrt(16))
print(math.pi)
  1. 1
    First, Python loads the math module and binds the module object to math.
  2. 2
    Next, math.sqrt uses the dot to locate sqrt inside that module. Parentheses call the function with 16.
  3. 3
    Finally, math.pi uses the dot to locate the constant pi. No function call is needed for a constant.
  • The first line imports the math module.
  • The second line finds sqrt inside math, calls it with 16, and prints the returned value 4.0.
  • The third line finds the pi constant inside math and prints its floating-point value.
Output
4.0
3.141592653589793
import mathload modulemath modulemodule objectmath.sqrt(16)call function4.0returned valuecreates access tofindsreturns
What sequence does Python follow from importing math to using its members?

Dot Notation Mistakes

  • Common mistake

    Trying to use an item from math before importing math

    What it looks like
    math.sqrt(16)
    Why it fails

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

    Do this instead

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

  • Common mistake

    Leaving out the module name

    What it looks like
    .sqrt(16)
    Why it fails

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

    Do this instead

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

  • Common mistake

    Using the function name without the module name

    What it looks like
    sqrt(16)
    Why it fails

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

    Do this instead
    Write math.sqrt(16).
  • Common mistake

    Adding parentheses when accessing the constant pi

    What it looks like
    math.pi()
    Why it fails

    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.

    Do this instead
    Write math.pi.
  • Common mistake

    Using an incorrect item name after the dot

    What it looks like
    math.square_root(16)
    Why it fails

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

    Do this instead
    Use math.sqrt(16).
Before
import math
print(math.pi())

The expression treats pi as though it were being called with parentheses.

After
import math
print(math.pi)

The expression accesses pi as a constant stored in the math module.

Applying the Pattern

Worked example

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?

  1. 1
    Load the module

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

  2. 2
    Use the function

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

  3. 3
    Use the constant

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

Result

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

At a glance

  • 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.

Related concepts

More concepts starting with W