ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

六. 数据类型转换

2022-02-01 11:59:09  阅读:154  来源: 互联网

标签:类型转换 int type float datatype print 数据


6.1 隐式类型转换

在隐式类型转换中,Python 会自动将一种数据类型转换为另一种数据类型,不需要干预。

a = 123
b = 1.23

c = a + b

print("datatype of a:", type(a))
print("datatype of b:", type(b))

print("Value of c:", c)
print("datatype of c:", type(c))

结果:

datatype of a: <class 'int'>
datatype of b: <class 'float'>
Value of c: 124.23
datatype of c: <class 'float'>

6.2 显式类型转换

a = int(11)
b = int(2.3)
c = int("34")

print("a=",a)
print("b=",b)
print("c=",c)

结果:

a= 11
b= 2
c= 34

x = float(122)
y = float(2.2)
z = float("9")
w = float("4.8")

print(x)
print(y)
print(z)
print(w)

结果:

122.0
2.2
9.0
4.8

a = str("sss1")
b = str(32)
c = str(23.3)

print("a = ", a)
print("b = ", b)
print("c = ", c)

结果:

a =  sss1
b =  32
c =  23.3

标签:类型转换,int,type,float,datatype,print,数据
来源: https://blog.csdn.net/qq_24430629/article/details/122763343

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有