1. 内建(built-in)数据类型种类
- 数字类型:int(), float()
- 顺序(sequence):
- 字符串:str()
- 元祖:tuple()
- 列表:list()
- 字典:dict()
- 集合:set()
- 文件:open()
- 布尔:True, False
- 空对象:None
2. 各类型示例详解
2.1. 数字类型:
数字直接量示例

>>> 123 + 222 # 整数加 345 >>> 1.5 * 4 # 浮点数乘 6.0 >>> 2 ** 100 # 2的100次幂 1267650600228229401496703205376 >>> str(2**100) # 转换为字符串 >>> 3.1415 * 2 # (Python < 2.7 和 3.1),调用类的__repr__函数 6.28300000000004 >>> print(3.1415 * 2) # 调用了类的__str__函数 6.283 >>> import math # 引入外部模块, math数学模块 >>> math.pi 3.141592653589793 >>> math.sqrt(85) 9.219544457292887 >>> import random # 引入随机数模块 >>> random.random() # 生产随机数, 小于1的正数 0.6489341324245831 >>> random.randint(1, 10) # 生产1~10之间的整数,含1,10 5 >>> random.choice([1,3,4,8]) # 1,3,4,8中随机选择一个数 4
第三方开源数字类型有:矩阵、向量、扩展精度数等。
字符串转换为数字示例

>>> help(int) class int(object) int(x=0) -> integer int(x, base=10) --> integer (默认转换为10进制整数) >>> int("12a") Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: '12a' >>> int("a12") Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: 'a12' >>> int("abc") Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: 'abc' 以上示例说明,含有非数字的字符串无法调用int()函数进行转换,使用过程中需要进行异常捕捉。 >>> int('12') 12 >>> int('12', 2) # 字符串中的2在二进制模式内是越界的 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 2: '12' >>> int('1010', 2) 10 >>> int('0777') 777 >>> int('0777', 8) 511 >>> int('0x1212') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: '0x1212' >>> int('0x1212', 16) 4626 >>> int('0xFF', 16) 255 >>> int('0xFG', 16) # 字符G在十六进制中越界 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: '0xFG' >>> help(float) class float(object) float(x) -> floating point number # 只接收一个参数,只能包含点和数字 >>> float('0.1x') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: could not covert string to float: '0.1x') >>> float('0.1') 0.1 >>> float('.1') 0.1 >>> float('0.1.2') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: could not covert string to float: '0.1.2')
2.2. 顺序(sequence):
”顺序“顾名思义,有先后、有序,其中包含的类型有:字符串、列表、元祖,Python中没有数组的提法,在Python里面需要忘记数组,如果难以做到,记得元祖和列表都可以认为是数组的一种,元祖和列表间又存在区别,稍后会提到这一点。sequence有以下几方面的特点:
- 可以切片获取新的sequence
- 可以下标访问
- 可len()函数获取长度
2.2.1. 字符串:
特点:
- 直接量定义:mystr = "this is my string"
- 工厂函数定义:mystr = str("this is my string")
- 可切片:mystr[0:3] (不含3), mystr[3:-2] (不含-2), 索引0, -1可以省略,mystr[0:3] === mystr[:3], mystr[3:-1] === mystr[3:], mystr[0:-1] (取全部值) === mystr[:]
- 可下标访问:mystr[0], mystr[1], mystr[-1], mystr[-2]
- 不可变值(immutable):mystr[0] = 'a' 会抛出TypeError错误
2.2.2. 列表:
特点:
- 直接量定义:mylist = [1, 3, 5, "this is a string"]
- 工厂函数定义:mylist = list([1, 3, 5, "this is a string"]); mylist = list( (1, 3, 5, "this is a string") ); mylist = list("this is a list")
- 可切片:mylist[0:3] (不含3), mylist[3:-2] (不含-2), 索引0, -1可以省略,mylist[0:3] === mylist[:3], mylist[3:-1] === mylist[3:], mylist[0:-1] (取全部值) === mylist[:]
- 可下标访问:mylist[0], mylist[1], mylist[-1], mylist[-2]
- 可变值(mutable): mylist[1] = 'newstring'
2.2.3. 元祖:
特点:
- 直接量定义:mytuple = (1,3,5, "this is a string"); mytuple = 1, 3, 5, "this is a string";
- 工厂函数定义:mytuple = tuple([1,3,5, "this is a string"]); mytuple = tuple( (1,3,5, "this is a string") ); mytuple = tuple("this is a tuple")
- 可切片:mytuple[0:3] (不含3), mytuple[3:-2] (不含-2), 索引0, -1可以省略,mytuple[0:3] === mytuple[:3], mytuple[3:-1] === mytuple[3:], mytuple[0:-1] (取全部值) === mytuple[:]
- 不可变值(immutable): mytuple[0] = "new one" # 抛出TypeError错误
2.2.4. 示例
字符串示例

