自动类型转换

Posted weekz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自动类型转换相关的知识,希望对你有一定的参考价值。

数字类型(Number => int bool float complex)

精度从低->高 排行
bool -> int -> float -> complex
如果两个不同的类型进行运算,
默认低精度向高精度转化

# True -> int 1 False -> int 0
# bool + int
res = True + 14
print(res)


# bool + float # 把False 转换成浮点型0.0 在加上float
res = False + 4.99
print(res)

# bool + complex # 把True 转换成复数1+0j 在加上complex
res = True + 3+4j
print(res)


# int + float # 把int 转换成浮点型7.0 再加上float
res = 7 + 3.66
print(res)


# int + complex # 把int 转换成复数 3+0j 再加上complex
res = 3 + 9-2j
print(res)

# float + complex 把float 转化成复数6.91+0j 再加上complex
res = 6.91 + 11-6j
print(res)

以上是关于自动类型转换的主要内容,如果未能解决你的问题,请参考以下文章

数据类型转换之自动类型转换

Java数据类型转换:强制类型转换+自动类型转换

Java数据类型自动转换的优先顺序

数据类型转换

基本数据类型转换

引用数据类型自动类型提升自动类型转换强制类型转换