官术网_书友最值得收藏!

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
主站蜘蛛池模板: 哈巴河县| 洛浦县| 东方市| 南澳县| 平山县| 新津县| 岳西县| 新民市| 贵定县| 科尔| 宁海县| 防城港市| 偏关县| 佛山市| 长顺县| 得荣县| 达州市| 无极县| 新闻| 绍兴县| 台湾省| 临西县| 东港市| 涟水县| 瓦房店市| 广昌县| 怀集县| 临武县| 吉首市| 广水市| 饶河县| 石柱| 民勤县| 周口市| 湖北省| 宜宾市| 内丘县| 甘泉县| 东乌| 安远县| 新和县|