购买
下载掌阅APP,畅读海量书库
立即打开
畅读海量书库
扫码下载掌阅APP

在使用Python时,帮助文档是非常有用的,尤其是对于初学者来说。使用命令help可以进入help帮助文档界面:

>>> help()
Welcome to Python 3.8's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet. . .

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

help>

继续键入相应关键词进行查询。例如,继续键入“modules”可以列出当前所有安装的模块,包括库。使用help函数也可以查看某个库或者函数的帮助文档。例如,要查看math库的帮助文档,可以输入:

>>> import math
>>> help(math)
Help on built-in module math:

NAME
    math

DESCRIPTION
    This module provides access to the mathematical functions
    defined by the C standard.

FUNCTIONS
    acos(x, /)
        Return the arc cosine (measured in radians) of x.
...

要查看函数round的帮助文档,可以输入:

>>> help(round)
Help on built-in function round in module builtins:
round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None.  Otherwise
    the return value has the same type as the number. ndigits may be negative.

或者,更简单地,输入:

>>> ?round

注意,如果要查看帮助的对象不在Python标准库而在第三方库里,需要先导入库。例如,要查看numpy库里的函数mean的帮助文档,可以输入以下命令:

>>> import numpy as np
>>> help(np.mean)
Help on function mean in module numpy:
mean(a, axis=None, dtype=None, out=None, keepdims=<no value>)
    Compute the arithmetic mean along the specified axis.

    Returns the average of the array elements.  The average is taken over
    the flattened array by default, otherwise over the specified axis.
    'float64' intermediate and return values are used for integer inputs.

    Parameters
    ----------
    a : array_like
       Array containing numbers whose mean is desired. If 'a' is not an
       array, a conversion is attempted.
...

在后面的章节我们会看到,有些函数与特定对象绑定使用,用点操作符“.”连接,称之为方法。要获取方法的帮助,需要加上特定对象的属性。例如,用下面的命令可以获取列表(list)的pop方法的使用帮助:

>>> help(list.pop)
Help on method_descriptor:
pop(self, index=-1, /)
    Remove and return item at index (default last).
    Raises IndexError if list is empty or index is out of range.

Python一个最基本但又很有用的功能是进行简单的数值计算。Python有3种数值类型,即整数型、浮点型和复数型。

●整数型(int),如2、−3、999。整数没有大小限制,仅仅受限于可用内存的大小。

●浮点型(float),如4.0、2e12,后面一个是科学记数法的形式,表示2的12次方。

●复数型(complex),如3+2j、2.4+6.3j。

表1-1列出了Python中常用的算术运算符。

表1-1 算术运算符

下面是简单数值计算的示例:

>>> 1 + 1
2
>>> 2 * 3
6
>>> 4 / 2
2.0
>>> 2 ** 3
8
>>> 3.2 * 2.4
7.68
>>> (3 + 5j) + (4 - 2j) 
(7+3j)

注意,用“/”对整数做除法,得到的结果是浮点型数值。

除了运算符,用户还可以用Python中的内置函数操作数值。例如,求绝对值:

>>> abs(-2)
2

对于稍微复杂一些的数值计算,需要导入math库后使用其中的函数。表1-2列举了math库中的一部分常用函数。

表1-2 math库中的一部分常用的函数

尝试输入并运行下面的命令:

>>> import math       # 导入math包
>>> math.sqrt(4)      # 计算4的平方根
2.0
>>> math.exp(2)       # 计算e的平方
7.38905609893065
>>> math.log(10)      # 计算10的自然对数
2.302585092994046
>>> math.ceil(3.48)   # 计算不小于3.48的最小整数
4
>>> math.floor(3.48)  # 计算不大于3.48的最大整数
3

在上面的代码中,符号“#”后面的文字是注释,Python会自动忽略每行命令里面“#”后面所有的输入。对比较复杂的代码添加注释是一个很好的习惯。

在math库中还包括两个常数值,即圆周率和自然常数。

>>> math.pi
3.141592653589793
>>> math.e
2.718281828459045

如果想把圆周率四舍五入,只保留两位小数,可以使用函数round:

>>> round(math.pi, ndigits = 2)
3.14

其中,“ndigits”是函数round里的一个参数,我们可以改变它的值以得到不同的输出效果。此外,因为这个参数位于该函数所有参数的第二个位置,所以参数名在这里可以省略,Python会自动将第二个输入的值赋给该参数。例如:

>>> round(math.pi, 4)
3.1416

Python是面向对象的编程语言。在Python中,“一切皆对象”。数据分析包括很多步骤,从数据整理、探索、建模到可视化,每个步骤都需要处理不同的对象,例如列表、数据集、函数、模型等。

对于一些简单的计算,可以把计算结果直接显示在屏幕上而不存储。但更多的时候,我们需要把结果存储在某个对象中。例如:

>>> a = 3 + 5
>>> print(a)
8

符号“=”用于赋值。函数print用于打印输出,直接输入对象名等价于调用函数print作用于对象。

>>> a
8

Python区分大小写,字符“a”与“A”的意义是不同的。这点对于初学者来说是非常值得注意的。

>>> A
NameError: name 'A' is not defined

现在创建第二个对象“b”:

>>> b = -2

然后,把两个对象相加:

>>> a + b
6

对象的名字可以由一个或一个以上的字符组成。对象名可以以字母或下划线“_”开头,后面可以连接字母、数字或下划线。例如:

>>> x1 = 1
>>> x1
1
>>> hours_per_day = 24
>>> hours_per_day
24

前面定义的都是数值型对象,Python中还有字符型和逻辑型对象等。

字符型对象由字符串构成,用引号来指定,可以是单引号、双引号、三引号,但必须要配对使用。例如,人的名字、地址、邮政编码等都需要用字符串表示。字符串对象不能进行算术运算。

>>> A = "Hubei University of Medicine"
>>> A
"Hubei University of Medicine"

字符串中的反斜杠表示转义字符,即有特殊含义的字符。表1-3列出了常用的转义字符。

表1-3 常用的转义字符

Python还允许使用r" "的方式表示" "内的字符串默认不转义。例如:

>>> print("Data\nAnalysis")
Data
Analysis
>>> print(r"Data\nAnalysis")
Data\nAnalysis

逻辑型对象常常是一些关系运算或逻辑运算的结果,其取值为True或False。表1-4列出了常用的关系和逻辑运算符。

表1-4 常用的关系和逻辑运算符

输入下面几个命令并观察输出结果:

>>> 3*3 == 3**2
True
>>> 3*2 == 3**2 
False
>>> 3*2 < 3**2
True

注意,检查等价性需要用双等号,一个等号表示赋值。

“&”(逻辑与)和“|”(逻辑或)可以用于连接多个逻辑型对象,其连接结果为True或False。

>>> (3*3 == 3**2) & (3*2 == 3**2)
False
>>> (3*3 == 3**2) | (3*2 == 3**2)
True

另外,逻辑型对象的取值True和False分别对应于整数1和0,可以进行数值运算。

>>> True == 1
True
>>> False == 0
True
>>> (3*3 == 3**2) + (5 > 4)
2

除了数值、字符串、逻辑型等标准类型对象之外,Python还有一种特殊的数据类型,即空值None。在Python中只有一个None对象。None通常被用作占位符,用于指示数据结构中某个位置的数据是有意义的但尚未计算出来。 OeZI+0uF1fyF4LqS/9XeH1Kbz0UbsSPKIWCoQwz/LLE+x18aHIjkAzRiQRro27El

点击中间区域
呼出菜单
上一章
目录
下一章
×

打开