python第十四章:数据类型的转换
一,str函数
str()函数是Python的内置函数,用于将其他类型的数据转换为字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
|
num = 123 print (num) print ( type (num)) numStr = str (num) print (numStr) print ( type (numStr)) flag = True print (flag) print ( type (flag)) flagStr = str (flag) print (flagStr) print ( type (flagStr)) |
运行结果:
123
<class 'int'>
123
<class 'str'>
True
<class 'bool'>
True
<class 'str'>
二,int函数
int()函数是Python的内置函数,用于将其他类型的数据转换为整数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
numStr = '456' print (numStr) print ( type (numStr)) num = int (numStr) print (num) print ( type (num)) flag = True print (flag) print ( type (flag)) flagInt = int (flag) print (flagInt) print ( type (flagInt)) numFloat = 3.1415 print (numFloat) print ( type (numFloat)) numInt = int (numFloat) print (numInt) print ( type (numInt)) |
运行结果:
456
<class 'str'>
456
<class 'int'>
True
<class 'bool'>
1
<class 'int'>
3.1415
<class 'float'>
3
<class 'int'>
说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/11/14/python-di-shi-si-zhang-shu-ju-lei-xing-de-zhuan-huan/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
三,float函数
float()函数是Python的内置函数,用于将其他类型的数据转换为浮点数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
numInt = 10 print ( "numInt值:" , numInt, ",类型:" , type (numInt)) numFloat = float (numInt) print ( "numFloat值:" , numFloat, ",类型:" , type (numFloat)) numStr = "3.1415" print ( "numStr值:" , numStr, ",类型:" , type (numStr)) numFloat = float (numStr) print ( "numFloat值:" , numFloat, ",类型:" , type (numFloat)) numBool = True print ( "numBool值:" , numBool, ",类型:" , type (numBool)) numFloat = float (numBool) print ( "numFloat值:" , numFloat, ",类型:" , type (numFloat)) |
运行结果:
numInt值: 10 ,类型: <class 'int'>
numFloat值: 10.0 ,类型: <class 'float'>
numStr值: 3.1415 ,类型: <class 'str'>
numFloat值: 3.1415 ,类型: <class 'float'>
numBool值: True ,类型: <class 'bool'>
numFloat值: 1.0 ,类型: <class 'float'>
分类:
python基础课程
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2021-11-17 vue.js 3.2.22:多选上传图片带缩略图可删除
2021-11-17 vue.js 3.2.22:自动旋转的音乐播放按钮