003.Python数据类型转换⼀⾃动类型转换
(针对于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)
2.1 把数据强制转换成整型 int
(整型浮点型布尔类型纯数字字符串)
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))
python代码转换[root@node10 python]# python3 test.py
1 <class'int'>
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'
2.2 float (整型浮点型布尔类型纯数字字符串) 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)
2.3 bool类型
( 容器类型数据 / 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)
3.1 str 强转成字符串类型
( 容器类型数据 / 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'>
3.2 list 列表
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']
3.3 tuple 元组
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')
3.4 set 集合
set 集合强转规律
1. 如果是字符串:把字符串中的每⼀个字符当成新的元素放到集合中
2. 如果是其他数据:就是单纯的把原标识符换成{} 变成集合即可
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'>
3.5 ⼆级容器
外⾯是⼀个容器类型的数据,⾥⾯的元素还是⼀个容器类型数据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]]
3.6 字典的强制类型转换
外⾯是列表,⾥⾯是列表或元组或字符串
listvar = [["a",1],("b",2),"c123"] # 字符串慎⽤如果值是多个,有局限性listvar = [["a",1],("b",2)] # 推荐 ***
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论