小白教程
所有教程
关于
Search
172.70.100.98
172.70.100.98
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Python3 数据类型转换”的源代码
本页内容
上一节:
Python3_基本数据类型
下一节:
Python3_推导式
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:Python3 数据类型转换}}[[Category:Python 教程|7]] = Python3 数据类型转换 = 有时候,我们需要对数据内置的类型进行转换,数据类型的转换,一般情况下你只需要将数据类型作为函数名即可。 Python 数据类型转换可以分为两种: * 隐式类型转换 - 自动完成 * 显式类型转换 - 需要使用类型函数来转换 === 隐式类型转换 === 在隐式类型转换中,Python 会自动将一种数据类型转换为另一种数据类型,不需要我们去干预。 以下实例中,我们对两种不同类型的数据进行运算,较低数据类型(整数)就会转换为较高数据类型(浮点数)以避免数据丢失。 <sample title="" desc="" lang="python" hererun="1"> num_int = 123 num_flo = 1.23 num_new = num_int + num_flo print("datatype of num_int:",type(num_int)) print("datatype of num_flo:",type(num_flo)) print("Value of num_new:",num_new) print("datatype of num_new:",type(num_new)) </sample> 以上实例输出结果为: <sample title="" desc="" lang="python" hererun="1"> num_int 数据类型为: <class 'int'> num_flo 数据类型为: <class 'float'> num_new: 值为: 124.23 num_new 数据类型为: <class 'float'> </sample> 代码解析: * 实例中我们对两个不同数据类型的变量 <code>num_int</code> 和 <code>num_flo</code> 进行相加运算,并存储在变量 <code>num_new</code> 中。 * 然后查看三个变量的数据类型。 * 在输出结果中,我们看到 <code>num_int</code> 是 <code>整型(integer)</code> , <code>num_flo</code> 是 <code> 浮点型(float)</code>。 * 同样,新的变量 <code>num_new</code> 是 <code> 浮点型(float)</code>,这是因为 Python 会将较小的数据类型转换为较大的数据类型,以避免数据丢失。 我们再看一个实例,整型数据与字符串类型的数据进行相加: <sample title="" desc="" lang="python" hererun="1"> num_int = 123 num_str = "456" print("Data type of num_int:",type(num_int)) print("Data type of num_str:",type(num_str)) print(num_int+num_str) </sample> 以上实例输出结果为: <sample title="" desc="" lang="python" hererun="1"> num_int 数据类型为: <class 'int'> num_str 数据类型为: <class 'str'> Traceback (most recent call last): File "/xiaobai-test/test.py", line 7, in <module> print(num_int+num_str) TypeError: unsupported operand type(s) for +: 'int' and 'str' </sample> 从输出中可以看出,整型和字符串类型运算结果会报错,输出 TypeError。 Python 在这种情况下无法使用隐式转换。 但是,Python 为这些类型的情况提供了一种解决方案,称为显式转换。 === 显式类型转换 === 在显式类型转换中,用户将对象的数据类型转换为所需的数据类型。 我们使用 int()、float()、str() 等预定义函数来执行显式类型转换。 int() 强制转换为整型: <sample title="" desc="" lang="python" hererun="1"> x = int(1) # x 输出结果为 1 y = int(2.8) # y 输出结果为 2 z = int("3") # z 输出结果为 3 </sample> float() 强制转换为浮点型: <sample title="" desc="" lang="python" hererun="1"> x = float(1) # x 输出结果为 1.0 y = float(2.8) # y 输出结果为 2.8 z = float("3") # z 输出结果为 3.0 w = float("4.2") # w 输出结果为 4.2 </sample> str() 强制转换为字符串类型: <sample title="" desc="" lang="python" hererun="1"> x = str("s1") # x 输出结果为 's1' y = str(2) # y 输出结果为 '2' z = str(3.0) # z 输出结果为 '3.0' </sample> 整型和字符串类型进行运算,就可以用强制类型转换来完成: <sample title="" desc="" lang="python" hererun="1"> num_int = 123 num_str = "456" print("num_int 数据类型为:",type(num_int)) print("类型转换前,num_str 数据类型为:",type(num_str)) num_str = int(num_str) # 强制转换为整型 print("类型转换后,num_str 数据类型为:",type(num_str)) num_sum = num_int + num_str print("num_int 与 num_str 相加结果为:",num_sum) print("sum 数据类型为:",type(num_sum)) </sample> 以上实例输出结果为: <sample title="" desc="" lang="python" hererun="1"> num_int 数据类型为: <class 'int'> 类型转换前,num_str 数据类型为: <class 'str'> 类型转换后,num_str 数据类型为: <class 'int'> num_int 与 num_str 相加结果为: 579 sum 数据类型为: <class 'int'> </sample> 以下几个内置的函数可以执行数据类型之间的转换。这些函数返回一个新的对象,表示转换的值。 {| class="table table-striped table-hover" ! 函数 ! 描述 |- | [[python func int|int(x [,base])]] | 将x转换为一个整数 |- | [[python func float|float(x)]] | 将x转换到一个浮点数 |- | [[python func complex|complex(real [,imag])]] | 创建一个复数 |- | [[python func str|str(x)]] | 将对象 x 转换为字符串 |- | [[python func repr|repr(x)]] | 将对象 x 转换为表达式字符串 |- | [[python func eval|eval(str)]] | 用来计算在字符串中的有效Python表达式,并返回一个对象 |- | [[python3 func tuple|tuple(s)]] | 将序列 s 转换为一个元组 |- | [[python3 att list list|list(s)]] | 将序列 s 转换为一个列表 |- | [[python func set|set(s)]] | 转换为可变集合 |- | [[python func dict|dict(d)]] | 创建一个字典。d 必须是一个 (key, value)元组序列。 |- | [[python func frozenset|frozenset(s)]] | 转换为不可变集合 |- | [[python func chr|chr(x)]] | 将一个整数转换为一个字符 |- | [[python func ord|ord(x)]] | 将一个字符转换为它的整数值 |- | [[python func hex|hex(x)]] | 将一个整数转换为一个十六进制字符串 |- | [[python func oct|oct(x)]] | 将一个整数转换为一个八进制字符串 |}
返回至“
Python3 数据类型转换
”。
上一节:
Python3_基本数据类型
下一节:
Python3_推导式