python中的library(库)

Reference: Working with External Libraries Click me!

本节将讨论python中库的导入以及介绍部分常用的库和它们所返回的对象。

1. 导入(imports)

我们之前有学过数据类型、自定义内置函数等,但python中最美好的事情大概是它所自带的库了吧;

有一些库称为“标准库”,可以轻松调用;但有一些库必须使用 ”imports“ 导入;

我们来看一个例子:

import math # 导入math

print("It's math! It has type {}".format(type(math)))

输出:

It's math! It has type <class 'module'>

解析:
math是一个模块。模块是指所有变量的集合。

我们可以用dir()函数去查看math模块中所存在的所有变量:

>>> import math # 每次使用math前,都别忘了导入哦
>>> dir(math)

现在知道了每个模块中都含有很多变量,那么怎么调用呢?

那就是模块名称 . 变量名;如调用pi的值:math.pi;

>>> import math
>>> "pi to 4 significant digits = {:.4}".format(math.pi)
"pi to 4 significant digits = 3.142"

除了pi,经常用到的还有math.log(),使用格式:math.log(底数, 对数):

>>> import math
>>> math.log(8,2)
3.0

2. 其他import语法

1、有时候,可能某个模块经常需要被用到,但是有些模块名称特别长,特别难记;这时候可以给模块起个容易记

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python3.6标准 It contains data types that would normally be considered part of the “core” of a language, such as numbers and lists. For these types, the Python language core defines the form of literals and places some constraints on their semantics, but does not fully define the semantics. The library also contains built-in functions and exceptions — objects that can be used by all Python code without the need of an import statement. Some of these are defined by the core language, but many are not essential for the core semantics and are only described here. The bulk of the library, however, consists of a collection of modules. There are many ways to dissect this collection. Some modules are written in C and built in to the Python interpreter; others are written in Python and imported in source form. Some modules provide interfaces that are highly specific to Python, like printing a stack trace; some provide interfaces that are specific to particular operating systems, such as access to specific hardware; others provide interfaces that are specific to a particular application domain, like the World Wide Web. Some modules are available in all versions and ports of Python; others are only available when the underlying system supports or requires them; yet others are available only when a particular configuration option was chosen at the time when Python was compiled and installed. This manual is organized “from the inside out:” it first describes the built-in functions, data types and exceptions, and finally the modules, grouped in chapters of related modules. This means that if you start reading this manual from the start, and skip to the next chapter when you get bored, you will get a reasonable overview of the available modules and application areas that are supported by the Python library. Of course, you don’t have to read it like a novel — you can also browse the table of contents (in front of the manual), or look for a specific function, module or term in the index (in the back). And finally, if you enjoy learning about random subjects, you choose a random page number (see module random) and read a section or two. Regardless of the order in which you read the sections of this manual, it helps to start with chapter Built-in Functions, as the remainder of the manual assumes familiarity with this material.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值