求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center 汽车系统工程   模型库  
会员   
 


企业架构方法与实践
7月27-28日 北京+线上



AI智能体开发技术实践
8月6-7日 上海+线上



敏捷测试-简单而可行
8月14-15日 北京+线上
 
 
 
  Python 3 教程
1. Python 3 简介
2. Python 3 环境搭建
3. Python 3 VScode
4. Python 3 AI 编程助手
5. Python 3 基础语法
6. Python 3 基本数据类型
7. Python 3 数据类型转换
8. Python 3 解释器
9. Python 3 注释
10. Python 3 运算符
11. Python 3 数字(Number)
12. Python 3 字符串
13. Python 3 列表
14. Python 3 元组
15. Python 3 字典
16. Python 3 集合
17. Python 3 条件控制
18. Python 3 循环语句
19. Python 3 编程第一步
20. Python 3 推导式
21. Python 3 迭代器与生成器
22. Python 3 with 关键字
23. Python 3 函数
24. Python 3 lambda
25. Python 3 装饰器
26. Python 3 数据结构
27. Python 3 模块
28. Python__name__
29. Python3 输入和输出
30. Python3 File
31. Python3 OS
32. Python3 错误和异常
33. Python3 面向对象
34. Python3 命名空间和作用域
35. Python 虚拟环境的创建
36. Python 类型注解
37. Python3 标准库概览
Python3 高级编程
1. Python3 正则表达式
2. Python3 CGI编程
3. Python3 MySQL(mysql-connector)
4. Python3 MySQL(PyMySQL)
5. Python3 网络编程
6. Python3 SMTP发送邮件
7. Python3 多线程
8. Python3 XML解析
9. Python3 JSON 数据解析
10. Python3 日期和时间
11. Python3 MongoDB
12. Python3 urllib
13. Python3 uWSGI 安装配置
14. Python3 pip
15. Python3 operator
16. Python math
17. Python requests
18. Python random
19. Python OpenAI
20. Python AI 绘画
21. Python statistics
22. Python hashlib
23. Python 量化
24. Python pyecharts
25. Python selenium 库
26. Python 爬虫
27. Python Scrapy 库
28. Python Markdown
29. Python sys 模块
30. Python Pickle 模块
31. Python subprocess 模块
32. Python queue 模块
33. Python StringIO 模块
34. Python logging 模块
 
 
目录
Python 3 数据类型转换
506 次浏览
6次  

有时候,我们需要对数据内置的类型进行转换,数据类型的转换,一般情况下你只需要将数据类型作为函数名即可。

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_intnum_flo 进行相加运算,并存储在变量 num_new 中。
  • 然后查看三个变量的数据类型。
  • 在输出结果中,我们看到 num_int整型(integer)num_flo 浮点型(float)
  • 同样,新的变量 num_new 浮点型(float),这是因为 Python 会将较小的数据类型转换为较大的数据类型,以避免数据丢失。

我们再看一个实例,整型数据与字符串类型的数据进行相加:

实例

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'>

以下几个内置的函数可以执行数据类型之间的转换。这些函数返回一个新的对象,表示转换的值。

 函数值  描述
int(x [,base]) 将x转换为一个整数
float(x) 将x转换到一个浮点数
complex(real [,imag]) 创建一个复数
str(x) 将对象 x 转换为字符串
repr(x) 将对象 x 转换为表达式字符串
eval(str) 用来计算在字符串中的有效Python表达式,并返回一个对象
tuple(s) 将序列 s 转换为一个元组
list(s) 将序列 s 转换为一个列表
set(s) 转换为可变集合
dict(d) 创建一个字典。d 必须是一个 (key, value)元组序列。
frozenset(s) 转换为不可变集合
chr(x) 将一个整数转换为一个字符
ord(x) 将一个字符转换为它的整数值
hex(x) 将一个整数转换为一个十六进制字符串
oct(x) 将一个整数转换为一个八进制字符串

您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



506 次浏览
6次