Working with Built-in Functions
Python comes with a large library of pre-built functions organized into modules.
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
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.
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.
| Expression | What it accesses | How it is used |
|---|---|---|
| math.sqrt | The sqrt function inside math | Call it with parentheses, such as math.sqrt(16) |
| math.pi | The pi constant inside math | Access 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?
Reveal 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)- 1First, Python loads the math module and binds the module object to math.
- 2Next,
math.sqrtuses the dot to locate sqrt inside that module. Parentheses call the function with 16. - 3Finally,
math.piuses 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.
4.0
3.141592653589793Dot Notation Mistakes
Common mistake Trying to use an item from math before importing math
What it looks likemath.sqrt(16)Why it failsPython has not yet been told to load the math module and bind a module object to the name math.
Do this insteadRun import math before using math.sqrt or math.pi.
Common mistake Leaving out the module name
What it looks like.sqrt(16)Why it failsThe dot operator needs an object or name on its left so Python knows where to look.
Do this insteadWrite math.sqrt(16) after importing the math module.
Common mistake Using the function name without the module name
What it looks likesqrt(16)Why it failsThe sqrt function is being accessed through the math module in the source pattern, so the module name identifies where Python should look.
Do this insteadWrite math.sqrt(16).Common mistake Adding parentheses when accessing the constant pi
What it looks likemath.pi()Why it failsmath.piis a constant access. The source example accesses it by name and prints its value; it is not shown as a function call.Do this insteadWrite math.pi.Common mistake Using an incorrect item name after the dot
What it looks likemath.square_root(16)Why it failsDot notation asks Python to find the specific name that follows the dot. The source identifies the function as sqrt.
Do this insteadUse math.sqrt(16).
import math
print(math.pi())The expression treats pi as though it were being called with parentheses.
import math
print(math.pi)The expression accesses pi as a constant stored in the math module.
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?
- 1Load the module
Begin with import math so Python loads the module and binds its module object to math.
- 2Use the function
Use
math.sqrt(16)because sqrt is a function and the parentheses provide its input. - 3Use the constant
Use
math.pibecause 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.
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
- 1import math tells Python to load the math module and create a module object.
- 2The name math is bound to that module object in the current namespace.
- 3A module is a container of organized functions, constants, and other tools.
- 4Dot notation uses the form
module_name.item_name to find an item inside a module. - 5Functions such as
math.sqrt(16)are called with parentheses, while constants such asmath.piare 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 asmath.piby name. - Most dot-notation errors can be checked by verifying the import, the module name, the item name, and whether parentheses are appropriate.