# 数字类型int与float
# 2.1 定义
# 1、定义:
# 1.1 整型int的定义
age=10 # 本质age = int(10)
# 1.2 浮点型float的定义
salary=3000.3 # 本质salary=float(3000.3)
# 注意:名字+括号的意思就是调用某个功能,比如
# print(...)调用打印功能
# int(...)调用创建整型数据的功能
# float(...)调用创建浮点型数据的功能
# 2.2 类型转换
# 1、数据类型转换
# 1.1 int可以将由纯整数构成的字符串直接转换成整型,若包含其他任意非整数符号,则会报错
s = '123'
res = int(s)
res,type(res)
# (123, <class 'int'>)
int('12.3') # 错误演示:字符串内包含了非整数符号.
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# ValueError: invalid literal for int() with base 10: '12.3'
# 1.2 进制转换
# 十进制转其他进制
bin(3)
'0b11'
oct(9)
'0o11'
hex(17)
'0x11'
# 其他进制转十进制
int('0b11',2)
# 3
int('0o11',8)
# 9
int('0x11',16)
# 17
# 1.3 float同样可以用来做数据类型的转换
s = '12.3'
res=float(s)
res,type(res)
# (12.3, <class 'float'>)
Python基础10:基本数据类型(补充)
最新推荐文章于 2023-07-22 10:55:24 发布