1 运算符
in
字符串 判断 : “hello” in "asdasfhelloasdfsadf"
列表元素判断:"li" in ['li', 'ok']
字典key判断:key in dic.keys()
2 基本的数据类型
1 类名() 其实就是执行类的 __init__ 2 3 int() 4 __init__(self,args,base=10) 5 6 int 7 a. 创建方式 两种 8 n1 = 123 # 根据int类,创建了一个对象 9 n2 = int(123) # 根据int类,创建了一个对象 类的实例化就是对象 10 b. int内部优化 11 1、n1 和 n2 的内存地址相同 12 n1 = 123 13 n2 = n1 14 2、按理说 n1 和 n2 的内存地址不相同 15 n1 = 123 16 n2 = 123 17 但是Python内部做了优化, 18 -5 ~ 257 以内的数,按照 2 的方式写时,都是指向同一个内存 19 n1 = 123 20 n2 = 123 21 除此以外的数,都是n1 和 n2 的内存地址不相同 python源码可以改取值范围 22 n1 = 123123 23 n2 = 123123 24 3、id查看对象的内存地址 25 n1 = 123 26 i1 = id(n1) 27 print(i1) 28 或者 29 i1 = id(123) 30 print(i1) 31 32 c. int长度限制 33 -2**31 ~ 2 **31 -1 34 -2**63 ~ 2**63 - 1 35 数字后有L 代表长整型 36 python 2.2之后,超过int的最大长度后,会自动转换成long类型,long类型无长度限制 37 38 python 3之后,int自己就无长度限制了 39 40 41 str, 42 a. 创建方式 43 s1 = "alex" 44 s1 = str('alex') 45 s1 = str('alex') #传参 字节和编码,可以加上编码方式 46 b. 特有功能 47 # 两端去除空格 48 # s1.strip() 49 50 # 以 .. 开头 51 # s1.startswith() 52 53 # 找子序列 "12","h" 54 # s1.find() 55 56 # 将字符串中的某子序列替换成 指定的值 57 # s1.replace() 58 59 # 变大写 60 # s1.upper() 61 62 # 是。。。吗? 63 # s1.isalpha() 64 65 c. 公共功能 66 索引:只能取一个元素 67 切片:取多个元素 68 len: 69 len("汉字") 70 3.5 ==》 字符 (utf-8 中 规定1个汉字3个字节,但是3版本中 1个汉字就是一个字符,不是字节。所以这里len是2) 71 2.7==》 字节(utf-8 中 规定1个汉字3个字节,所以这里len是6) 72 for: 73 3.5 ==》 字符 (3 版本中 循环汉字字符串不是字节,而是按照字符来进行循环的) 74 2.7==》 字节(2版本总循环汉字是以字节来循环的) 75 76 编码、for bytes方法:转换成字节 77 3版本python中 汉字字符串循环不是按照字节循环,而是按照字符循环。 78 name = "李露" 79 for i in name: 80 print(i) #循环打印每个汉字而不是字节 81 bytes_list = bytes(i, encoding='utf-8') #将汉字转成字节,编码是utf-8 82 print(bytes_list) 83 for b in bytes_list: 84 #1、3.5 for循环时候,循环的每一个元素是 “字符” 85 #2、字符 =》 字节 86 bytes_list = bytes("字符串",encoding='utf-8') 87 88 # utf-8 -> 3字节 1个汉字 89 # gbk -》 2字节 1个汉字 90 print(bytes_list) # 默认每一个字节都是16进制表示 91 for b in bytes_list: 92 print(b) # 默认每一个字节都是10进制表示 93 94 3、10进制的数字 ==》 2进制 95 len 96 id 97 bin(10进制的数字) 98 d、 bytes和str的转换 将字节转换成字符串 bytes方法 99 a = "李露" 100 # 将字符串转换成字节 101 b1 = bytes(a, encoding='utf-8') 102 print(b1) 103 b2 = bytes(a, encoding='gbk') 104 print(b2) 105 # 将字节转换成字符串 106 107 newa1 = str(b1, encoding="utf-8") 108 print(newa1) 109 110 newa2 = str(b2, encoding='gbk') 111 print(newa2) 112 113 114 ########### 115 x = str() 116 # 创建字符串 117 # 转换成字符串,字节,编码 118 m = bytes() 119 # 创建字节 120 # 转换成字节,字符串,要编程什么编码类型的字节 121 122 python进制转换 123 hex 可以 十进制转16进制 二进制转16进制 结果都是字符串 124 >>> hex(0b10) 125 '0x2' 126 >>> hex(10) 127 '0xa' 128 bin 可以十进制转2进制 16进制转2进制 结果都是字符串 129 >>> bin(10) 130 '0b1010' 131 >>> bin(0x2) 132 '0b10' 133 int 可以16进制转换十进制 2进制转换十进制 134 >>> int(0xe) 135 136 >>> int(0b100) 137 138 139 140 list 141 可变元素的“集合” 142 143 ----------- 144 str -> 创建字符串,或者将其他的转换成字符串 145 ------------------------------------------ 146 list -> 创建列表,将其他元素转换成列表 147 148 a. 创建和转换 149 1、创建 150 li = [11,22,33,4] 151 li = list() 152 li = list([11,22,33,4]) 153 2、转换 注:放可迭代的 凡是可以for循环的都可以迭代。 字符串 元组 字典 列表本身 都可以迭代 154 s1 = "李露" 155 # for,字符 ==> 可迭代 156 l1 = list(s1) # for循环,将循环的每一个元素,当做列表的元素 157 # ["李", "露"] 158 print(l1) 159 160 # 元组 转换成列表 161 # t2 = ("alex", "laonanhai", "seven") 162 # l2 = list(t2) 163 # print(l2) 164 165 # 字典 转换成列表 166 # dic = {'k1': "alex", "k2": 'seven'} 167 # l3 = list(dic.items()) 168 # print(l3) 169 # 字符串,元组,字典 =》 列表 170 171 b. 列表特有功能 172 # 追加 173 # li.append() 174 # 清除 175 # li.clear() 176 # 扩展自己,用另外一个可迭代的对象,扩充到自己内部 177 # str,list,dict,tuple 178 # s = "李露" 179 # li.extend(s) 180 # print(li) 181 # 翻转,自己内部元素翻转 182 # li.reverse() 183 # 向指定位置插入指定元素 184 # li.insert(1, "X") 185 c. 公共功能 186 li = ["alex", "eric", 'seven', 123] 187 索引:li[2] 188 切片:li[2:3] 189 del 190 enumerate 191 for 192 len 193 join 194 d. 多层列表 字典取值 195 li = ["alex", "eric", 'seven', 123] 196 li = [ "alex" , 123, {"k1":"v1", "k2": {"vv": (11,22,123), "ii": 456}}] 197 198 li[2] --> {"k1":"v1", "k2": {"vv": 123, "ii": 456}} 199 li[2]['k2'] ==> {"vv": 123, "ii": 456} 200 li[2]['k2']["vv"] ==> (11,22,123) 201 li[2]['k2']["vv"][2] 202 203 元组 tuple 转换时候放可迭代的,即可以for循环取的类型。 比如 字符串 列表 字典都可以循环 204 a. 创建和转换 205 t = (11,22,33) 206 t = tuple((11,22,33)) 207 t = tuple([]) # 字符串,列表,字典 208 b. 特有方法 209 count 210 index 211 c. 嵌套(元素不可修改) 注:如果元素是列表或者字典则可以修改 212 t = (11,22,33) 213 t = (11,22,["alex", {"k1": "v1"}]) 214 215 216 e. 元组的特性,不可修改,谁不可被修改 #如果元素是列表或者字典可以修改 217 元组,儿子不能变 218 元组,儿子不能变,孙子,... 219 220 整理: 元组 字符串 特性总结 221 一般字符串,执行一个功能,生成一个新内容,原来内容不变 222 list,tuple,dict,执行一个功能,自身进行变化 223 224 上节总结 基本数据类型
********************** 列表一句转变成字典 li = [11,12,13] dic = dict(enumerate(li,10)) print(11) print(dic) ********************** 2 版本 print(dic.keys()) 直接返回 一个列表 3 版本 print(dic.keys()) 返回的是一个类 ********************** fromkeys(seq,value=None) 解释: 默认不写value的话,所有的值为None n = dict.fromkeys(['k1', 'k2'], []) #默认逗号后面不给的话,key对应的value都是None n['k1'].append(2) #我们修改逗号后面的空列表元素 打印 所有key的value也会修改 print(n) n['k1'] = 3 # 而单独修改key的value的话,只有 这个key的value改了,而其他的key对应的value都不变 print(n) n = dict.fromkeys() #默认逗号后面不给的话,key对应的value都是None print(n)
1、set 集合
set集合,是一个无序且不重复的元素集合
1 class set(object): 2 """ 3 set() -> new empty set object 4 set(iterable) -> new set object 5 6 Build an unordered collection of unique elements. 7 """ 8 def add(self, *args, **kwargs): # real signature unknown 9 """ 10 Add an element to a set,添加元素 11 12 This has no effect if the element is already present. 13 """ 14 pass 15 16 def clear(self, *args, **kwargs): # real signature unknown 17 """ Remove all elements from this set. 清楚内容""" 18 pass 19 20 def copy(self, *args, **kwargs): # real signature unknown 21 """ Return a shallow copy of a set. 浅拷贝 """ 22 pass 23 24 def difference(self, *args, **kwargs): # real signature unknown 25 """ 26 Return the difference of two or more sets as a new set. A中存在,B中不存在 27 28 (i.e. all elements that are in this set but not the others.) 29 """ 30 pass 31 32 def difference_update(self, *args, **kwargs): # real signature unknown 33 """ Remove all elements of another set from this set. 从当前集合中删除和B中相同的元素""" 34 pass 35 36 def discard(self, *args, **kwargs): # real signature unknown 37 """ 38 Remove an element from a set if it is a member. 39 40 If the element is not a member, do nothing. 移除指定元素,不存在不保错 41 """ 42 pass 43 44 def intersection(self, *args, **kwargs): # real signature unknown 45 """ 46 Return the intersection of two sets as a new set. 交集 47 48 (i.e. all elements that are in both sets.) 49 """ 50 pass 51 52 def intersection_update(self, *args, **kwargs): # real signature unknown 53 """ Update a set with the intersection of itself and another. 取交集并更更新到A中 """ 54 pass 55 56 def isdisjoint(self, *args, **kwargs): # real signature unknown 57 """ Return True if two sets have a null intersection. 如果没有交集,返回True,否则返回False""" 58 pass 59 60 def issubset(self, *args, **kwargs): # real signature unknown 61 """ Report whether another set contains this set. 是否是子序列""" 62 pass 63 64 def issuperset(self, *args, **kwargs): # real signature unknown 65 """ Report whether this set contains another set. 是否是父序列""" 66 pass 67 68 def pop(self, *args, **kwargs): # real signature unknown 69 """ 70 Remove and return an arbitrary set element. 71 Raises KeyError if the set is empty. 移除元素 72 """ 73 pass 74 75 def remove(self, *args, **kwargs): # real signature unknown 76 """ 77 Remove an element from a set; it must be a member. 78 79 If the element is not a member, raise a KeyError. 移除指定元素,不存在保错 80 """ 81 pass 82 83 def symmetric_difference(self, *args, **kwargs): # real signature unknown 84 """ 85 Return the symmetric difference of two sets as a new set. 对称交集 86 87 (i.e. all elements that are in exactly one of the sets.) 88 """ 89 pass 90 91 def symmetric_difference_update(self, *args, **kwargs): # real signature unknown 92 """ Update a set with the symmetric difference of itself and another. 对称交集,并更新到a中 """ 93 pass 94 95 def union(self, *args, **kwargs): # real signature unknown 96 """ 97 Return the union of sets as a new set. 并集 98 99 (i.e. all elements that are in either set.) 100 """ 101 pass 102 103 def update(self, *args, **kwargs): # real signature unknown 104 """ Update a set with the union of itself and others. 更新 """ 105 pass 106 107 set 集合class