1、整型 int
1 # Python3中,所有的整型都是int类型 2 # 1.int():将字符串转换为数字 3 a = "123" 4 b = int(a) 5 # type():识别数据类型 6 print(type(a), type(b)) # <class ‘str‘> <class ‘int‘> 7 # 转成16进制 8 num = "b" 9 v = int(num, base=16) 10 print(v) # 11 11 12 # 2.bit_length():当前数字的二进制,至少用n位表示 13 age = 5 14 v = age.bit_length() 15 print(v) # 3
2、字符串 str
1 # 1.capitalize():将字符串首字母变成大写 2 s = "admin" 3 v1 = s.capitalize() 4 print(v1) # Admin 5 6 # 2.casefold():将字符串字母变成小写 7 v2 = v1.casefold() 8 print(v2) # admin 9 10 # 3.expandtabs():断句20, 11 s = "username\tpassword\temail\nguixiang\t123456\tguixiang.126.com\nguixiang\t123456\tguixiang.126.com" 12 v3 = s.expandtabs(20) 13 print(v3) 14 # username password email 15 # guixiang 123456 guixiang.126.com 16 # guixiang 123456 guixiang.126.com 17 18 # 4.center():设置宽度,并将内容居中 19 # 20表示总长度, 20 # * 表示空白未知填充,一个字符,可有可无 21 v4 = v3.center(20, "*") 22 print(v4) # *******ADMIN******** 23 24 # 5.rjust():设置宽度,并将内容居左 25 v5 = v3.rjust(20, "*") 26 print(v5) # ***************ADMIN 27 28 # 6.ljust():设置宽度,并将内容居右 29 v6 = v3.ljust(20, "-") 30 print(v6) # ADMIN--------------- 31 32 # 7.count():去字符串寻找子序列出现的个数 33 # 2表示起始位置开始查找,可有可无 34 # 8表示结束位置,可有可无 35 s = "testtest" 36 v7 = s.count("st", 2, 8) 37 print(v7) # 2 38 39 # 8.endswith():以什么结尾,返回布尔值 40 # startswith():以什么开始,返回布尔值 41 s = "testtest" 42 v8 = s.endswith("st") 43 print(v8) # True 44 v8 = s.startswith("te") 45 print(v8) # True 46 47 # (重点)9.find():从开始往后找,找到第一个之后,获取其位置;没有则返回-1 48 # 2表示起始位置开始查找,可有可无 49 # 8表示结束位置,可有可无 50 s = "testtest" 51 v9 = s.find("st", 2, 8) 52 print(v9) # 2 53 54 # 10.format():格式化,将一个字符串中的占位符替换为指定的值 55 s = "i am {0},age {1}" 56 v10 = s.format("桂香", 20) 57 print(v10) # i am 桂香,age 20 58 59 # 11.format_map() 60 s = "i am {name},age {a}" 61 v11 = s.format_map({"name": "湘桂", "a": 20}) 62 print(v11) # i am 桂香,age 20 63 64 # 12.isalnum():判断字符串中是否只包含 字母和数字 65 s = "123test" 66 v12 = s.isalnum() 67 print(v12) # True 68 69 # 13.isalpha():判断字符串中是否只包含 字母 70 s = "123test" 71 v13 = s.isalpha() 72 print(v13) # False 73 74 # 14.isdigit()、isdigit()、isnumeric():判断字符串中是否只包含 数字 75 s = "一" 76 v14 = s.isdecimal() # 十进制数字 77 v14 = s.isdigit() # 十进制数字和特殊数字(如:②) 78 v14 = s.isnumeric() # 十进制数字、特殊数字(如:②)和 中文数字(如:一) 79 print(v14) # True 80 81 # 15.isidentifier():判断是否以 字母 或 _ 开头 82 s = "_123" 83 v15 = s.isidentifier() 84 print(v15) # True 85 86 # 16.isprintable():判断是否只包含 可显示的字符 87 # \t:制表符 88 # \n:换行符 89 s = "testtest\t" 90 v16 = s.isprintable() 91 print(v16) # False 92 93 # 17.isspace():判断是否全是 空字符 94 s = " " 95 v17 = s.isspace() 96 print(v17) # True 97 98 # 18.istitle(): 判断是否只有所有的首字母都转成大写,其余的为小写 99 s = "One TWO Three" 100 v18 = s.istitle() 101 print(v18) # False 102 103 # 19.title(): 把所有的首字母都转成大写,其余的为小写 104 s = "One TWO Three" 105 v19 = s.title() 106 print(v19) # One Two Three 107 108 # (重点)20.join():将字符串中的每一个元素按照指定分隔符进行拼接 109 s = "你是风儿我是沙" 110 v20 = "_".join(s) 111 print(v20) # 你_是_风_儿_我_是_沙 112 113 # 21.islower():判断字符串是否都是小写 114 s = "Admin" 115 v21 = s.islower() 116 print(v21) # False 117 118 # (重点)22.lower():将字符串转成小写 119 s = "Admin" 120 v22 = s.lower() 121 print(v22) # admin 122 123 # 23.isupper():判断字符串是否都是大写 124 v23 = s.isupper() 125 print(v23) # False 126 127 # (重点)24.upper():将字符串字母变成大写 128 v24 = s.upper() 129 print(v24) # ADMIN 130 131 # 25.lstrip():去掉字符串前不可见字符,也可以去掉指定字符 132 s = " test " 133 v25 = s.lstrip() 134 print(v25) # test 135 136 # 26.rstrip():去掉字符串后不可见字符,也可以去掉指定字符 137 s = " test " 138 v26 = s.rstrip() 139 print(v26) # test 140 141 # (重点)27.strip():去掉字符串前后不可见字符,也可以去掉指定字符 142 s = " test " 143 v27 = s.strip() 144 print(v27) # test 145 146 # 28.partition():指定分隔符,只能分3份 147 s = "asfdjdsbdsff" 148 v28 = s.partition("s") 149 print(v28) # (‘a‘, ‘s‘, ‘fdjdsbdsff‘) 150 151 # (重点)29.split:指定分隔符,拿不到分隔符,默认找全部,也可以指定 152 s = "asfdjdsbdsff" 153 v29 = s.split("s", 2) 154 print(v29) # [‘a‘, ‘fdjd‘, ‘bdsff‘] 155 156 # 30.splitlines():以换行符分隔;true、false表示是否保留换行符 157 s = "asfd\njdsb\ndsff" 158 v30 = s.splitlines(True) 159 print(v30) # [‘asfd\n‘, ‘jdsb\n‘, ‘dsff‘] 160 161 # 31.swapcase():大小写转换 162 s = "Admin" 163 v31 = s.swapcase() 164 print(v31) # aDMIN 165 166 # (重点)32.replace():指定字符串替换;可以指定替换几个,默认全部 167 s = "testtest" 168 v32 = s.replace("es", "bbb", 1) 169 print(v32) # tbbbttest 170 171 # (重点)33.索引,下标,获取字符串中的某一个字符 172 s = "test" 173 v33 = s[0] 174 print(v33) # t 175 176 # (重点)34.切片 177 s = "test" 178 v34 = s[0:-1] 179 print(v34) # tes 180 181 # (重点)35.len(): 182 # Python3:获取当前字符串中由几个字符组成 183 s = "test" 184 v35 = len(s) 185 print(v35) # 4 186 187 # (重点)36.str():将int类型转成字符串类型 188 s = 123 189 v36 = str(s) 190 print(type(s), type(v36)) # <class ‘int‘> <class ‘str‘> 191 192 # (重点)37.for循环 193 s = "abcd" 194 for i in s: 195 print(i) 196 # a 197 # b 198 # c 199 # d 200 201 # 帮助创建连续数字,也可以通过设置步长来指定不连续 202 # python3:循环迭代使用时,创建 203 # python2:立即创建 204 v = range(0, 30, 5) 205 for i in v: 206 print(i) 207 # 0 208 # 5 209 # 10 210 # 15 211 # 20 212 # 25 213 214 for i in range(0, len("abcd")): 215 print(i, s[i]) 216 # 0 a 217 # 1 b 218 # 2 c 219 # 3 d 220 221 # (重点)38. 222 # 字符串一旦创建,不可修改 223 # 一旦修改或拼接,都会造成重新生成字符串
3、列表 list
1 # 中括号括起来 2 # ,分割每个元素 3 # 列表中的元素可以是 数字,字符串,布尔,列表....可以放任何东西 4 # (重点)1.索引取值 5 li = [1, 2, 5, "湘桂", ["gg", "mm"]] 6 print(li[3]) # 湘桂 7 print(li[3:5]) # [‘湘桂‘, [‘gg‘, ‘mm‘]] 8 9 # (重点)2.for、while循环遍历 10 for item in li: 11 print(item) 12 # 1 13 # 2 14 # 5 15 # 湘桂 16 # [‘gg‘, ‘mm‘] 17 18 # (重点)3.列表元素可以被修改 19 li[3] = 4 20 print(li) # [1, 2, 5, 4, [‘gg‘, ‘mm‘]] 21 22 # 4.删除 23 del li[2:5] 24 print(li) # [1, 2] 25 26 # 5.in 操作 27 v = 1 in li 28 print(v) # True 29 v = "gg" in li 30 print(v) # False 31 32 # (重点)6.取值操作 33 li = [1, 2, 5, "湘桂", ["gg", "mm"]] 34 v = li[4][1] 35 print(v) # mm 36 37 # 7.list():字符串转成列表,内部使用for循环;int不能 38 s = "assdsck" 39 new_li = list(s) 40 print(new_li) # [‘a‘, ‘s‘, ‘s‘, ‘d‘, ‘s‘, ‘c‘, ‘k‘] 41 42 # 8.列表转换成字符串,需要自己写for循环处理 43 # 列表包含int和字符串 44 li = [12, 13, 15, "17", "test"] 45 s = "" 46 for i in li: 47 s += str(i) 48 print(s) # 12131517test 49 # 列表只包含字符串,直接使用字符串的join方法 50 li = ["17", "test"] 51 s = "".join(li) 52 print(s) # 17test 53 54 # (重点)9.append():在原li追加元素,不需要接收 55 li = [11, 22, 33, 44] 56 v9 = li.append(55) 57 print(v9) # None 58 print(li) # [11, 22, 33, 44, 55] 59 60 # 10.clear():清空列表 61 li.clear() 62 print(li) # [] 63 64 # 11.copy():拷贝,浅拷贝 65 li = [11, 22, 33, 44] 66 v11 = li.copy() 67 print(v11) # [11, 22, 33, 44] 68 69 # 12.count():计算元素出现的次数 70 v12 = li.count(22) 71 print(v12) # 1 72 73 # (重点)13.extend():扩展原列表 74 li.extend([55, "test"]) 75 print(li) # [11, 22, 33, 44, 55, ‘test‘] 76 li.extend("test") 77 print(li) # [11, 22, 33, 44, 55, ‘test‘, ‘t‘, ‘e‘, ‘s‘, ‘t‘] 78 79 # 14.index():根据值获取当前值索引位置,左边优先 80 li = [11, 22, 33, 44] 81 v14 = li.index(22) 82 print(v14) # 1 83 84 # (重点)15.insert():指定索引位置插入值 85 li.insert(0, 8) 86 print(li) # [8, 11, 22, 33, 44] 87 88 # 16.pop():指定索引删除某个值,并且获取删除的值,默认是删除最后一个 89 li = [11, 22, 33, 44] 90 v16 = li.pop(2) 91 print(v16) # 33 92 print(li) # [11, 22, 44] 93 94 # 17.remove():指定删除列表中的值,左边优先 95 li = [11, 22, 33, 44] 96 li.remove(11) 97 print(li) # [22, 33, 44] 98 99 # 18.reverse():将当前列表进行反转 100 li = [11, 22, 33, 44] 101 li.reverse() 102 print(li) # [44, 33, 22, 11] 103 104 # 19.sort():列表的排序,默认升序;参数reverse=True表示降序 105 li = [11, 44, 22, 33] 106 li.sort(reverse=True) 107 print(li) # [44, 33, 22, 11]
4、元组 tuple
1 # (重点)一级元素不能修改,不能删除,不能添加 2 tu = (11, 22, "test", (11, 22), 11, 22, [‘11‘, ‘22‘],) 3 print(tu) # (11, 22, ‘test‘, (11, 22), 11, 22, [‘11‘, ‘22‘]) 4 5 # (重点)1.索引,切片 6 v1 = tu[1] 7 print(v1) # 22 8 v1 = tu[0:3] 9 print(v1) # (11, 22, ‘test‘) 10 11 # (重点)2.可以for循环,可迭代对象 12 for i in tu: 13 print(i) 14 # 11 15 # 22 16 # test 17 # (11, 22) 18 # 11 19 # 22 20 # [‘11‘, ‘22‘] 21 22 # 3.字符串,列表转成元组 23 s = "asdacd" 24 v3 = tuple(s) 25 print(v3) # (‘a‘, ‘s‘, ‘d‘, ‘a‘, ‘c‘, ‘d‘) 26 li = [11, 22, 33, 44] 27 v3 = tuple(li) 28 print(v3) # (11, 22, 33, 44) 29 30 # 4.元组转换成列表 31 v4 = list(tu) 32 print(v4) # [11, 22, ‘test‘, (11, 22), 11, 22, [‘11‘, ‘22‘]] 33 34 # 5.元组转字符串和列表转字符串同理 35 36 # 6.指定索引获取元素 37 tu = (11, 22, "test", (11, 22), 11, 22, [‘11‘, ‘22‘],) 38 v6 = tu[6][1] 39 print(v6) # 22 40 41 # 7.修改二级元素 42 tu = (11, 22, "test", (11, 22), 11, 22, [(‘11‘, ‘22‘)],) 43 tu[6][0] = 123 44 print(tu) # (11, 22, ‘test‘, (11, 22), 11, 22, [123]) 45 46 # 8.count():计算元素出现的次数 47 tu = (11, 22, 33, 44) 48 v8 = tu.count(33) 49 print(v8) # 1 50 51 # 9.index():根据值获取当前值索引位置,左边优先 52 tu = (11, 22, 33, 44) 53 v9 = tu.index(11) 54 print(v9) # 0
5、字典 dict
1 # 字典是无序的 2 info = { 3 "k1": "v1", # 键值对 4 "k2": "v2" 5 } 6 print(info) # {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘} 7 8 # 字典的key可以是:字符串,布尔值, 元组; value可以是任意值 9 info = { 10 "name": "桂香", 11 "age": 20, 12 "sex": ["男", "女"], 13 "addr": { 14 "k1": (11, 22) 15 }, 16 True: "10", 17 (1, 2, 3): "123" 18 } 19 print(info) # {‘name‘: ‘桂香‘, ‘age‘: 20, ‘sex‘: [‘男‘, ‘女‘], ‘addr‘: {‘k1‘: (11, 22)}, True: ‘10‘, (1, 2, 3): ‘123‘} 20 21 # (重点)1.通过索引key获取指定元素value值 22 v = info["name"] 23 print(v) # 桂香 24 v = info["addr"]["k1"][0] 25 print(v) # 11 26 27 # 2.通过索引key删除元素 28 del info["addr"] 29 print(info) # {‘name‘: ‘桂香‘, ‘age‘: 20, ‘sex‘: [‘男‘, ‘女‘], True: ‘10‘, (1, 2, 3): ‘123‘} 30 31 # (重点)3.for循环; keys()获取key,values()获取value,items()获取键值对,默认key 32 for k, v in info.items(): 33 print(k, v) 34 # name 桂香 35 # age 20 36 # sex [‘男‘, ‘女‘] 37 # True 10 38 # (1, 2, 3) 123 39 40 # 4.clear():清空字典 41 42 # 5.copy():拷贝字典 43 44 # 6.dict.fromkeys():根据序列创建字典,并指定统一的值 45 v6 = dict.fromkeys(["k1","k2"],123) 46 print(v6) # {‘k1‘: 123, ‘k2‘: 123} 47 48 # (重点)7.get():根据key获取值,若key不存在时,可以指定默认值(None) 49 dic = { 50 "k1": "v1", 51 "k2": "v2" 52 } 53 v7 = dic.get("k3",1111) 54 print(v7) # 1111 55 56 # 8.pop():指定key删除,若key不存在,可以指定返回值 57 v8 = dic.pop("k3",80) 58 print(v8) # 80 59 print(dic) # {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘} 60 61 # 9.popitem():随机删除一个key 62 v9 = dic.popitem() 63 print(v9) # (‘k2‘, ‘v2‘) 64 print(dic) # {‘k1‘: ‘v1‘} 65 66 # 10.setdefault():设置值 67 # 已存在,不设置,获取当前key对应的值 68 # 已存在,设置,获取当前key对应的值 69 dic = { 70 "k1": "v1", 71 "k2": "v2" 72 } 73 v10 = dic.setdefault("k3","v3") 74 print(dic,v10) # {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘, ‘k3‘: ‘v3‘} v3 75 76 # (重点)11.update():更新 77 dic = { 78 "k1": "v1", 79 "k2": "v2" 80 } 81 dic.update({"k1":"1111","k3":123}) 82 print(dic) # {‘k1‘: ‘1111‘, ‘k2‘: ‘v2‘, ‘k3‘: 123} 83 dic.update(k1=123,k3=456) 84 print(dic) # {‘k1‘: 123, ‘k2‘: ‘v2‘, ‘k3‘: 456}
6、布尔 bool
1 # 0 1 2 # False:None "" () [] {} 0
原文地址:https://www.cnblogs.com/lihai1020/p/9736799.html
时间: 2024-10-11 10:43:16