Understanding Functions and How to Call Them
Import the math module using import math to load it into your program and create a module object.
From Need to Module
Suppose your program needs a square root, a trigonometric function, or the value of pi. Python already provides these mathematical tools in the math module. Instead of writing these functions yourself, you bring the module into your program and then select the specific function or constant you need.
A module is a container of pre-built functions, constants, and other tools organized around a topic. The math module is a collection of mathematical tools.
The word import tells Python to bring a module into the program. The name math identifies which module to load. This statement does not yet call a mathematical function; it makes the math module available so that your program can use its contents.
What Import Creates
When Python runs import math, it searches for a file or built-in module named math, loads its code, and creates a module object. The name math in your current namespace is bound to that module object. You can think of the object as a container holding the functions and constants defined by the module.
The diagram separates two ideas that beginners often combine. math is the name used by your program, while the module object is the loaded collection represented by that name. The module object contains items such as mathematical functions and constants.
Printing math shows that it is a module object and that it is built in, meaning it is part of Python's standard library. The exact display is an object description rather than the mathematical contents themselves.
Reading Dot Notation
After the module object exists, use dot notation to select something inside it. The general form is module_name.item_name. The dot operator tells Python to look inside the object on its left and find the name on its right.
| Expression | What it selects | What happens next |
|---|---|---|
| math.sqrt | The sqrt function inside math | Parentheses can call the function |
| math.pi | The pi constant inside math | The value can be accessed directly |
Dot notation identifies the module first and then the item inside that module.
The distinction between math.sqrt and math.pi matters. math.sqrt identifies a function, so parentheses with an argument can call it. math.pi identifies a constant, so it is accessed as a value rather than called as a function.
Calling a Module Function
What do you think happens?
What will these three lines do?
Reveal answer
Answer: They load math, calculate a square root, and access pi.
The first line imports the module. The second line uses dot notation to call math.sqrt with 16, which returns 4.0. The third line uses dot notation to access the pi constant.
import math print(math.sqrt(16)) print(math.pi)
4.0
The full floating-point value of piThe call math.sqrt(16) can be traced in order. Python first uses math to identify the module object, then uses sqrt to find the function inside that object, then supplies 16 as the function's argument. The function returns 4.0, and print displays that result. The expression math.pi follows the same module lookup pattern but accesses a constant instead of calling a function.
Read a dotted expression from left to right: identify the object on the left of the dot, then identify the item being requested on the right. If the item is a function, check whether the expression includes the parentheses needed to call it.
Correcting Member Access
Dot notation only works when each name identifies the intended object or member. A misspelled module name does not identify the math module. A missing attribute does not identify an item contained in that module. Incorrect member access can therefore produce an error instead of the expected result.
| Expression | Status | Correction or meaning |
|---|---|---|
| math.sqrt(25) | Valid | Find sqrt in math and call it with 25 |
| Math.sqrt(25) | Incorrect module name | Use the imported name math |
| math.squrt(25) | Incorrect member name | Use the member name sqrt |
| math.pi | Valid | Access the pi constant inside math |
Using a different spelling or capitalization for the module name
The imported name in the example is math, so Math does not identify that module name.
Fix:
Write math.sqrt(25).Misspelling the member name
The requested name does not identify the sqrt function inside the math module.
Fix:
Write math.sqrt(25).Treating the module name as if it were the function call by itself
The module name identifies the module object. Dot notation is needed to select a function inside it.
Fix:
Select the function with math.sqrt and then supply the argument: math.sqrt(25).
Practice the Lookup
Write a short Python program that imports math, prints the result of math.sqrt(25), and then prints math.pi. Before checking your work, identify which expression calls a function and which expression accesses a constant.
Hints
- Begin with import math.
- Use dot notation to select sqrt from the math module.
- Use parentheses for the function call and no parentheses when accessing pi.
To check the program, confirm that the first output is the square-root result for 25 and that the second output is the full floating-point value of pi. Also confirm that the module is imported before either member is used.
Key Takeaways
- import math loads the math module and creates a module object represented by the name math.
- A module is a container of related functions, constants, and other tools.
- Dot notation has the form module_name.item_name and tells Python where to look.
- math.sqrt selects a function that can be called with parentheses and an argument.
- math.pi selects a constant directly, while misspelled module or member names lead to incorrect access.
Key Takeaways
- Importing math loads a module and binds the name math to its module object.
- Dot notation lets a program select a function or constant inside that module.
- math.sqrt(16) selects and calls sqrt, producing 4.0.
- math.pi accesses the pi constant without calling it.
- Correct spelling and ordering are essential when debugging module member access.