Other Math Module Functions: Trigonometry and Logarithms
math.pi is a variable from the math module that provides an approximation of π accurate to about 15 digits; use it without parentheses.
From Mathematical Idea to Python Value
Geometry, physics, and engineering calculations often need both precise mathematical values and reliable mathematical operations. Python's math module provides math.pi for an approximation of π and math.sqrt() for computing square roots. These tools let you use established implementations instead of entering your own approximation or writing your own square-root algorithm.
math.pi is a variable from the math module containing an approximation of π accurate to about 15 digits. Because it is a variable rather than a function call, you access it without parentheses.
math.sqrt() is a function that computes the square root of a non-negative number. You call it with parentheses and pass the number as its argument.
Tracing a Square-Root Calculation
What do you think happens?
What should the final value be when Python computes the square root of 2 and then divides that result by 2.0?
Reveal answer
Answer: The square root of 2 divided by 2.0, approximately 0.7071
Python first sends 2 into math.sqrt(), receives the square root of 2, and then divides that returned value by 2.0.
What Python Does at Each Step
When Python evaluates math.sqrt(2), the value 2 flows into the function. The function performs the square-root calculation internally and returns the result. Python can then use that returned result in another operation, such as division, or store it in a variable or print it.
import math root = math.sqrt(2) result = root / 2.0 print(result)
approximately 0.7071067811865476The division does not happen inside math.sqrt(). The function returns a square-root result first; the separate arithmetic operation then uses that result.
Using Pi with a Square Root
You can combine math.pi and math.sqrt() in one calculation when a geometry, physics, or engineering problem requires both. The important part is to track each value: math.pi supplies the approximation of π, math.sqrt() transforms its input into a square root, and arithmetic operations combine the resulting values.
Combining the Two Math Tools
Evaluate an expression that multiplies math.pi by the square root of 9.
Access pi: math.pi contributes the math module's approximation of π. It is accessed directly, without parentheses.
Compute the square root: math.sqrt(9) sends 9 into the square-root function and returns 3.
Combine the results: The multiplication operation uses the value supplied by math.pi and the value returned by math.sqrt(9).
The expression produces a value based on the approximately 15-digit value supplied by math.pi and the square-root result 3.
Mistakes with Values and Functions
Writing parentheses after math.pi
math.pi is a variable containing a value, not the square-root-style function described by a call with an argument.
Fix:
Access math.pi directly, without parentheses.Leaving out parentheses when using math.sqrt
math.sqrt() is a function and needs an argument so it knows which non-negative number to process.
Fix:
Call the function with parentheses and place the number inside them, as in math.sqrt(9).Assuming math.sqrt() performs later arithmetic
math.sqrt() computes and returns only the square root. The division is a separate operation performed on the returned value.
Fix:
Trace the calculation in order: input, square-root result, then arithmetic operation.Typing a short approximation instead of using math.pi
The math module provides an approximation of π accurate to about 15 digits.
Fix:
Use math.pi when the calculation needs the math module's supplied approximation.
Check Your Understanding
Explain the value flow in an expression that uses math.pi and math.sqrt(16) together. Identify which item supplies a stored mathematical value, which item performs a function call, what result the function returns, and how an arithmetic operation could use both results.
Hints
- Remember that math.pi is accessed without parentheses.
- The argument to math.sqrt() is 16.
- Trace the calculation after math.sqrt() returns its result.
Suppose a calculation contains math.sqrt(25) / 5. Describe the two stages of the calculation and explain why the result can be checked using ordinary arithmetic.
Hints
- First determine what math.sqrt(25) returns.
- Then apply the division by 5.
- The returned function value becomes the input to the later arithmetic operation.
Key Takeaways
- Use math.pi as the math module's approximation of π, accurate to about 15 digits, and access it without parentheses.
- Use math.sqrt() with parentheses and a non-negative argument to compute a square root.
- A value flows into math.sqrt(), the function returns a result, and later arithmetic can use that result.
- Combining math.pi and math.sqrt() supports calculations in geometry, physics, and engineering.
- To verify a result, separate the function's returned value from the arithmetic operation that follows it.
Key Takeaways
- math.pi is a variable containing an approximation of π accurate to about 15 digits, so it is used without parentheses.
- math.sqrt() computes the square root of a non-negative number and requires parentheses around its argument.
- Python returns the square-root result before applying later arithmetic such as division or multiplication.
- Using math.pi and math.sqrt() together helps build precise calculations for geometry, physics, and engineering.