>>> S = 'Spam' >>> len(S) 4 下标访问 >>> S[0] 'S' >>> S[1] 'p' >>> S[100] # 越界抛出 IndexError 错误 Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range >>> S[-1] 'm' >>> S[len(S) - 1] 'm' >>> S[-2] 'a' 切片操作 >>> S[1:3] 'pa' >>> S[1:] 'pam' >>> S 'Spam' >>> S[0:3] # 切片不包含最后的值 'Spa' >>> S[:3] # 开头的0可以省略 "Spa' >>> S[:-1] 'Spa' >>> S[:] # 末尾的-1也可以省略 'Spam' >>> S[::2] # 第三个值为步长 'Sa' 字符串拼接 >>> S + 'xyz' 'Spamxyz' >>> S * 8 # 重复8次 'SpamSpamSpamSpamSpamSpamSpamSpam' 不可修改值 >>> S[0] = 'z' # 抛出 TypeError 错误 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment
字符串相关函数介绍示例:

>>> my_string = "this is a string" >>> my_string.capitalize() # 首字母大写 This is a string >>> my_string.center(50, "=") # 中对齐,总长度50,填充= =======this is a string======= >>> my_string.ljust(50, "=") # 左对齐,总长度50,填充= this is a string >>> my_string.rjust(50, "=") # 右对齐,总长度50,填充= ==============this is a string # 默认my_string为UTF-8, UTF-8是UNICODE的子集 # str--------> unicode(utf-8) ---------------->str # "".decode("GBK") --> unicode(utf-8) --> u"".encode("GBK") >>> my_string.encode("GBK") b'this is a string' # 因为是因为字符,所以显示出了字节类型的具体值 >>> my_string = "中文" # 因为"中文"两个字是UTF-8编码的,所以只能执行encode方法 >>> my_string.encode("GBK") b'xd6xd0xcexc4' # 只显示出了二进制值 >>> my_string = "this is a string" >>> my_string.endswith('ing') True >>> my_string.endswith('abc') False >>> my_string.startswith('this') True >>> my_string = "this is a string" >>> my_string.expandtabs(tabsize=5) # 替换为5个空格 this is a string >>> my_string.find('i') # 从左往右,查找第一个i的索引值,找不到返回值为-1 2 >>> my_string.rfind('i') # 从右往左,查找第一个i的索引值,找不到返回值为-1 13 >>> "My name is {}, age is {}, sex is {}".format('Tim', 28, 'man') "My name is Tim, age is 28, sex is man # format_map 参数为字典(后面会介绍字典) >>> "My name is {name}, age is {age}, sex is {sex}".format_map({'name': 'Tim', 'age': 28, 'sex': 'man'}) "My name is Tim, age is 28, sex is man >>> my_string.index(" is") # 获取index值,找不到抛出ValueError异常 4 >>> my_string.rindex(" is") # 从右往左获取index值,找不到抛出ValueError异常 4 >>> my_string.isalnum() # 只包含字母,数字,因为有空格所以False False >>> my_string.isdecimal() # 只包含数字时为True False >>> my_string.isalpha() # 只包含字母时为True False >>> my_srting.islower() # 是纯小写 True >>> my_srting.isupper() # 是纯大写 False >>> ','.join(my_string) # 每个迭代项目之间插入',',返回字符串 t,h,i,s, ,i,s, ,a, ,s,t,r,i,n,g >>> my_string.replace('i', 'x', 2) # i->x, 最大替换2次 thxs xs a string >>> my_string.partition('is') # 返回三个元素的元祖:head, sep, tail ('th', 'is', ' is a string') >>> my_string.partition('is') # 找不到sep时 ('this is a string', '', '') >>> my_string.partition('is') # 返回三个元素的元祖:head, sep, tail ('th', 'is', ' is a string') >>> my_string.rpartition('is') # 优先从右往左查找 ('this ', 'is', ' a string') >>> my_string.split('is') # 使用'is'对字符串进行切割,得到列表,切割后的内容中不在包含有'is’ ['th', ' ', ' a string'] >>> my_string.split('is', 1) # 切一次 ['th', ' is a string'] >>> my_string.split('xis') # 不存在 ['this is a string'] >>> my_string.rsplit('is', 1) # 从右往左开始切 ['this ', ' a string'] >>> "tzt this is a string ttt".strip('tz') # 开头、结尾含有't', 'z'的字符 ' this is a string ' >>> my_string.swapcase() # 交换大小写 THIS IS A STRING >>> my_string.zfill(20) # 左侧填0 0000this is a string