- int(x) 将x转换为一个整数。
- float(x) 将x转换到一个浮点数。
- complex(x) 将x转换到一个复数,实数部分为 x,虚数部分为 0。
- complex(x, y) 将 x 和 y 转换到一个复数,实数部分为 x,虚数部分为 y。x 和 y 是数字表达式。
1 haauleon = "我有一只小猫咪,我从来也不骑。" 2 Haauleon = "床前明月光,疑是地上霜。" 3 4 #拼接字符串 5 print(haauleon + Haauleon) 6 7 #重复输出 8 Haauleon *= 3 9 print(Haauleon) 10 11 #索引:'猫' 12 print(haauleon[5]) 13 print(haauleon[-10]) 14 15 #截取字符串 16 Virgos = "Virgos are naturally meticulous planners." 17 print(Virgos[:]) 18 print(Virgos[:-21]) 19 print(Virgos[21:]) 20 21 #成员运算符 22 if ("are" in Virgos): 23 print("'are' in Virgos") 24 else: 25 print("'are' not in Virgos") 26 27 if ("girl" not in Virgos): 28 print("'girl' not in Virgos") 29 else: 30 print("'girl' in Virgos") 31 32 #原始字符串 33 virgos = r"'Virgos' are very choosy about their lover.\n\n\n" 34 print(virgos)
运行结果:
我有一只小猫咪,我从来也不骑。床前明月光,疑是地上霜。
床前明月光,疑是地上霜。床前明月光,疑是地上霜。床前明月光,疑是地上霜。
猫
猫
Virgos are naturally meticulous planners.
Virgos are naturally
meticulous planners.
'are' in Virgos
'girl' not in Virgos
'Virgos' are very choosy about their lover.\n\n\n
三、列表
序列是Python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。Python有6个序列的内置类型,但最常见的是列表和元组。序列都可以进行的操作包括索引,切片,加,乘,检查成员。Python已经内置确定序列的长度以及确定最大和最小的元素的方法。
列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现。
列表的数据项不需要具有相同的类型。创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可。
1 haauleon = ["靓仔",22,"Boy"]
1.访问列表中的值
使用下标索引来访问列表中的值,也可以使用方括号的形式截取字符。
1 haauleon = ["Boy",22,"靓仔","Virgos",5201314] 2 3 #遍历整个列表的错误写法 4 #print("haauleon[]:",haauleon[]) /*SyntaxError: invalid syntax*/ 5 6 #遍历整个列表的正确写法 7 print("haauleon:",haauleon) 8 print("haauleon[:]:",haauleon[:]) 9 10 #列表的索引、切片 11 print("haauleon[2]:",haauleon[2]) 12 print("haauleon[:2]:",haauleon[:2]) 13 print("haauleon[-3:-1]:",haauleon[-3:-1])
运行结果:
haauleon: ['Boy', 22, '靓仔', 'Virgos', 5201314]
haauleon[:]: ['Boy', 22, '靓仔', 'Virgos', 5201314]
haauleon[2]: 靓仔
haauleon[:2]: ['Boy', 22]
haauleon[-3:-1]: ['靓仔', 'Virgos']
2.更新列表
可以对列表的数据项进行修改或更新,也可以使用append()方法来添加列表项。
1 haauleon = ["Boy",22,"靓仔","Virgos"] 2 3 print("第三个元素:",haauleon[2]) 4 5 #修改列表元素 6 haauleon[2] = "怪蜀黍" 7 print("更新后的第三个元素:",haauleon[2]) 8 9 #添加列表项 10 haauleon.append(5201314) 11 print("添加列表元素后的新列表:",haauleon)
运行结果:
第三个元素: 靓仔
更新后的第三个元素: 怪蜀黍
添加列表元素后的新列表: ['Boy', 22, '怪蜀黍', 'Virgos', 5201314]
3.删除列表元素
可以使用 del 语句来删除列表的的元素。
1 haauleon = ["Boy",22,"靓仔","Virgos",5201314] 2 3 #删除列表元素1 4 print("第三个元素:",haauleon[2]) 5 del haauleon[2] 6 print("更新后的第三个元素:",haauleon[2]) 7 8 9 print("\n") 10 11 12 haauleon = ["Boy",22,"靓仔","Virgos",5201314] 13 14 #删除列表元素2 15 print("第五个元素:",haauleon[4]) 16 del haauleon[4] 17 print("更新后的第五个元素:",haauleon[4])
运行结果:
第三个元素: 靓仔
更新后的第三个元素: Virgos
第五个元素: 5201314
Traceback (most recent call last):
File "C:/Users/bringbuys/AppData/Local/Programs/Python/Python36-32/practice/mul.py", line 19, in <module>
print("更新后的第五个元素:",haauleon[4])
IndexError: list index out of range
4.列表脚本操作符
列表对 + 和 * 的操作符与字符串相似。+ 号用于组合列表,* 号用于重复列表。
1 haauleon = ["Boy",22,"靓仔","Virgos",5201314] 2 3 #列表元素个数 4 print(len(haauleon)) 5 6 #列表拼接 7 Haauleon = ["haauleonBB","近排颠佐","可能系。。。"] 8 9 All = haauleon + Haauleon 10 print(All) 11 12 #重复列表 13 print(All * 2) 14 15 #成员判断 16 if (22 in haauleon): 17 print("22 in haauleon") 18 else: 19 print("22 not in haauleon")
运行结果:
5
['Boy', 22, '靓仔', 'Virgos', 5201314, 'haauleonBB', '近排颠佐', '可能系。。。']
['Boy', 22, '靓仔', 'Virgos', 5201314, 'haauleonBB', '近排颠佐', '可能系。。。', 'Boy', 22, '靓仔', 'Virgos', 5201314, 'haauleonBB', '近排颠佐', '可能系。。。']
22 in haauleon
5.嵌套列表
使用嵌套列表即在列表里创建其它列表。
1 haauleon = ["Boy", 2 22, 3 "靓仔", 4 "Virgos"] 5 6 Haauleon = ["haauleonBB", 7 "近排颠佐"] 8 9 Virgos = [123, 10 456, 11 789] 12 13 #嵌套列表 14 All = [haauleon,Haauleon] 15 print(All) 16 print(All[1]) 17 #索引元素:"近排颠佐" 18 print(All[1][1]) 19 20 print("\n") 21 22 #三维列表 23 All = [haauleon,Haauleon,Virgos] 24 print(All) 25 print(All[2]) 26 #索引元素:789 27 print(All[2][2])
运行结果:
[['Boy', 22, '靓仔', 'Virgos'], ['haauleonBB', '近排颠佐']]
['haauleonBB', '近排颠佐']
近排颠佐
[['Boy', 22, '靓仔', 'Virgos'], ['haauleonBB', '近排颠佐'], [123, 456, 789]]
[123, 456, 789]
789
四、元组
1.Python 的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号( ),列表使用方括号[ ]。元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。
1 #书写方式1:有括号 2 haauleon = ("Boy",22,"靓仔","Virgos") 3 print(type(haauleon)) 4 5 #书写方式2:无括号 6 haauleon = "Boy",22,"靓仔","Virgos" 7 print(type(haauleon)) 8 9 #创建空元组 10 Haauleon= () 11 12 #元组中只包含一个元素时,需要在元素后面添加逗号,否则括号会被当作运算符使用 13 Haauleon = (999) 14 print(type(Haauleon)) 15 16 Haauleon = 999 17 print(type(Haauleon)) 18 19 Haauleon = (999,) 20 print(type(Haauleon))
运行结果:
<class 'tuple'>
<class 'tuple'>
<class 'int'>
<class 'int'>
<class 'tuple'>
2.访问元组
元组与字符串类似,下标索引从0开始,可以进行截取,组合等。元组可以使用下标索引来访问元组中的值。
1 haauleon = ("Boy",22,"靓仔","Virgos") 2 3 #索引 4 print(haauleon[2]) 5 6 #截取 7 print(haauleon) 8 print(haauleon[:]) 9 print(haauleon[1:]) 10 print(haauleon[:-2])
运行结果:
靓仔
('Boy', 22, '靓仔', 'Virgos')
('Boy', 22, '靓仔', 'Virgos')
(22, '靓仔', 'Virgos')
('Boy', 22)
3.修改元组
元组中的元素值是不允许修改的,但可以对元组进行连接组合。
1 haauleon = ("Boy",22,"靓仔","Virgos") 2 3 #元组拼接 4 #元组中只包含一个元素时,需要在元素后面添加逗号,否则会抛出异常 5 Haauleon = ("风华正茂",) 6 All = haauleon + Haauleon 7 print(All) 8 9 #修改元组的元素 10 haauleon[2] = "怪蜀黍" 11 print(haauleon)
运行结果:
('Boy', 22, '靓仔', 'Virgos', '风华正茂')
Traceback (most recent call last):
File "C:/Users/bringbuys/AppData/Local/Programs/Python/Python36-32/practice/mul.py", line 13, in <module>
haauleon[2] = "怪蜀黍"
TypeError: 'tuple' object does not support item assignment
4.删除元组
元组中的元素值是不允许删除的,但可以使用del语句来删除整个元组。
1 haauleon = ("Boy",22,"靓仔","Virgos") 2 3 print(haauleon) 4 5 #删除元组的一个元素 6 del haauleon[1] 7 print(haauleon)
运行结果:
('Boy', 22, '靓仔', 'Virgos')
Traceback (most recent call last):
File "C:/Users/bringbuys/AppData/Local/Programs/Python/Python36-32/practice/mul.py", line 9, in <module>
del haauleon[1]
TypeError: 'tuple' object doesn't support item deletion
1 haauleon = ("Boy",22,"靓仔","Virgos") 2 3 print(haauleon) 4 5 #删除整个元组 6 del haauleon 7 print(haauleon)
运行结果:
('Boy', 22, '靓仔', 'Virgos')
Traceback (most recent call last):
File "C:/Users/bringbuys/AppData/Local/Programs/Python/Python36-32/practice/mul.py", line 10, in <module>
print(haauleon)
NameError: name 'haauleon' is not defined
5.元组运算符
1 haauleon = ("Boy",22,"靓仔","Virgos") 2 3 #计算元组的元素个数 4 print(len(haauleon)) 5 6 #元组拼接 7 Haauleon = ("怪蜀黍",) 8 print(haauleon + Haauleon) 9 10 #复制元组 11 Haauleon *= 2 12 print(Haauleon) 13 14 #成员判断 15 if ("怪蜀黍" not in haauleon): 16 print("'怪蜀黍' not in haauleon\n") 17 else: 18 print("'怪蜀黍' in haauleon\n") 19 20 #迭代 21 for i in haauleon: 22 print(i,) 23 24 #将元组换成列表 25 print(list(haauleon))
运行结果:
4
('Boy', 22, '靓仔', 'Virgos', '怪蜀黍')
('怪蜀黍', '怪蜀黍')
'怪蜀黍' not in haauleon
Boy
22
靓仔
Virgos
['Boy', 22, '靓仔', 'Virgos']
五、字典
1.字典是另一种可变容器模型,且可存储任意类型对象。
1 #字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 2 haauleon = {"sex":"Boy", 3 "old":22, 4 "description":"靓仔", 5 "constellation":"Virgos"} 6 print(haauleon) 7 8 #键必须是唯一的,但值则不必,键相同导致值缺失 9 haauleon = {"sex":"Boy", 10 "sex":22, 11 "description":"靓仔", 12 "constellation":"Virgos"} 13 print(haauleon) 14 15 #创建单个键值对的字典 16 Haauleon = {"hight":186} 17 print(Haauleon)
运行结果:
{'sex': 'Boy', 'old': 22, 'description': '靓仔', 'constellation': 'Virgos'}
{'sex': 22, 'description': '靓仔', 'constellation': 'Virgos'}
{'hight': 186}
2.索引
1 haauleon = {"sex":"Boy", 2 "old":22, 3 "description":"靓仔", 4 "constellation":"Virgos"} 5 6 #把相应的键放入方括弧,通过键来索引对应的值 7 print(haauleon["description"]) 8 9 #用字典里不存在的键来访问数据,则会抛出异常 10 print(haauleon["sssssss"])
运行结果:
靓仔
Traceback (most recent call last):
File "C:/Users/bringbuys/AppData/Local/Programs/Python/Python36-32/practice/mul.py", line 13, in <module>
print(haauleon["sssssss"])
KeyError: 'sssssss'
3.修改字典
1 haauleon = {"sex":"Boy", 2 "old":22, 3 "description":"靓仔", 4 "constellation":"Virgos"} 5 print(haauleon) 6 7 #增加新的键值对 8 haauleon["hight"] = 186 9 print(haauleon) 10 11 #更新数据 12 haauleon["sex"] = "Gigl" 13 print(haauleon)
运行结果:
{'sex': 'Boy', 'old': 22, 'description': '靓仔', 'constellation': 'Virgos'}
{'sex': 'Boy', 'old': 22, 'description': '靓仔', 'constellation': 'Virgos', 'hight': 186}
{'sex': 'Gigl', 'old': 22, 'description': '靓仔', 'constellation': 'Virgos', 'hight': 186}
4.删除字典元素
1 haauleon = {"sex":"Boy", 2 "old":22, 3 "description":"靓仔", 4 "constellation":"Virgos"} 5 print(haauleon) 6 7 #删除单个键值对,只需删除键即可 8 del haauleon["description"] 9 print(haauleon) 10 11 #清空整个字典,即只是清空字典的数据并未删除整个字典,其为其分配内存空间 12 haauleon.clear() 13 print(haauleon) 14 print(id(haauleon)) 15 16 #删除整个字典 17 del hauleon 18 print(haauleon)
运行结果:
{'sex': 'Boy', 'old': 22, 'description': '靓仔', 'constellation': 'Virgos'}
{'sex': 'Boy', 'old': 22, 'constellation': 'Virgos'}
{}
90722048
Traceback (most recent call last):
File "C:/Users/bringbuys/AppData/Local/Programs/Python/Python36-32/practice/mul.py", line 20, in <module>
del hauleon
NameError: name 'hauleon' is not defined