O'Reilly logo

Python for Finance by Yuxing Yan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Importing a module

Assume that we want to estimate the square root of the number three. However, after issuing the following lines of code, we would encounter an error message:

>>>sqrt(3)
SyntaxError: invalid syntax
>>>

The reason is that the sqrt() function is not a built-in function. To use the sqrt() function, we need to import the math module first as follows:

>>>import math
>>>x=math.sqrt(3)
>>>round(x,4)
1.7321

To use the sqrt() function, we have to type math.sqrt() if we use the import math command to upload the math module. In addition, after issuing the command dir(), we will see the existence of the math module, which is the last one in the output shown as follows:

>>>dir()
['__builtins__', '__doc__', '__name__', '__package__', 'math'] ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required