(针对于Number类型) bool float int complex
当Number不同的数据类型进行运算的时候,默认向更高精度转化
精度从低到高顺序:bool -> int -> float ->complex
True 默认转化是1
False 默认转化是0
# (1) bool + int res = True + 89 print(res) # (2) bool + float res = True + 55.78 print(res) # (3) bool + complex res = False + 2-4j print(res) # (4) int + float res = 31 + 4.1 print(res) # (5) int + complex res = 17 + 4-7j print(res) # (6) float + complex res = 8.12 + 3+5j print(res)
[root@node10 python]# python3 test.py
90 56.78 (2-4j) 35.1 (21-7j) (11.12+5j)
Number => (int float bool complex)
(整型 浮点型 布尔类型 纯数字字符串)
var1 = 13 var2 = 99.99 var3 = True var3_1 = False var4 = 4+1j var5 = "123321" var6 = "你好123" res = int(var2) # True 强转整型是1 False 强转整型是0 res = int(var3)
print(res,type(res)) res = int(var3_1) print(res) res = int(var5) print(res,type(res))
[root@node10 python]# python3 test.py
1 <class ‘int‘> 0 123321 <class ‘int‘>
字符型字符串不能转化
var1 = 13 var2 = 99.99 var3 = True var3_1 = False var4 = 4+1j var5 = "123321" var6 = "你好123" res = int(var6) print(res,type(res))
[root@node10 python]# python3 test.py
Traceback (most recent call last): File "test.py", line 9, in <module> res = int(var6) ValueError: invalid literal for int() with base 10: ‘你好123‘
var1 = 13 var2 = 99.99 var3 = True var3_1 = False var4 = 4+1j var5 = "123321" var6 = "你好123" res = float(var1) print(res) res = float(var3) # 加上.0 成为小数 print(res) res = float(var3_1) # 0.0 print(res) res = float(var5) #123321.0 print(res)
[root@node10 python]# python3 test.py
13.0 1.0 0.0 123321.0
[root@node10 python]# python3 test.py
var1 = 13 var2 = 99.99 var3 = True var3_1 = False var4 = 4+1j var5 = "123321" var6 = "你好123" res = float(var4) #can‘t convert complex to float print(res)
[root@node10 python]# python3 test.py
Traceback (most recent call last): File "test.py", line 9, in <module> res = float(var4) #can‘t convert complex to float TypeError: can‘t convert complex to float
complex (整型 浮点型 布尔类型 纯数字字符串 复数)
var1 = 13 var2 = 99.99 var3 = True var3_1 = False var4 = 4+1j var5 = "123321" var6 = "你好123" res = complex(var1) # 13 + 0j print(res) res = complex(var2) #(99.99+0j) print(res) res = complex(var3) #(1+0j) print(res) res = complex(var3_1) #0j print(res) res = complex(var5) #(123321+0j) print(res)
[root@node10 python]# python3 test.py
(13+0j) (99.99+0j) (1+0j) 0j (123321+0j)
( 容器类型数据 / Number类型数据 都可以,要么True要么False)
布尔类型为假的十种情况: 0,0.0,False,0j,"",[],(),set(),{},None None 是系统的一个关键字 表示空的,什么也没有,一般做初始值
var1 = 13 var2 = 99.99 var3 = True var3_1 = False var4 = 4+1j var5 = "123321" var6 = "你好123" res = bool(var6) print(res,type(res)) res = bool(var4) print(res,type(res)) res = bool([1,2,3]) print("<!!!>") print(res) res = None print(res,type(res))
[root@node10 python]# python3 test.py
True <class ‘bool‘> True <class ‘bool‘> <!!!> True None <class ‘NoneType‘>
(str list tuple set dict)
( 容器类型数据 / Number类型数据 都可以 )
字符串强转规律: 就是单纯的在原数据的两侧加上引号
var1 = "快乐每一天" var2 = [1,2,3] var3 = (4,5,6) var4 = {"美丽","店铺名个人"} var5 = {"a":1,"b":2,"c":3} var6 = 123 res = str(var2) print(repr(res),type(res)) res = str(var3) print(repr(res),type(res)) res = str(var5) print(repr(res),type(res)) res = str(var6) # print(res,type(res)) # repr 以字符串形式原型化输出数据 (想看到引号用repr转化) print(repr(res),type(res))
[root@node10 python]# python3 test.py
‘[1, 2, 3]‘ <class ‘str‘> ‘(4, 5, 6)‘ <class ‘str‘> "{‘a‘: 1, ‘b‘: 2, ‘c‘: 3}" <class ‘str‘> ‘123‘ <class ‘str‘>
list 列表强转规律:
如果是字符串:把字符串中的每一个字符当成新的元素放到列表中,如果是其他数据:就是单纯的把原标识符换成[]
var1 = "快乐每一天" var2 = [1,2,3] var3 = (4,5,6) var4 = {"美丽","店铺名个人"} var5 = {"a":1,"b":2,"c":3} var6 = 123 res = list(var1) #[‘快‘, ‘乐‘, ‘每‘, ‘一‘, ‘天‘] print(res) res = list(var3) print(res) res = list(var4) print(res) res = list(var5) #[‘a‘, ‘b‘, ‘c‘] 强转字典时,保留键,舍去值 # res = list(var6) # error print(res)
[root@node10 python]# python3 test.py
[‘快‘, ‘乐‘, ‘每‘, ‘一‘, ‘天‘] [4, 5, 6] [‘店铺名个人‘, ‘美丽‘] [‘a‘, ‘b‘, ‘c‘]
tuple 元组强转规律
如果是字符串:把字符串中的每一个字符当成新的元素放到元组中
如果是其他数据:就是单纯的把原标识符换成() 变成元组即可
var1 = "快乐每一天" var2 = [1,2,3] var3 = (4,5,6) var4 = {"美丽","店铺名个人"} var5 = {"a":1,"b":2,"c":3} var6 = 123 res = tuple(var1) #(‘快‘, ‘乐‘, ‘每‘, ‘一‘, ‘天‘) print(res) res = tuple(var2) print(res) res = tuple(var5) #(‘a‘, ‘b‘, ‘c‘) #强转字典时,保留键,舍去值 print(res)
[root@node10 python]# python3 test.py
(‘快‘, ‘乐‘, ‘每‘, ‘一‘, ‘天‘) (1, 2, 3) (‘a‘, ‘b‘, ‘c‘)
set 集合强转规律
var1 = "快乐每一天" var2 = [1,2,3] var3 = (4,5,6) var4 = {"美丽","店铺名个人"} var5 = {"a":1,"b":2,"c":3} var6 = 123 res = set(var1) #因为无序,字符串被打散 print(res) res = set(var2) # {1,2,3} print(res) res = set(var5) #强转字典时,保留键,舍去值,键值顺序被打乱 print(res)
[root@node10 python]# python3 test.py
{‘快‘, ‘每‘, ‘一‘, ‘乐‘, ‘天‘} {1, 2, 3} {‘c‘, ‘a‘, ‘b‘}
过滤列表重复数据
listvar = [1,2,3,4,5,5,6,7,6] container = set(listvar) print(container) container = list(container) print(container,type(container))
[root@node10 python]# python3 test.py
{1, 2, 3, 4, 5, 6, 7} [1, 2, 3, 4, 5, 6, 7] <class ‘list‘>
外面是一个容器类型的数据,里面的元素还是一个容器类型数据
listvar = [1,2,3,(4,5,6)] # 二级容器 print(listvar)
[root@node10 python]# python3 test.py
[1, 2, 3, (4, 5, 6)]
二级元祖
tup = (3,5,(7,8,9)) print(tup)
[root@node10 python]# python3 test.py
(3, 5, (7, 8, 9))
二级集合 (只能存放元组)
setvar = {1,2,3,(11,22,33)} print(setvar)
[root@node10 python]# python3 test.py
{1, 2, 3, (11, 22, 33)}
二级字典
dictvar = {‘a‘:{‘c‘:333},‘b‘:2} # 取出333 print(dictvar[‘a‘][‘c‘])
[root@node10 python]# python3 test.py
333
# 四级容器
container = [1,2,3,(4,5,6,{"a":1,"b":[7,8,9]}),90] # 取出9 res = container[-2][-1]["b"][-1] print(res)
[root@node10 python]# python3 test.py
9
等长的二级容器
(1) 里面每个元素都是容器类型数据
(2) 每个容器类型数据的元素个数都相同
container = [(1,2,3),[4,5,6]]
外面是列表,里面是列表或元组或字符串
listvar = [["a",1],("b",2),"c123"] # 字符串慎用 如果值是多个,有局限性 listvar = [["a",1],("b",2)] # 推荐 *** res = dict(listvar) print(res)
[root@node10 python]# python3 test.py
{‘a‘: 1, ‘b‘: 2}
外面是元组,里面是列表元组或字符串
tuplevar = (["c",11],("d",23)) # 推荐 *** res = dict(tuplevar) print(res)
[root@node10 python]# python3 test.py
{‘c‘: 11, ‘d‘: 23}
例外:如果往列表或者元组容器放集合,语法上不报错,但情况出乎意料,达不到想要效果
container = dict([{"a",1},{"b",2}]) # 不推荐使用 print(container)
[root@node10 python]# python3 test.py
{‘a‘: 1, ‘b‘: 2}
外面是集合,里面是元组或字符串
setvar = {(‘a‘,1),(‘b‘,2),"c3"} # 必须放入不可变数据,即可哈希 res = dict(setvar) print(res)
[root@node10 python]# python3 test.py
{‘b‘: 2, ‘a‘: 1, ‘c‘: ‘3‘}
int() float() bool() complex()
str() list() tuple() set() dict()
这些函数在进行强转时,都默认转化成当前的数据类型
用这样的方式也可以初始化一个变量
res = int() res = list() print(res)
[root@node10 python]# python3 test.py
[]
原文:https://www.cnblogs.com/zyxnhr/p/12254410.html