Other Math Module Functions: Trigonometry and Logarithms
The division does not happen inside math.sqrt(). The function returns a square-root result first; the separate arithmetic operation then uses that result.
Try it: The math Module: Logs, Roots and Angles
What math.log(x), math.log10(x), math.log(x, base), math.sqrt(x) and math.sin / cos / tan really compute: which logarithm base each call uses, why 0 and negative inputs must be checked before the call (ValueError: math domain error), why trigonometric functions need radians, and that math.pi and every result are binary64 approximations (math.log(1000, 10) prints 2.9999999999999996).
How it works
- math.log(x) is the natural logarithm (base e); math.log10(x) is base 10; math.log(x, base) computes log(x) / log(base) with two rounded natural logs.
- Logarithms need x > 0 and math.sqrt needs x >= 0; otherwise the call raises ValueError: math domain error, so a guard checks the input first.
- math.sin, math.cos and math.tan read their argument as radians: convert degrees with degrees / 360.0 * 2 * math.pi.
- math.pi is a float value (no parentheses) that approximates pi to about 16 digits, so sin(180 degrees) is 1.2246467991473532e-16, not 0.
- Every result is the nearest binary64 value; print shows the shortest repr that reads back as the same float, while the stored value has more digits.
Default run (6 steps): Ready: the program calls math.log10(x). … print(result) shows 3.0.
Simplified: One-call programs over int / float literals you type (|x| up to 1e15). The results come from the browser's IEEE-754 math library: math.sqrt and the radians arithmetic are identical to CPython; log / sin / cos / tan match CPython 3.12 (Windows) exactly for about 91% of the tested inputs and are within 2 units in the last place otherwise, because C math libraries differ in the last bit. Error messages are CPython 3.12's (3.14 says 'expected a positive input').
Loading the simulation…
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
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)- 1The import makes the math module available under the name math.
- 2The call
math.sqrt(2)sends 2 into the square-root function. - 3The function returns the square root of 2, which is assigned to root.
- 4The expression root / 2.0 uses that returned value in arithmetic.
- 5The print statement displays the final result.
- Imports the math module so its mathematical tools can be accessed.
- Calls
math.sqrt()with 2 and stores the returned square root. - Divides the returned square root by 2.0 and stores the new result.
- Prints the final value.
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.
- 1Access pi
math.picontributes the math module's approximation of π. It is accessed directly, without parentheses. - 2Compute the square root
math.sqrt(9)sends 9 into the square-root function and returns 3. - 3Combine the results
The multiplication operation uses the value supplied by
math.piand the value returned bymath.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
Common mistake Writing parentheses after math.pi
What it looks likeTreating math.pi as though it were a function call
Why it failsmath.piis a variable containing a value, not the square-root-style function described by a call with an argument.Do this insteadAccess math.pi directly, without parentheses.
Common mistake Leaving out parentheses when using math.sqrt
What it looks likeReferring to math.sqrt without passing a number
Why it failsmath.sqrt()is a function and needs an argument so it knows which non-negative number to process.Do this insteadCall the function with parentheses and place the number inside them, as in math.sqrt(9).
Common mistake Assuming math.sqrt() performs later arithmetic
What it looks likeExpecting math.sqrt(2) / 2.0 to be handled entirely by math.sqrt()Why it failsmath.sqrt()computes and returns only the square root. The division is a separate operation performed on the returned value.Do this insteadTrace the calculation in order: input, square-root result, then arithmetic operation.
Common mistake Typing a short approximation instead of using math.pi
What it looks likeReplacing math.pi with a manually entered approximation in a precision-sensitive calculation
Why it failsThe math module provides an approximation of π accurate to about 15 digits.
Do this insteadUse 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.piis 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
- 1Use
math.pias the math module's approximation of π, accurate to about 15 digits, and access it without parentheses. - 2Use
math.sqrt()with parentheses and a non-negative argument to compute a square root. - 3A value flows into
math.sqrt(), the function returns a result, and later arithmetic can use that result. - 4Combining
math.piandmath.sqrt()supports calculations in geometry, physics, and engineering. - 5To verify a result, separate the function's returned value from the arithmetic operation that follows it.
At a glance
math.piis 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.piandmath.sqrt()together helps build precise calculations for geometry, physics, and engineering.