本文共 1759 字,大约阅读时间需要 5 分钟。
布尔表达式有and or not 组合时,优先级为:not>and>or,当然可以通过加括号来改变优先级,一般为了不引起歧义,开发过程也应该加上括号
深拷贝、浅拷贝:
list3 = [1,2,3,4,5,[6,7,8]]list3_new = list3#直接等的方式是赋值,相当于创建快捷方式,给列表取了别名,不生成新的对象print(id(list3),id(list3_new))list3[2]=9999print(id(list3),id(list3_new))print(list3_new)输出33864328 3386432833864328 33864328[1, 2, 9999, 4, 5, [6, 7, 8]]浅拷贝:import copylist3 = [1,2,3,4,5,[6,7,8]]list3_new = copy.copy(list3)#浅拷贝生成了新的对象print(id(list3),id(list3_new))list3[2]= 999print(id(list3),id(list3_new))print(list3)print(list3_new)输出:36709256 3671053636709256 36710536[1, 2, 999, 4, 5, [6, 7, 8]][1, 2, 3, 4, 5, [6, 7, 8]]import copylist3 = [1,2,3,4,5,[6,7,8]]list3_new = copy.copy(list3)#浅拷贝生成了新的对象,但是其中的子列表仍然是同一个对象print(id(list3),id(list3_new))list3[5][0] = 999print(id(list3),id(list3_new))print(list3)print(list3_new)输出34415496 3441677634415496 34416776[1, 2, 3, 4, 5, [999, 7, 8]][1, 2, 3, 4, 5, [999, 7, 8]]深拷贝:import copylist3 = [1,2,3,4,5,[6,7,8]]list3_new = copy.deepcopy(list3)#深拷贝列表和子列表都生成了新的对象print(id(list3),id(list3_new))list3[2]= 999print(id(list3),id(list3_new))print(list3)print(list3_new)输出40969096 4097037640969096 40970376[1, 2, 999, 4, 5, [6, 7, 8]][1, 2, 3, 4, 5, [6, 7, 8]]import copylist3 = [1,2,3,4,5,[6,7,8]]list3_new = copy.deepcopy(list3)print(id(list3),id(list3_new))list3[5][0]= 999print(id(list3),id(list3_new))print(list3)print(list3_new)输出40641416 4064269640641416 40642696[1, 2, 3, 4, 5, [999, 7, 8]][1, 2, 3, 4, 5, [6, 7, 8]]
条件判断:
1、Python对于缩进是有严格要求的,但是对于缩进几个字符是没有强制性要求的。 2、几个判断字符串的方法: str1 = ‘21341afd’ str1.isdigit(),str1.isalpha(),str1.isupper(),str1.islower(),str1.isalnum()【是否数字字母组合】a = '123kdi1D'print(a.isalnum())print(a.islower())print(a.isupper())print(a.isdigit())b = a.upper()print(b)c = a.lower()print(c)输出TrueFalseFalseFalse123KDI1D123kdi1d
转载地址:http://echof.baihongyu.com/