1.5 Python的数据类型
Python主要有六种数据类型,分别为数字(number)、字符串(string)、列表(list)、元组(tuple)、集合(sets)和字典(dictionary)。其中,数字类型又分为整型(int)、浮点型(float)、布尔型(boolean)和复数类型(complex)四种数据类型。Python是一种弱类型语言,所以变量都不需要提前声明,可以直接拿来使用。
1.5.1 整型
在Python内部对整型数字的处理分为普通整数和长整数,普通整数长度为机器位长,超过这个范围的整数就自动当成长整数处理。在32位机器上,整数的位数为32位,取值范围为-231~231-1,即-2 147 483 648~2 147 483 647;在64位机器上,整数的位数为64位,取值范围为-263~263-1,即-9 223 372036 854 775 808~9 223 372036 854 775 807。Python可以处理任意大小的整数,当然包括负整数,在程序中的表示方法和数学上的写法一模一样,如5、23、-10等。
【例1-1】整型数字示例。

1.5.2 浮点型
Python中的浮点型数字就是数学中的小数。在运算中,整数与浮点数运算的结果是浮点数。之所以称为浮点数,是因为按照科学记数法表示时,一个浮点数的小数点位置是可变的,如1.23e9和12.3e8是相等的。浮点数可以用数学写法,如4.56、2.34、-8.21。但是对于很大或很小的浮点数,就必须用科学计数法表示,把10用e代替,如将0.000023写成2.3e-5。整数和浮点数在计算机内部的存储方式是不同的,整数运算永远是精确的,而浮点数运算则可能会有四舍五入的误差。变量在定义赋值时,只要赋值为小数,该变量就被定义成浮点型。
【例1-2】浮点型数字示例。

1.5.3 字符串类型
字符串是由数字、字母、下划线组成的一串字符。所有的字符串都是直接按照字面的意思来使用,没有转义特殊或不能打印的字符。原始字符串除在字符串的第一个引号前加上字母“r”(不区分大小写)以外,与普通字符串有着几乎完全相同的语法。
【例1-3】普通字符串示例。

原始字符串示例。

Python接受单引号(')、双引号(")、三引号('''或""")来表示字符串,引号的开始与结束类型必须一致,也就是说前面是单引号,后面也必须是单引号。其中三引号可以由多行组成,这也是编写多行文本的常用语法,经常用于处理文档字符串,但在文件的特定地点,会被当成注释来处理。
【例1-4】用单引号括起来表示字符串,示例如下。

双引号中的字符串与单引号中的字符串用法完全相同,示例如下。

利用三引号表示多行字符串,可以在三引号中自由地使用单引号和双引号,示例如下。

1.5.4 列表类型
列表是任意对象的集合,所有元素都放在方括号“[]”中,元素之间使用逗号分隔,元素可以是单独的,也可以是嵌套关系。列表是一种有序的非泛型集合,内部可以加入类型不同的数据,并且使用数组下标作为索引。列表是可以修改的,对于需要不断更新的数据来说很适用。
【例1-5】列表示例。

为了更加方便地操作列表,可以使用列表函数实现列表的各种操作。
常用的列表函数示例如下。
1.修改
【例1-6】声明一个列表list,通过下标对列表list中的元素进行操作,实现对列表的内容修改。这里实现对list中元素下标为2的值进行修改。

2.append(e)
【例1-7】该函数接收一个元素作为参数,作用是向列表list中的最后一位添加指定元素。


3.insert(index,e)
【例1-8】该函数接收两个参数,作用是向列表list中指定的元素下标插入元素。其中,index参数表示当前插入的位置,e参数表示需要插入的元素,在插入位置后面的元素依次往后移动一位。

4.remove(e)
【例1-9】该函数接收一个元素作为参数,作用是移除列表中某个值的第一个匹配项。如果有多个相同的元素,则只删除第一个。

5.reverse()
【例1-10】该函数的作用是反向列表中的元素。

6.sort()
【例1-11】该函数的作用是对原列表进行排序,默认是升序。

7.index(e)
【例1-12】该函数接收一个元素作为参数,作用是从列表中找出某个值第一个匹配项的索引位置,索引编号从0开始。

8.count(e)
【例1-13】该函数接收一个元素作为参数,作用是统计某个元素在列表中出现的次数。

9.pop()
【例1-14】该函数的作用是移除列表中的一个元素(默认是最后一个元素),并且返回该元素的值。

1.5.5 集合类型
集合类型有三个特点:无序;不重复;使用花括号表示。可以使用花括号“{ }”或者set()函数创建集合。

常用的集合函数示例如下。
1.remove(e)
【例1-15】该函数接收一个元素作为参数,作用是删除集合中指定的元素。

2.pop()
【例1-16】该函数的作用是随机移除一个元素。

3.len()
【例1-17】该函数的作用是获得集合中元素的个数。

4.clear()
【例1-18】该函数的作用是清除集合中的所有元素。

5.add(e)
【例1-19】该函数接收一个元素作为参数,作用是向集合中添加元素。

6.union(e)
【例1-20】该函数接收一个集合作为参数,作用是合并两个集合。

1.5.6 字典类型
字典是一种无序存储结构,包括关键字(key)和关键字对应的值(value)。字典的格式为:dictionary={key:value}。通过关键字可以获得对应的值。
【例1-21】字典示例。

常用的字典函数示例如下。
1.len(d)
【例1-22】该函数接收一个字典作为参数,作用是计算字典元素个数,即键的总数。

2.clear()
【例1-23】该函数的作用是删除字典内所有元素。

3.copy()
【例1-24】该函数的作用是返回一个字典的复制。

4.get(key,default=None)
【例1-25】该函数接收两个参数——key和默认返回值(当key不存在时),作用是返回指定键的值,如果该键不在字典中,则返回默认返回值None。

5.keys()
【例1-26】该函数的作用是以列表形式返回字典中的所有键。

6.values()
【例1-27】该函数的作用是以列表形式返回字典中的所有值。


1.5.7 元组类型
元组具有和列表相似的数据结构,但它一旦初始化就不能更改,速度比列表快,同时元组不提供动态内存管理的功能,元组可以用下标返回一个元素或子元组。元组和列表有两个区别:元组不能修改里面的元素;元组使用圆括号“()”表示。同样,元组也使用数组下标进行操作。由于元组不可更改,因此可以存放适用于程序生命周期内的数据。
【例1-28】元组示例。

常用的元组函数示例如下。
1.len(t)
【例1-29】该函数接收一个元组作为参数,作用是计算元组元素个数。

2.max(t)
【例1-30】该函数接收一个元组作为参数,作用是返回元组中元素的最大值。

3.min(t)
【例1-31】该函数接收一个元组作为参数,作用是返回元组中元素的最小值。

4.tuple(list)
【例1-32】该函数接收一个列表作为参数,然后将该列表转换为元组。
