1. 强制转换成容器数据类型
# ### 强制类型转换 容器类型数据 (str list tuple set ) var1 = "你好世界" var2 = ["陈博文","刘守乱","钟志红","桂晓斌"] var3 = ("陈博文","刘守乱","钟志红","桂晓斌") var4 = {"陈博文","刘守乱","钟志红","桂晓斌"} var5 = 5488 var6 = {"a":1,"b":2} # 1.str 强制转换成字符串 """就是单纯的在原有数据的两边套上引号""" res = str(var2) res = str(var4) res = str(var5) print(res , type(res)) # repr 不转义字符,原型化输出 , 可以显示引号 print(repr(res)) # 2.list 强制转换成列表 """ 如果是字符串:把每一个字符都当成一个新元素组合在一起,形成新列表 如果是字典:只保留字典的键 如果是其他容器:仅仅单纯的在原有数据的两边,套上中括号[]即可 """ res = list(var1) res = list(var3) res = list(var4) res = list(var6) print(res , type(res)) # 3.tuple 强制转换成元组 """ 如果是字符串:把每一个字符都当成一个新元素组合在一起,形成新元组 如果是字典:只保留字典的键 如果是其他容器:仅仅单纯的在原有数据的两边,套上小括号()即可 """ res = tuple(var1) res = tuple(var2) # res = tuple(var5) error res = tuple(var6) print(res) # 4.set 强制转换成集合 """ 集合具有无序的属性: 如果是字符串:把每一个字符都当成一个新元素组合在一起,形成新集合 如果是字典:只保留字典的键 如果是其他容器:仅仅单纯的在原有数据的两边,套上花括号{}即可 """ res = set(var1) res = set(var2) res = set(var6) print(res) """ # 括号里面不加任何值,可以转换出一个当前数据类型的值 str() list() tuple() set() dict() """ var = dict() print(var)
# ### 二级容器: 外面是一个容器类型的数据,里面的元素还是一个容器类型的元素 # 二级列表 listvar = [1,2,3,4,5,[6,7,8]] # 二级元组 tuplevar = (1,2,3,4,5,(11,12,34),90) # 二级集合 Number str tuple setvar = {1,2,3,"a","b","c",(5,6,7)} # 二级字典 dictvar = {"a":1 , "b":2 ,"c":{"d":1,"e":2}} # 四级容器 container = [1,2,3,4,5,(6,7,8,["a","b","c",{"aa":1,"bb":"王文"}])] # (6, 7, 8, ['a', 'b', 'c', {'aa': 1, 'bb': '王文'}]) res = container[-1] print(res) # ['a', 'b', 'c', {'aa': 1, 'bb': '王文'}] res2 = res[-1] print(res2) # {'aa': 1, 'bb': '王文'} res3 = res2[-1] print(res3) # 王文 res4 = res3["bb"] print(res4) # 简写 res = container[-1][-1][-1]["bb"] print(res) # 等长的二级容器 container = [(1,2,3,4) , {"a","b","c","d"}] container = [ (1,2) , [3,4]] # ### dict 强制转换成字典 """必须是等长的二级容器,并且里面的元素个数是2个""" # (1)外面是列表,里面可以是元组或者列表(推荐) listvar = [ ("a",1) , ["b",2]] dictvar = dict(listvar) print(dictvar , type(dictvar)) # 语法上可以,但是不推荐,因为集合无序,达不到想要的效果 (不推荐) listvar = [ ("a",1) , ["b",2] , {"c",3}] dictvar = dict(listvar) print(dictvar) # 字符串作为元素,有一定的局限性,仅限于2个元素才可以(不推荐) listvar = [ ("a",1) , ["b",2] , "c3"] dictvar = dict(listvar) print(dictvar) # (2)外面是元组,里面可以是元组或者列表(推荐) tuplevar = ( ("a",1) , ["b",2] ) dictvar = dict(tuplevar) print(dictvar) # (3)外面是集合,里面只能放元组 setvar = { ("a",1),("b",2) } dictvar = dict(setvar) print(dictvar) # 去掉列表中的所有重复数据 listvar = [1,2,3,3,4,4,5,6,7,"a","a","b","b","c","c"] res = set(listvar) print(res,type(res)) lst = list(res) print(lst,type(lst)) # 可以修改元组嵌套的列表中的数据; tuplevar = (1,2,3,4,5,6,["a","b","c"]) lst = tuplevar[-1] print(lst) lst[2] = "d" print(tuplevar)
2. 运算符、比较运算符、赋值运算符、逻辑运算符、身份运算符
# ### python 运算符 # (1)算数运算符: + - * / // % ** var1 = 7 var2 = 4 # + # res = var1 + var2 # print(res) # - # res = var1 - var2 # print(res) # * # res = var1 * var2 # print(res) # / 结果一定是一个小数 res = var1 / var2 res = 8 / 4 print(res) # // 地板除 res = 8 // 4 # 如果除数和被除数其中有小数,那么结果就是个小数 res = 8 // 4.0 print(res) # % 取余 res = var1 % var2 res = 87 % 6 # res = -7 % 4 # -3 + 4 = 1 # res = 7 % -4 # 3 + -4 = -1 # res = -7 % -4# 如果两边都是符号,就是单纯的在余数的前面套上负号即可 print(res) # ** 幂运算 res = 2 ** 4 print(res) # (2)比较运算符: > < >= <= == != 只会产生两个结果,要么是True真的,要么是False 假的 var1 = 10 var2 = 8 # > res = var1 > var2 print(res) # < res = var1 < var2 print(res) # >= res = 5 >= 5 print(res) # <= res = 5 <= 5 print(res) # == 比较两个值是否相等 """ = 代表的是赋值,把右边的值赋值给左边 == 代表的是比较两个值是否相等,满足返回True ,不满足返回False """ res = 5 == 5 print(res) # != 比较两个值是否不相等 res = 5 != 5 print(res)
# ### 赋值运算符 = += -= *= /= //= %= **= # = a = 5 var1 = 7 var2 = 8 # += """var1 = var1 + var2""" # var1 += var2 # print(var1) # -= """var1 = var1 - var2""" # var1 -= var2 # print(var1) # *= """var1 = var1 * var2""" # var1 *= var2 # print(var1) # /= """var1 = var1 / var2""" # var1 /= var2 # print(var1) # //= """var1 = var1 // var2""" # var1 //= var2 # print(var1) # %= """var1 = var1 % var2""" # var1 %= var2 # print(var1) # **= var1 = 3 """var1 = var1 ** var2""" var1 **= 2 print(var1) # (4)成员运算符: in 和 not in (针对于容器型数据) # str """必须是一个连续的片段""" strvar = "如果遇到你是一种错,那我宁愿一错再错" res = "你" in strvar res = "一种错" in strvar res = "宁再" in strvar print(res) # list tuple set listvar = ["罗总要","主进程","杨马志","肖波","张晓东","是滑清"] res = "主进程" in listvar tuplevar = ("罗总要","主进程","杨马志","肖波","张晓东","是滑清") res = "杨马志" not in tuplevar setvar = {"罗总要","主进程","杨马志","肖波","张晓东","是滑清"} res = "肖波123" not in setvar print(res) # dict """在字典中,in , not in 判断的是字典的键,不是值""" dictvar = {"lzy":"应该减肥","zjc":"小心被卖掉","ymz":"非常大气"} res = "应该减肥" in dictvar res = "lzy" in dictvar res = "非常大气" not in dictvar print(res)
# ### 身份运算符 is 和 is not (检测两个数据在内存当中是否是同一个值) # 整型: -5 ~ 正无穷 var1 = 90 var2 = 90 res = var1 is var2 print(res) # 浮点型 非负数 var1 = 5.89 var2 = 5.89 res = var1 is var2 print(res) # bool 两个值相同即可 var1 = True var2 = False res = var1 is not var2 print(res) # 复数 (实数+虚数 地址永远不相同, 只有虚数的情况例外) var1 = 4+3j var2 = 4+3j res = var1 is var2 print(res) # 相同的字符串 和 空元组 地址相同,其他容器类型数据地址都不相同 var1 = [90,91] var2 = [90,91] res = var1 is var2 print(res) # ### 逻辑运算符 print("<==========>") # and 逻辑与 """全真则真,一假则假""" res = True and True # 真 res = True and False # 假 res = False and False # 假 res = False and True # 假 print(res) # or 逻辑或 """一真则真,全假则假""" res = True or True # 真 res = True or False # 真 res = False or True # 真 res = False or False # 假 print(res) # not 逻辑非 res = not True res = not False print(res) # 逻辑短路 : (不能够改变最终的结果了,后面的代码不执行) """ (1) True or 表达式 (2) False and 表达式 """ True or print(111) # 不会打印111 False and print(222) # 不会打印111 res = 5 or 6 # 5 res = 5 and 6 # 6 res = not 5 # False print(res) print("<=========>") # 逻辑的优先级 # () > not > and > or res = 5 or 6 and 7 # 5 or 7 => 5 res = (5 or 6) and 7 # 5 and 7 => 7 res = not (5 or 6) and 7 # not 5 and 7 => False and 7 => False print(res) res = 1>2 or 3<4 and 5>6 # False or True and False => False or False => False res = 1<2 and 3>5 or 7<6 and 90>100 or 100<2000 # True and False or False and False or True => False or False or True => False or True => True print(res) # 注意点: res = True or False and True or False #(这种情况直接短路) res = False and True or True and True #(False and 一堆表达式,不能直接判定结果) # False or True print(res) # isinstance 判断类型的 """ int float bool complex str tuple list set dict isinstance(要判断的值,要判断的类型) 返回True真 或者 False假 # 只要有一个类型满足条件,就返回真,否则返回假 isinstance(要判断的值, (把所有可能的类型塞到元组当中) ) """ # 用法一 strvar = "我好帅" res = isinstance(strvar,str) print(res) # 用法二 listvar = ["张晓东","朱金城","石华清"] res = isinstance(listvar, (str,int,tuple,list) ) print(res) container = [1,2,3,4,[5,6,7,[8,9,10,(11,{"a":{"bb":98},"pp":{"d":"bingo"}})]]] # ”bingo” print(container[-1][-1][-1][-1]["pp"]["d"])
3. 逻辑短路与isinstance
# 逻辑短路 : (不能够改变最终的结果了,后面的代码不执行) """ (1) True or 表达式 (2) False and 表达式 """ True or print(111) # 不会打印111 False and print(222) # 不会打印111 res = 5 or 6 # 5 res = 5 and 6 # 6 res = not 5 # False print(res) print("<=========>") # 逻辑的优先级 # () > not > and > or res = 5 or 6 and 7 # 5 or 7 => 5 res = (5 or 6) and 7 # 5 and 7 => 7 res = not (5 or 6) and 7 # not 5 and 7 => False and 7 => False print(res) res = 1>2 or 3<4 and 5>6 # False or True and False => False or False => False res = 1<2 and 3>5 or 7<6 and 90>100 or 100<2000 # True and False or False and False or True => False or False or True => False or True => True print(res) # 注意点: res = True or False and True or False #(这种情况直接短路) res = False and True or True and True #(False and 一堆表达式,不能直接判定结果) # False or True print(res) # isinstance 判断类型的 """ int float bool complex str tuple list set dict isinstance(要判断的值,要判断的类型) 返回True真 或者 False假 # 只要有一个类型满足条件,就返回真,否则返回假 isinstance(要判断的值, (把所有可能的类型塞到元组当中) ) """ # 用法一 strvar = "我好帅" res = isinstance(strvar,str) print(res) # 用法二 listvar = ["张晓东","朱金城","石华清"] res = isinstance(listvar, (str,int,tuple,list) ) print(res) container = [1,2,3,4,[5,6,7,[8,9,10,(11,{"a":{"bb":98},"pp":{"d":"bingo"}})]]] # ”bingo” print(container[-1][-1][-1][-1]["pp"]["d"])
day03