The Python programming language has math library which provides commonly used mathematical functions and more. To import all these mathematical functions into Python or Python script we have to type in the following command.
Example 1 Create a program that will take user input (number type float) then calculate and show as the output the sine, cosine, exponential (exp(x)), natural logarithm , base-10 logarithm, and base-2 logarithm.
Solution:
Solution
import mathThe previous code line creates a module object named math. If you print the name of the math module
print(math)The output:
< module 'math' (built-in) >In this module, various mathematical functions are defined and to access them you have to specify the name of the module and the name of the function which is separated by a dot (dot notation). So to use any function from the math module you have to type the name of the module (math) followed by a dot (.) and followed by the built-in mathematical function you wan to use (e.g. sqrt, log, log10, exp,...).
Example 1 Create a program that will take user input (number type float) then calculate and show as the output the sine, cosine, exponential (exp(x)), natural logarithm , base-10 logarithm, and base-2 logarithm.
Solution:
import mathOutput:
while True:
try:
USER_INPUT = input("Enter a number:> ")
if isinstance(float(USER_INPUT), float):
print("The number is float type")
print("math.sin(USER_INPUT) = {}".format(math.sin(float(USER_INPUT))))
print("math.cos(USER_INPUT) = {}".format(math.cos(float(USER_INPUT))))
print("math.exp(USER_INPUT) = {}".format(math.exp(float(USER_INPUT))))
print("math.log(USER_INPUT) = {}".format(math.log(float(USER_INPUT))))
print("math.log10(USER_INPUT) = {}".format(math.log10(float(USER_INPUT))))
print("math.log2(USER_INPUT) = {}".format(math.log2(float(USER_INPUT))))
break
else:
print("Enter another number.")
except:
print("Enter another number.")
Enter a number:> 4.23Aside from mathematical functions, the math module has the following mathematical constants:
The number is float type.
math.sin(USER_INPUT) = -0.8858892112966027
math.cos(USER_INPUT) = -0.4638968692589801
math.exp(USER_INPUT) = 68.7172321738465
math.log(USER_INPUT) = 1.4422019930581866
math.log10(USER_INPUT) = 0.6263403673750424
math.log2(USER_INPUT) = 2.0806576633452254
- math.e - Euler's number
- math.inf - Returns a floating-point positive infinity
- math.nan - Returns a floating-point NaN (not a number) value
- math.pi - returns PI (3.1415...)
- math.tau - Returns tau (6.2831...)
Solution
import mathOutput:
while True:
try:
USER_INPUT = input("Enter a circle radius in meters:> ")
if isinstance(float(USER_INPUT), float):
print("The number is float type.")
CircleArea = (float(USER_INPUT))**2*math.pi
print("The area of the circle is {} [m^2]".format(CircleArea))
break
else:
print("Enter another number.")
except:
print("Failed! Enter another number.")
Enter a circle radius in meters:> 4.56
The number is float type.
The area of the circle is 65.32502100168472 [m^2]