Functions from Modules

In previous exercises you’ve already worked with several built-in Python commands (or, functions): input and print are two examples. The Python interpreter has several functions built into it that are always available. There are many more functions which are not available by default.

To calculate the sine of a number, the function sin is available. But if you try to calculate this in Python with sin(1.0), an error will appear:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'sin' is not defined

Note the final line of this error message. This is what it says formulated in understandable English: Python does not know the name sin and therefore cannot execute the function!

To be able to use the sin-functionality you have to make sure the math-library and all the functions within this library are available in your program:

import math         # import the math library

x = 0.5
print(math.sin(x))

If you want to use the sin() function you should indicate in which library Python can find this function. There could also be other libraries that contain a sin function. We should thus explicitly choose for the math library.

Documentation