0003-20180422-自动化第三章-python基础学习笔记
3章
内容回顾:
1. 计算机组成
2. 程序编译器
3. 变量
4. 条件
5. 循环
6. py2与py3区别
- 默认编码,
- 除法,
- input ,raw_input
7. 位,字节关系
8. 运算符:
- and or
- in not
- py2 除法特殊代码
- += -=
9. 编码
汉字在utf-8里位3个字节,24位;汉字在gbk里是两个字节,16位
ascii --> unicode(4个字节,32位表示) ->(优化) utf-8/utf-16/utf-32
10. 字符串格式化
tpl = "asdfa&s,asdf%d" %("二哥", 666,)
问题:
tpl = "我是%s",年龄%d,学习进度100%%" %('张勤学',99)
今日内容:
- 数据类型
- 整数(int类) v = 1 ----擅长计算
- v = 1
- v.bit_length()
- 布尔值(bool类) v = true -----擅长真或假
- 0 "" 为false,其他为true
- v = 2 or 1 ----v的值是2
- 字符串(str类) 引号引起来 "1" v = "1"
- 类提供的功能:
- 变大、小写
- 去除空白
- 替换
- 分割(分割后是列表类型)
- 首字母大写
ps:字符串本身不变化
- 公共功能:
- 长度
- 索引(只能取值)
- for循环
"张勤学,二个,成绩,xxx,信息,"
[“郑勤学,“二个”]
- 列表(list类) v = ["xx",“oo”, "666"] #---存取多个值比较方便
- 类提供功能:
- 追加
- 插入
ps:列表本身发生变化
- 公共功能:
- 长度
- 索引(取值,修改,删除) 特殊的:v[0:2] = ["asdf",]
- for循环
v[2]
v[1] = "孙骑兵"
- 元组(tuple类) v = ("xx",“oo”, "666") #ps:----元素不可以被修改
- 类提供功能:
- 无
- 公共功能:
- 长度
- for循环
- 索引(只能取值)
- 字典(dict类)-----无序的 v = {"name":"张勤学","age":18}
- 类提供的功能:
- get
- 公共功能:
- 长度
- 索引(取值,删除,修改)
- for循环
- for a in v.keys()
- for a in v.values()
- for a,b in v.items()
v = "郑勤学,18" #-----字符串表示姓名年龄
v = ["郑勤学",18] #-----列表表示姓名年龄
v = {
"name":"张勤学",
"age":18
} #-----字典表示姓名年龄
v['name'] #指名字
v['age'] #指年龄
===> 相同类的值(指定类的对象)具有相同的功能,功能保存在类中(只有一份)
今日作业:
内容整理
=============================================脚本=======================================
字符串类中的功能脚本:
1 ###############################str类中为当前对象提供的功能######################### 2 #str 3 # v1 = "alex" 4 5 #1. 变大写 6 # value = v1.upper() 7 # print(v1,value) 8 9 10 #2,.变小写 11 # value = v1.lower() 12 # print(v1.value) 13 14 15 #3. 首字母变大写 16 # value = v1.capitalize() 17 # print(v1,value) 18 19 #4. 去除收尾空格 20 # v1 = " asdfasdf " 21 # print(v1) 22 23 24 # v1 = " asdfasdf " 25 # value = v1.strip() 26 # print(v1,value) 27 28 """ 29 name = input("请输入用户名:") 30 if name == 'alex': 31 print('登录成功') 32 """ 33 34 """ 35 name = input("请输入用户名:") 36 value = name.strip() 37 if value == 'alex': 38 print('登录成功') 39 """ 40 41 """ 42 #去除左边空格 -----去除指定空格-----左边或右边 43 44 name = input("请输入用户名:") 45 value = name.lstrip() 46 if value == 'alex': 47 print('登录成功') 48 49 """ 50 51 """ 52 去除右边空格 53 name = input("请输入用户名:") 54 value = name.rstrip() 55 if value == 'alex': 56 print('登录成功') 57 """ 58 59 60 #7. 替换 61 """ 62 v1 = "ascii第一次以规范标准的型态发表是在1967年标准,最后一次更新则标准是在1986年,至今为标准止共定义了128个字符,其中33个字符无法显示" 63 64 # value = v1.replace("标准", "**") 65 # print(value) 66 67 68 #只替换第1个标准,后面的不替换 69 value = v1.replace("标准", "**",1) 70 data = value.replace('第一次','#') 71 print(data) 72 """ 73 74 #8. 分割: 75 """ 76 v1 = "ascii第一次以规范标准的型态发" 77 value = v1.split('第一次') #分割之后是列表类型 78 print(value) 79 """ 80 """ 81 v1 = "ascii第一次以规范标准的型态发以规范标准的型态以规范标准的型态" 82 value = v1.split('标准') #分割之后是列表类型 83 print(value) 84 """ 85 86 """ 87 v1 = "ascii第一次以规范标准的型态发以规范标准的型态以规范标准的型态" 88 value = v1.split('标准',2) #分割之后是列表类型 89 print(value) 90 """ 91 92 #9. 判断当前字符串中是否全部都为数字 93 """ 94 v = "abc123fsdf" 95 for item in v: 96 print(item) #item = "a", item = "b" 97 """ 98 99 """ 100 v = "123" 101 val = v.isdecimal() 102 print(val) 103 """ 104 105 """ 106 v = "a123" 107 val = v.isdecimal() 108 print(val) 109 """ 110 111 #统计字符串中有多少个数字?-------"abc01234fsdf" 112 """ 113 count = 0 114 v = "abc01234fsdf" 115 for item in v: 116 if item.isdecimal(): 117 print(item) 118 count += 1 119 print(count) 120 """ 121 122 #重点重点重点:原值不变 123 124 ###################公共功能############### 125 126 # 1.计算当前字符串长度 127 128 """ 129 v = "徐芳名sdf" 130 value = len(v) 131 print(value) 132 133 """ 134 135 """ 136 name = input(">>>") 137 value = len(name) 138 print(value) 139 """ 140 141 #取首字符或第几个字符 142 #2. 根据“索引” “下标” 143 # - 单个值:v[0] 144 # - 多个值:v[起始位置:结束位置] 145 # - 多个值: v[起始位置:结束位置:步长] 146 # ps: -1 表示最后 147 148 # 取第一个字符-----取单个字符 149 # v = "ascii第一次以规范标准的型态发" 150 # value = v [0] 151 # print(value) 152 153 #取倒数第3个字符 154 # v = "ascii第一次以规范标准的型态发" 155 # value = v [-3] 156 # print(value) 157 158 159 #动态计算取最后一个字符 160 # v = "ascii第一次以规范标准的型态发" 161 # le = len(v) - 1 162 #-1表示最后一个字符 163 # value = v [le] 164 # print(value) 165 166 #取一部分字符,取从0到第几个之前的字符------连续字符 167 # v = "ascii第一次以规范标准的型态发" 168 # value = v [0:5] # 0 1 2 3 4 169 # print(value) 170 171 #取某两个之间的字符 172 # v = "ascii第一次以规范标准的型态发" 173 # value = v [4:5] 174 # print(value) 175 176 #取从第几个开始到结尾的字符 177 # v = "ascii第一次以规范标准的型态发" 178 # value = v [4:] 179 # print(value) 180 181 # #取从第几个开始到最后一个之间的字符 182 # v = "ascii第一次以规范标准的型态发" 183 # value = v [4:-1] 184 # print(value) 185 186 187 #取从第几个开始到第几个之间的字符,中括号中第3个数字表示步长,表示每隔几取一个 188 # v = "ascii第一次以规范标准的型态发" 189 # value = v [1:15:3] 190 # print(value) 191 192 193 #题 : v = "ascii第一次以规范标准的型态发" 连续将每个字符大印出来 194 """ 195 v = "ascii第一次以规范标准的型态发" 196 length = len(v) 197 index = 0 198 while true: 199 data = v[index] 200 print(data) 201 index += 1 202 if index == length: 203 break 204 print(data) 205 """ 206 207 """ 208 v = "ascii第一次以规范标准的型态发" 209 length = len(v) 210 index = 0 211 while index < length: 212 data = v[index] 213 print(data) 214 index += 1 215 """ 216 217 #3. 循环 218 # 将对象中每个元素,分别循环显示出来 219 """ 220 v = "ascii第一次以规范标准的型态发" 221 for item in v: 222 print(item) 223 """
上节回顾字符串格式化脚本:
1 # tpl = "我是%s,年龄%d,学习进度100%" %('张勤学',99) 2 # print(tpl) 3 4 5 #占位符只有格式化时才有意义 6 # tpl = "我是%s,年龄%d,学习进度100%" 7 # print(tpl) 8 9 10 # tpl = "我是%s,年龄%d,学习进度100" %('张勤学',99) 11 # print(tpl) 12 13 14 #如果想要%,需要写两个% 15 tpl = "我是%s,年龄%d,学习进度100%%" %('张勤学',99) 16 print(tpl)
int整数类脚本:
1 #int 2 # v1 = 1 3 # v2 = 23 4 5 6 7 #前戏 8 # 十进制 0 1 2...9 9 #八进制 0 1 2 ..7 10 10 #二进制 0 1 10 11 100 11 12 # v = 11 在计算机里是怎么存的? 13 # ascii 00110001 00110001 14 15 # v1 = 11 16 # data = v1.bit_length() 17 # print(data) 18 #本质上的内部存储:00110001 00110001 19 # 11 -> 20 21 #二进制 八进制 十进制 十六进制 22 # 0 0 0 0 23 # 1 1 1 1 24 # 10 2 2 2 25 # 11 3 3 3 26 # 100 4 4 4 27 # 101 6 6 6 28 # 111 7 7 7 29 # 1000 10 7 8 30 # 1001 11 9 9 31 # 1010 12 10 a 32 # 1011 13 11 b 33 # 1100 14 12 c 34 # 1101 15 13 d 35 # 1110 16 14 e 36 # 1111 17 15 f 37 # 10000 20 16 10 38 39 ########################################1个需要注意 40 v1 = 16 41 #当前十进制用二进制表示时,最少使用的位数 42 data = v1.bit_length() 43 print(data) 44 45 #str 46 v1 = "alex" 47 48 # new_v1 = v1.upper() 49 # print(v1) 50 # print(new_v1) 51 52 53 # new_v1 = v1.capitalize() 54 # print(v1) 55 # print(new_v1)
布尔类脚本:
1 v1 = true # 1 2 v2 = false # 0
整数、布尔值、字符串之间转换脚本:
1 #int 8 9 2 #bool true false 3 #str "sdf" "7" ----只有字符串可以循环取值 4 5 6 #加法计算器 7 # v1 = input("请输入第一个值:") 8 # v2 = input("请输入第二个值:") 9 # v3 = int(v1) + int(v2) # 通过int将字符串转换为整数 10 # print(v3) 11 12 #字符串--> 整数 13 14 # v = true 15 # val = int(v) 16 # print(val) 17 18 19 # v = false 20 # val = int(v) 21 # print(val) 22 23 #布尔值-->整数 int(值) 24 #整数,布尔值----> 字符串 str(值) 25 # 整数,字符串 ----布尔 bool(值) 26 # - 0 false ,其他都为true 27 # - 字符串和空格都为true,只有不填时才为false 28 29 # v = 8 30 # val = str(v) 31 # print(val,type(val)) 32 33 34 # v = true 35 # val = str(v) 36 # print(val,type(val)) 37 38 39 # v = -1 40 # val = bool(v) 41 # print(val,type(val)) 42 43 44 # v = 0 45 # val = bool(v) 46 # print(val,type(val)) 47 48 49 # v = "abdcd" 50 # val = bool(v) 51 # print(val,type(val)) 52 53 54 # v = " " 55 # val = bool(v) 56 # print(val,type(val)) 57 58 59 # v = "" 60 # val = bool(v) 61 # print(val,type(val)) 62 63 # 面试题: 64 # a = 1 == 1 or 2 > 3 65 # print(a) 66 #true 67 68 # a = true or 2 > 3 69 # print(a) 70 #true 71 72 # a = false or 2 > 3 73 # print(a) 74 #false 75 76 # a = 0 or 2 > 3 77 # print(a) 78 #false 79 80 81 # a = 9 or 2 > 3 82 # print(a) 83 #9 84 85 86 # a = "asdf" or 2 > 3 87 # print(a) 88 # asdf 89 90 #总结: 91 # - 互相转换原则 类(值) 92 # -布尔值:只有0 ""为false 93 # -a = 9 or 2 > 3 94 95 # a = 0 or 6 96 # print(a) 97 # 6
列表类功能相关脚本:
1 ####################list类中为当前对象提供的功能##################### 2 3 """ 4 v = [11,22,33,44,"蒋毅"] 5 6 #1. 在列表尾部追加一个值-----修改了本身的值 7 v.append('李俊义') 8 print(v) 9 """ 10 11 """ 12 v = [] 13 while true: 14 name = input("请输入用户名:") 15 if name == 'q': 16 break 17 v.append(name) 18 print(v) 19 """ 20 21 """ 22 v = [] 23 while true: 24 name = input("请输入用户名:") 25 if name == 'q' or name == "q": 26 break 27 v.append(name) 28 print(v) 29 """ 30 31 """ 32 v = [] 33 while true: 34 name = input("请输入用户名:") 35 if name.lower() == 'q': 36 break 37 v.append(name) 38 print(v) 39 40 """ 41 42 """ 43 #2.在列表中间指定位置插入数据----insert 44 v = [11,22,33,44,"蒋毅"] 45 v.insert(4,"孙骑兵") 46 print(v) 47 """ 48 #3. 反转 49 """ 50 v = [11,22,33,44] 51 #print(v) 52 v.reverse() 53 print(v) 54 """ 55 56 57 58 59 #####################公共功能###################### 60 #1.通过索引取值----和字符串相同 61 # - v[0] 62 # - v[0:4] 63 # - v[0:4:2] 64 #2.计算长度 65 #2.5 替换或修改 66 # 替换: 67 """ 68 v = ["张勤学","成大井","成绩","刘龙华"] 69 v[3] = "吴国胜" 70 print(v) 71 """ 72 73 """ 74 #删除: 75 v = ["张勤学","成大井","成绩","刘龙华"] 76 del v[3] 77 print(v) 78 """ 79 80 """ 81 v = ["张勤学","成大井","成绩","刘龙华","alex"] 82 del v[3] 83 del v[2] 84 print(v) 85 """ 86 """ 87 v = ["张勤学","成大井","成绩","刘龙华","alex"] 88 del v[2:4] 89 print(v) 90 """ 91 92 """ 93 v = ["张勤学","成大井","成绩","刘龙华","alex"] 94 v[2:4] = "吴国胜" 95 print(v) 96 """ 97 """ 98 v = ["张勤学","成大井","成绩","刘龙华","alex"] 99 v[2:4] = ["吴国胜"] 100 print(v) 101 """ 102 """ 103 v = ["张勤学","成大井","成绩","刘龙华","alex"] 104 v[2:4] = ["吴国胜","狗胜","牛胜"] 105 print(v) 106 """ 107 108 #3.for循环 109 110 """ 111 v = [11,22,33,44,"蒋毅"] 112 print(v[3]) 113 114 115 v = [11,22,33,44,"蒋毅"] 116 val1 = v [3] 117 val2 = v [4] 118 119 val1.bit_length() 120 val2.split("蒋") 121 """ 122 123 """ 124 v = [11,22,33,44,"蒋毅"] 125 val = v[0:5:2] 126 print(val) 127 128 """ 129 130 """ 131 v = [11,22,33,44,"蒋毅"] 132 val = len(v) 133 print(val) 134 """ 135 """ 136 v = [11,22,33,44,"蒋毅"] 137 for i in v: 138 print(i) 139 """ 140 141 #练习: 142 """ 143 v = ["123","孙骑兵",666] 144 # val = len(v) 145 # print(val) 146 val = v[1][1] 147 print(val) 148 print(v[1]) 149 """ 150 """ 151 #将孙奇兵和666替换为王猛 152 v = ["123","孙骑兵",666] 153 v[1:3] = ["王猛",] 154 print(v) 155 """ 156 """ 157 #尾部添加王皓林 158 v = ["123","孙骑兵",666] 159 v.append('王皓林') 160 print(v) 161 """ 162 163 #补充: 164 #1.列表中元素可以是:数字,字符串,布尔值,另外一个列表 165 """ 166 li = [11,22,[6,5,7],33,true,"蒋毅",55] 167 v = len(li) 168 print(v) 169 val = li[2] 170 print(val) 171 """ 172 """ 173 li = [11,22,[6,"88991"],33,true,"蒋毅",55] 174 val = li[2][1][-2] 175 print(val) 176 """ 177 178 """ 179 #将33换成66 180 li = [11,22,[6,"88991"],33,true,"蒋毅",55] 181 li[3] = 66 182 print(li) 183 """ 184 #将7换成77 185 """ 186 li = [11,22,[6,"88991",7],33,true,"蒋毅",55] 187 li[2][2] = 77 188 print(li) 189 190 """ 191 #将88991换成666 192 """ 193 li = [11,22,[6,"88991",7],33,true,"蒋毅",55] 194 li[2][1] = 666 195 print(li) 196 """ 197 198 #字符串中数据替换 199 """ 200 v = "蒋毅" 201 v = v.replace('蒋','江') 202 print(v) 203 """ 204 205 #将"蒋毅"中的蒋替换为江 206 """ 207 li = [11,22,[6,"88991",7],33,true,"蒋毅",55] 208 val = li[5].replace('蒋','江') 209 li[5] = val 210 print(li) 211 """ 212 #简写 213 """ 214 li = [11,22,[6,"88991",7],33,true,"蒋毅",55] 215 li[5] = li[5].replace('蒋','江') 216 print(li) 217 """
元组类相关脚本:
1 #tuple 2 3 #v = (11,'ffff',22,33,44,) 4 5 ###########tuple 类中为当前对象提供的功能########### 6 #无 7 8 ##########公共功能############ 9 #1. 长度 10 11 12 """ 13 v = (11,'ffff',22,33,44,) 14 val = len(v) 15 print(val) 16 """ 17 #索引取值 18 """ 19 v = (11,'ffff',22,33,44,) 20 print(v[0]) 21 print(v[1]) 22 """ 23 #3.for循环 24 """ 25 v = (11,'ffff',22,33,44,) 26 for i in v: 27 print(i) 28 """ 29 30 #4. 元组也可以嵌套 31 """ 32 v = (11,'ffff',22,['蒋毅','二井"'],33,(9,1,2,"alex"),44,) 33 print(v) 34 """ 35 """ 36 v = (11,'ffff',22,['蒋毅','二井"'],33,(9,1,2,"alex"),44,) 37 v[3][1] = "xxxxx" 38 print(v) 39 """ 40 41 #元组的儿子不能替换修改,但元组的孙子可以替换或修改添加 42 v = (11,'ffff',22,['蒋毅','二井"'],33,(9,1,2,"alex"),44,) 43 v[3].append("asdasd") 44 print(v)
字典类功能相关脚本:
1 #####################dict类中为当前对象提供的功能############## 2 3 """ 4 v = { 5 "k1":'v1', # 键值对 6 "k2":'v2', # 键值对 7 } 8 9 val = v.get('k1') 10 print(val) 11 #------取k对应的value值 12 """ 13 14 """ 15 v = { 16 "k1":'v1', # 键值对 17 "k2":'v2', # 键值对 18 } 19 20 val = v.keys() 21 print(val) 22 #------取k的值 23 """ 24 25 """ 26 v = { 27 "k1":'v1', # 键值对 28 "k2":'v2', # 键值对 29 } 30 31 val = v.values() 32 print(val) 33 #-----取value值 34 """ 35 36 """ 37 v = { 38 "k1":'v1', # 键值对 39 "k2":'v2', # 键值对 40 } 41 42 val = v.items() 43 print(val) 44 #-----同时取k和的值 45 """ 46 47 ##########v.keys() v.values() v.items() 一般不单独使用,而是和for循环一起使用 48 49 # 当根据k获取对应的值不存在时,k不存在默认取值为none 50 """ 51 v = { 52 "k1":'v1', # 键值对 53 "k2":'v2', # 键值对 54 } 55 56 val = v.get('k1ffff') 57 print(val) 58 """ 59 60 # 当k值不存在时后面指定什么值,key就为什么值 61 """ 62 v = { 63 "k1":'v1', # 键值对 64 "k2":'v2', # 键值对 65 } 66 67 val = v.get('k1ffff','9999') 68 print(val) 69 """ 70 71 ###################公共功能############### 72 #1.获取字典的长度(键值的个数) 73 """ 74 v = { 75 "k1":'v1', # 键值对 76 "k2":'v2', # 键值对 77 } 78 val = len(v) 79 print(val) 80 """ 81 #2.索引 82 #不推荐(当k不存在时程序报错),推荐使用get取值 83 """ 84 v = { 85 "k1":'v1', # 键值对 86 "k2":'v2', # 键值对 87 } 88 val = v['k1'] 89 print(val) 90 91 """ 92 93 """ 94 v = { 95 "k1":'v1', # 键值对 96 "k2":'v2', # 键值对 97 } 98 val = v.get('k1') 99 print(val) 100 """ 101 102 #对字典进行设置值,如果k存在对应的值,则更新值;如果k不存在,则创建键值对 103 """ 104 v = { 105 "k1":'v1', # 键值对 106 "k2":'v2', # 键值对 107 } 108 #v['k1'] = 666 109 v['kkk1'] = 666 110 val = v.items() 111 print(val) 112 """ 113 114 #字典是无序的,所以不能对字典进行赋值 115 116 #del 删除键值对 117 """ 118 v = { 119 "k1":'v1', # 键值对 120 "k2":'v2', # 键值对 121 } 122 123 del v['k1'] 124 val = v.items() 125 print(val) 126 """ 127 128 #3. for循环 129 """ 130 v = { 131 "k1":'v1', # 键值对 132 "k2":'v2', # 键值对 133 } 134 for i in v: 135 print(i) 136 """ 137 #默认只列所有的k 138 """ 139 v = { 140 "k1":'v1', # 键值对 141 "k2":'v2', # 键值对 142 } 143 for i in v.keys(): 144 print(i) 145 """ 146 147 148 149 150 #只取value 151 """ 152 v = { 153 "k1":'v1', # 键值对 154 "k2":'v2', # 键值对 155 } 156 for i in v.values(): 157 print(i) 158 """ 159 160 #取完整键值对 161 #方法一 162 """ 163 v = { 164 "k1":'v1', # 键值对 165 "k2":'v2', # 键值对 166 } 167 for i in v.keys(): 168 print(i,v[i]) 169 """ 170 171 #分解 172 """ 173 v = { 174 "k1":'v1', # 键值对 175 "k2":'v2', # 键值对 176 } 177 178 val = v['k1'] 179 print(val) 180 """ 181 182 183 #方法二 184 """ 185 v = { 186 "k1":'v1', # 键值对 187 "k2":'v2', # 键值对 188 } 189 for k in v.items(): 190 print(k) 191 #------取值形式为元组模式 192 193 """ 194 195 196 """ 197 v = { 198 "k1":'v1', # 键值对 199 "k2":'v2', # 键值对 200 } 201 for k in v.items(): 202 print(k[0],k[1]) 203 """ 204 205 """ 206 #更简洁的方法 207 v = { 208 "k1":'v1', # 键值对 209 "k2":'v2', # 键值对 210 } 211 for k,v in v.items(): 212 print(k,v) 213 """ 214 215 #4. 字典嵌套 216 """ 217 v = { 218 "k1":123, 219 "k2":"asdf", 220 "k3":true, 221 "k4":[11,22,33], 222 "k5":(1,2,3), 223 "k6":{ 224 'kk1':'vv1', 225 'kk2':'vv2', 226 }, 227 'k7':[1,2,(8,6,2),{"k111":'vvv1'},5] 228 } 229 val = v.items() 230 print(val) 231 232 """ 233 234 #-----删除其中一个键值对 235 """ 236 v = { 237 "k1":123, 238 "k2":"asdf", 239 "k3":true, 240 "k4":[11,22,33], 241 "k5":(1,2,3), 242 "k6":{ 243 'kk1':'vv1', 244 'kk2':'vv2', 245 }, 246 'k7':[1,2,(8,6,2),{"k111":'vvv1'},5] 247 } 248 249 del v['k6']['kk2'] 250 print(v) 251 """ 252 253 #---替换掉k7里面的元组数据 254 """ 255 v = { 256 "k1":123, 257 "k2":"asdf", 258 "k3":true, 259 "k4":[11,22,33], 260 "k5":(1,2,3), 261 "k6":{ 262 'kk1':'vv1', 263 'kk2':'vv2', 264 }, 265 'k7':[1,2,(8,6,2),{"k111":'vvv1'},5] 266 } 267 v['k7'][2] = 666 268 print(v) 269 """ 270 271 #------在k7的嵌套字典里添加一个k222,vvv2 272 """ 273 v = { 274 "k1":123, 275 "k2":"asdf", 276 "k3":true, 277 "k4":[11,22,33], 278 "k5":(1,2,3), 279 "k6":{ 280 'kk1':'vv1', 281 'kk2':'vv2', 282 }, 283 'k7':[1,2,(8,6,2),{"k111":'vvv1'},5] 284 } 285 v['k7'][3]['k222'] = 'vvv2' 286 print(v) 287 """ 288 289 #列表中嵌套字典 290 291 """ 292 user_list = [ 293 {'name':'老狗','age':18}, 294 {'name':'流量','age':66}, 295 {'name':'wifi':'age':99}, 296 ] 297 """ 298 #练习:通过用户输入的姓名和年龄创建字典 299 300 """ 301 user_list = [ 302 {'name':'老狗','age':18}, 303 {'name':'流量','age':66}, 304 {'name':'wifi','age':99}, 305 ] 306 n = input("请输入姓名:") 307 a = input("请输入年龄:") 308 temp = {} 309 temp['name'] = n 310 temp['age'] = a 311 temp = {'name':n,'age':int(a)} 312 user_list.append(temp) 313 print(user_list) 314 """ 315 #或者下面的写法 316 317 """ 318 user_list = [ 319 {'name':'老狗','age':18}, 320 {'name':'流量','age':66}, 321 {'name':'wifi','age':99}, 322 ] 323 n = input("请输入姓名:") 324 a = input("请输入年龄:") 325 temp = {'name':n,'age':int(a)} 326 user_list.append(temp) 327 print(user_list) 328 """ 329 330 #------改进版----使用for循环 331 """ 332 user_list = [ 333 {'name':'老狗','age':18}, 334 {'name':'流量','age':66}, 335 {'name':'wifi','age':99}, 336 ] 337 n = input("请输入姓名:") 338 a = input("请输入年龄:") 339 temp = {'name':n,'age':int(a)} 340 user_list.append(temp) 341 for item in user_list: 342 print(item) 343 """ 344 345 346 #----再次改进 347 """ 348 user_list = [ 349 {'name':'老狗','age':18}, 350 {'name':'流量','age':66}, 351 {'name':'wifi','age':99}, 352 ] 353 n = input("请输入姓名:") 354 a = input("请输入年龄:") 355 temp = {'name':n,'age':int(a)} 356 user_list.append(temp) 357 for item in user_list: 358 print(item['name'],item['age']) 359 """