GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.runoob.com — Python3 数据类型转换 | 菜鸟教程
Python 3 教程
Python3 教程
Python3 简介
Python3 环境搭建
Python3 Pycharm
Python3 VScode
Python3 基础语法
Python3 基本数据类型
Python3 数据类型转换
Python3 解释器
Python3 注释
Python3 运算符
Python3 数字(Number)
Python3 字符串
Python3 列表
Python3 元组
Python3 字典
Python3 集合
Python3 条件控制
Python3 循环语句
Python3 编程第一步
Python3 推导式
Python3 迭代器与生成器
Python3 with
Python3 函数
Python3 lambda
Python3 装饰器
Python3 数据结构
Python3 模块
Python __name__
Python3 输入和输出
Python3 File
Python3 OS
Python3 错误和异常
Python3 面向对象
Python3 命名空间/作用域
Python 虚拟环境的创建
Python 类型注解
Python3 标准库概览
Python3 实例
Python 测验
Python3 高级教程Python3 正则表达式 Python3 CGI编程 Python3 MySQL(mysql-connector) Python3 MySQL(PyMySQL) Python3 网络编程 Python3 SMTP发送邮件 Python3 多线程 Python3 XML 解析 Python3 JSON Python3 日期和时间 Python3 内置函数 Python3 MongoDB Python3 urllib Python uWSGI 安装配置 Python3 pip Python3 operator Python math Python requests Python random Python OpenAI Python 有用的资源 Python AI 绘画 Python statistics Python hashlib Python 量化 Python pyecharts Python selenium 库 Python 爬虫 Python Scrapy 库 Python Markdown Python sys 模块 Python Pickle 模块 Python subprocess 模块 Python queue 模块 Python StringIO 模块 Python logging 模块 Python datetime 模块 Python re 模块 Python csv 模块 Python threading 模块 Python asyncio 模块 Python PyQtPython3 数据类型转换有时候,我们需要对数据内置的类型进行转换,数据类型的转换,一般情况下你只需要将数据类型作为函数名即可。 Python 数据类型转换可以分为两种:
隐式类型转换在隐式类型转换中,Python 会自动将一种数据类型转换为另一种数据类型,不需要我们去干预。 以下实例中,我们对两种不同类型的数据进行运算,较低数据类型(整数)就会转换为较高数据类型(浮点数)以避免数据丢失。 实例
num_int = 123 num_flo = 1.23 num_new = num_int + num_flo print("num_int 数据类型为:",type(num_int)) print("num_flo 数据类型为:",type(num_flo)) print("num_new 值为:",num_new) print("num_new 数据类型为:",type(num_new)) 以上实例输出结果为: num_int 数据类型为: <class 'int'> num_flo 数据类型为: <class 'float'> num_new: 值为: 124.23 num_new 数据类型为: <class 'float'> 代码解析:
我们再看一个实例,整型数据与字符串类型的数据进行相加: 实例
num_int = 123 num_str = "456" print("num_int 数据类型为:",type(num_int)) print("num_str 数据类型为:",type(num_str)) print(num_int+num_str) 以上实例输出结果为: num_int 数据类型为: <class 'int'>
num_str 数据类型为: <class 'str'>
Traceback (most recent call last):
File "/runoob-test/test.py", line 7, in <module>
print(num_int+num_str)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
从输出中可以看出,整型和字符串类型运算结果会报错,输出 TypeError。 Python 在这种情况下无法使用隐式转换。 但是,Python 为这些类型的情况提供了一种解决方案,称为显式转换。 显式类型转换在显式类型转换中,用户将对象的数据类型转换为所需的数据类型。 我们使用 int()、float()、str() 等预定义函数来执行显式类型转换。 int() 强制转换为整型: 实例
x = int(1) # x 输出结果为 1 y = int(2.8) # y 输出结果为 2 z = int("3") # z 输出结果为 3 float() 强制转换为浮点型: 实例
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 str() 强制转换为字符串类型: 实例
x = str("s1") # x 输出结果为 's1' y = str(2) # y 输出结果为 '2' z = str(3.0) # z 输出结果为 '3.0' 整型和字符串类型进行运算,就可以用强制类型转换来完成: 实例
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)) 以上实例输出结果为: num_int 数据类型为: <class 'int'> 类型转换前,num_str 数据类型为: <class 'str'> 类型转换后,num_str 数据类型为: <class 'int'> num_int 与 num_str 相加结果为: 579 sum 数据类型为: <class 'int'> 以下几个内置的函数可以执行数据类型之间的转换。这些函数返回一个新的对象,表示转换的值。
AI 思考中...
| ||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |