1.4 - 数据类型/字符编码练习题
1.可变数据类型: list dict set
2.不可变数据类型: 数字类(bool int float complex) str tuple(元祖) frozenset(不可变集合)
1 # 1.下划线将列表拼接成字符串 2 li = ['alex', 'eric', 'rain'] 3 s = '_'.join(li) 4 print(s) 5 6 # 2.移除空格 并以a A 开头 c 结尾得元素 7 li = ["alec", " aric", "Alex", "Tony", "rain"] 8 li_new = [] 9 tu = ("alec", " aric", "Alex", "Tony", "rain") 10 dic = {'K1': "alex", 'K2': ' aric', "K3": "Alex","K4": "Tony"} 11 12 for i in li: 13 if i.strip().startswith('a') or i.strip().startswith('A') and i.strip().endswith('c'): 14 li_new.append(i.strip()) 15 print(li_new) 16 17 for i in tu: 18 if i.strip().startswith('a') or i.strip().startswith('A') and i.strip().endswith('c'): 19 print(i.strip()) 20 21 for i in dic: 22 if dic[i].strip().startswith('a') or dic[i].strip().startswith('A') and dic[i].strip().endswith('c'): 23 print(dic[i].strip()) 24 25 # 3. 实现每一个功能 26 li = ['alex', 'eric', 'rain'] 27 print(len(li)) 28 li.append("seven") 29 print(li) 30 li.insert(0, 'Tony') 31 print(li) 32 li[1] = 'Kelly' 33 print(li) 34 li.remove('eric') 35 print(li) 36 print(li.pop(1)) 37 print(li) 38 del li[2] 39 print(li) 40 temp = [1,2,3,4,5,6,5,4,3,2,1,0] 41 print(temp) 42 del temp[2:4] 43 print(temp) 44 temp.reverse() 45 print(temp) 46 for index in range(len(li)): 47 print(index,li[index]) 48 for index,i in enumerate(li): 49 print(index,i) 50 for index, i in enumerate(li,100): 51 print(index, i) 52 for i in li: 53 print(i) 54 55 # 4.实现每一个功能 56 li = ["hello", 'seven', ["mon", ["h", "kelly"], 'all'], 123, 446] 57 58 print(li[2][1][1]) 59 li[2][2] = 'ALL' 60 print(li) 61 62 # 5.实现每一个功能 63 tu = ('alex', 'eric', 'rain') 64 65 print(len(tu)) 66 print(tu[1]) 67 print(tu[1:3]) 68 for i in tu: 69 print(i) 70 for index in range(len(tu)): 71 print(index,tu[index]) 72 for index, i in enumerate(tu,10): 73 print(index,i) 74 75 # 6.实现功能 76 # 元祖得特性:元祖本身不可变,如果元祖中包含可变元素,这些可变元素可以改变 77 tu = ('alex', [11, 22, {"K1": 'V1', "K2": ["age", "name"], "K3": (11, 22, 33)}, 44]) 78 print(tu) 79 # 1.第一个元素alex不可被修改 80 # 2.K2对应得值得类型是列表,可以被修改 81 # 3.K3对应得是元祖类型,不可被修改 82 tu[1][2]["K2"].append("seven") 83 print(tu) 84 85 # 7.字典 86 dic = {'K1': 'V1', 'K2': 'V2', 'K3': [11, 22, 33]} 87 88 for i in dic.keys(): 89 print(i) 90 for i in dic.values(): 91 print(i) 92 for i in dic: 93 print(i, dic[i]) 94 dic['K4'] = 'V4' 95 print(dic) 96 print(dic.setdefault("K1", 'alex')) 97 print(dic) 98 dic['K1'] = 'alex' 99 print(dic) 100 dic['K3'].append(44) 101 print(dic) 102 dic['K3'].insert(0, 18) 103 print(dic) 104 105 # 8.转换 106 s = 'alex' 107 print(s.split(' ')) 108 print(tuple(s)) 109 li = ["alex", 'seven'] 110 print(tuple(li)) 111 tu = ('Alex', 'Seven') 112 print(list(tu)) 113 li = ["alex", 'seven'] 114 li1= [10, 11] 115 print(dict(zip(li1,li))) 116 li = [[1,2],[2,2]] 117 print(dict(li)) 118 119 # 9.元素分类:{'k1':大于66得值,'k2':小于66得值} 120 li = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90] 121 dic = {} 122 dic['k1'] = li[-4:] 123 dic['k2'] = li[:5] 124 print(dic) 125 126 # 10.用户添加商品 选择商品 127 li = ["手机", "电脑", "鼠标垫", "游艇"] 128 129 products = input("添加商品:") 130 li.append(products) 131 print(li) 132 index = int(input("商品序号:")) 133 print(li[index]) 134 135 # 11. 后期 用户交互 省市县 输入省显示下面得市内容 136 dic = {"河北": {"石家庄": ["鹿泉", "元氏"], 137 "邯郸": ["永年", "涉县"]}, 138 "河南": {}, 139 "山西": {}} 140 141 while True: 142 province = input("请输入省份,输入q时退出:").strip() 143 if province is 'q': 144 break 145 elif not province in dic: 146 print("输入得省有误!") 147 else: 148 # print(dic[province].keys()) 149 for city in dic.get(province): 150 print(city) 151 city = input("请输入市").strip() 152 print(dic.get(province).get(city)) 153 154 # 12. 布尔值是false得所有值 155 print(bool(False)) 156 print(bool(None)) 157 print(bool(0)) 158 print(bool("")) 159 print(bool(())) 160 print(bool([])) 161 print(bool({})) 162 163 # 13.两个列表得比较: 164 L1 = [11, 22, 33] 165 L2 = [22, 33, 44] 166 167 print(set(L1).intersection(set(L2))) 168 print(set(L1).difference(set(L2))) 169 print(set(L2).difference(set(L1))) 170 print(set(L1).symmetric_difference(set(L2))) 171 172 # 14.利用for循环和range输出 173 li = [] 174 for i in range(1,100): 175 li.append(i) 176 print(li) 177 li.reverse() 178 print(li) 179 count = 0 180 while count < 20: 181 print(count) 182 count += 1 183 184 # 15.for和range输出9*9乘法表 185 for i in range(1, 10): 186 for j in range(1, i+1): 187 print("%d*%d=%d" % (i, j, i*j), end=" ") 188 print()