#类型转换 #转换为整型 base :指定参数的进制类型 a = int('123456', base = 8) print(a, type(a)) #42798 <class 'int'> b = int(True) print(b) # 1 本身没有什么意义 #浮点数(float) c = float(123456) print(c) # 123456.0 #字符串(str) d = str(123456) print(d) # 123456 这里打印的是字符串 #列表(list) e = list('123456') print(e) # ['1', '2', '3', '4', '5', '6'] e1 = list(' I love you ') print(e1) # [' ', 'I', ' ', 'l', 'o', 'v', 'e', ' ', 'y', 'o', 'u', ' '] e2 = list((1,2,3)) print(e2) # [1, 2, 3] 这里把元组转换成列表 e3 = list({1,2,3}) print(e3) # [1, 2, 3] 这里把集合转换成列表 e4 = list({'姓名:':'梦江','年龄':'18','职业:':'工程师'}) print(e4,type(e4)) # ['姓名:', '年龄', '职业:'] <class 'list'> #元组(tuple) f = tuple([1,2,3,4,5,6]) print(f) # (1, 2, 3, 4, 5, 6) #列表转换为元组 #集合(set) g = set([1,2,3,4,5,6,]) print(g, type(g)) # {1, 4, 5, 6, 23} <class 'set'> #字典(dict) dict1 = {'姓名:':'梦江','年龄':'18','职业:':'工程师'} K = dict(dict1) print(K, type(K)) # {'姓名:': '梦江', '年龄': '18', '职业:': '工程师'} <class 'dict'>
1.3类型转换
猜你喜欢
转载自blog.csdn.net/xc_lmh/article/details/81146088
今日推荐
周排行