基本数据类型的相互转换

# 基本数据类型自动转换
# 会朝着更精确的方向转换
# 1. 自动转换
# 2. 手动转换

"""
-自动类型转换:当2个不同类型的数据进行运算的时候,默认向更高精度转换
			  数据类型精度从低到高:bool int float complex

-强制类型转换:
	-->Number 部分
	int :            浮点型 布尔类型  纯数字字符串
	float:    整型          布尔类型  纯数字字符串
	complex:  整型   浮点型 布尔类型  纯数字字符串
	bool: 	  ( 容器类型数据  /  Number类型数据 都可以 )

	-->容器类型部分
	str:	  ( 容器类型数据  /  Number类型数据 都可以 )
	list:  	  字符串 元祖 集合 字典
	tuple: 	  字符串 列表 集合 字典
	set:   	  字符串 列表 元祖 字典  (注意:相同的值,只会保留一份)
	dict:	  使用二级列表 或 二级元祖 或 二级集合(注意:里面放元祖)
"""

a = 12
b = 13.1
c = True
d = 5+6j
e = '92.5'
f = [1,2,3,4,5,6]
g = (9,8,7,6,5,4)
h = {147,258,369}
i = {"name":'ZXX',"age":22}

# int
# print(int(a),type(a)) #"""能"""
# print(int(b),type(b)) #"""能"""
# print(int(c),type(c)) #"""能"""
# print(int(d),type(d))
# print(int(e),type(e)) #"""只能是纯整数字符串才行"""
# print(int(f),type(f))
# print(int(g),type(g))
# print(int(h),type(h))
# print(int(i),type(i))

# float
# print(float(a),type(a)) #"""能"""
# print(float(b),type(b)) #"""能"""
# print(float(c),type(c)) #"""能"""
# print(float(d),type(d))
# print(float(e),type(e)) #"""只能是纯整数和浮点数字符串才行"""
# print(float(f),type(f))
# print(float(g),type(g))
# print(float(h),type(h))
# print(float(i),type(i))

# True
# False = 0
# False = 0.0
# False = False
# False = 0j
# False = ""
# False = []
# False = ()
# False = set()
# False = {}
# 除此之外都是True

# complex
# print(complex(a),type(a)) #"""能"""
# print(complex(b),type(b)) #"""能"""
# print(complex(c),type(c)) #"""能"""
# print(complex(d),type(d)) #"""能"""
# print(complex(e),type(e)) #"""只能是纯整数和浮点数字符串才行"""
# print(complex(f),type(f))
# print(complex(g),type(g))
# print(complex(h),type(h))
# print(complex(i),type(i))

# list
# print(list(a),type(a)) # 'int' object is not iterable
# print(list(b),type(b)) # 'float' object is not iterable
# print(list(c),type(c)) # 'bool' object is not iterable
# print(list(d),type(d)) # 'complex' object is not iterable
# print(list(e),type(e)) #"""能"""
# print(list(f),type(f)) #"""能"""
# print(list(g),type(g)) #"""能"""
# print(list(h),type(h)) #"""能"""
# print(list(i),type(i)) #"""只能转化键 , 而值不能转化"""

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值