Foundations of Python

You are currently auditing this course.
61 / 134

Math Functions

Python provides us with a module called math which has certain functions and variables that help us in doing mathematical calculations. Functions in math module return float values. Before we can use the module, we have to import it:

import math

Try print(math) and see the result. (You need to import math before printing)

To access one of the functions, we need to specify the name of the module and the name of the function, separated by a dot (also known as a period). This format is called dot notation.

math.log10(1000)

It will calculate log to the base 10 and return 3.0 in this case. The math module also provides a function called log that computes logarithms to the base e.

math.cos(0)

It will find the cosine of radians and return 1.0 in this case. The name of the variable is a hint that cos and the other trigonometric functions (sin, tan, etc.) take arguments in radians.

The expression math.pi gets the variable pi from the math module. The value of this variable is an approximation of π, accurate to about 15 digits. To convert from degrees to radians, divide by 360 and multiply by 2π.

math.sqrt(4)

It will calculate the square root of 4 and return 2.0 here.

INSTRUCTIONS
  • Import math module
  • Calculate the square root of 56.98 and store it in the variable square_root
  • Convert 26 degrees into radians and store it to sin_rad
  • Calculate the sin of 26 degrees and store it in the variable sin_degree
  • Calculate the log to the base e for 1000 and store it in the variable log_e

Loading comments...