- The Python Apprentice
- Robert Smallshire Austin Bingham
- 212字
- 2021-07-02 22:16:52
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
- Spring 5企業級開發實戰
- vSphere High Performance Cookbook
- CentOS 7 Server Deployment Cookbook
- JavaScript語言精髓與編程實踐(第3版)
- Ext JS Data-driven Application Design
- Python金融數據分析
- PHP 編程從入門到實踐
- OpenShift在企業中的實踐:PaaS DevOps微服務(第2版)
- 基于Swift語言的iOS App 商業實戰教程
- Apex Design Patterns
- Reactive Android Programming
- Cocos2d-x學習筆記:完全掌握Lua API與游戲項目開發 (未來書庫)
- 青少年學Python(第1冊)
- Arduino可穿戴設備開發
- 嵌入式Linux C語言程序設計基礎教程