Importing standard library modules

As mentioned earlier, Python comes with an extensive standard library, an aspect of Python that is often referred to as batteries included. The standard library is structured as modules, a topic we'll discuss in depth later. What's important at this stage is to know that you gain access to standard library modules by using the import keyword.

The basic form of importing a module is simply the import keyword followed by a space and the name of the module. For example, lets see how we can use the standard library's math module to compute square roots. At the triple-arrow prompt we type the following command:

>>> import math

Since import is a statement which doesn't return a value, Python doesn't print anything if the import succeeds, and we're immediately returned to the prompt. We can access the contents of the imported module by using the name of the module, followed by a dot, followed by the name of the attribute in the module that you need. Like many object
oriented languages the dot operator is used to drill down into object structures. Being expert Pythonistas, we have inside knowledge that the math module contains a function called sqrt(). Let's try to use the following command:

>>> math.sqrt(81)
9.0
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset