2019-03-19
阅读量:
372
python如何转换数据类型
dict():此函数用于将顺序元组(键,值)转换为字典。
str():用于将整数转换为字符串。
complex(real,imag)::此函数将实数转换为复数(实数,图像)数。
# Python code to demonstrate Type conversion
# using dict(), complex(), str()
# initializing integers
a = 1
b = 2
# initializing tuple
tup = (('a', 1) ,('f', 2), ('g', 3))
# printing integer converting to complex number
c = complex(1,2)
print ("After converting integer to complex number : ",end="")
print (c)
# printing integer converting to string
c = str(a)
print ("After converting integer to string : ",end="")
print (c)
# printing tuple converting to expression dictionary
c = dict(tup)
print ("After converting tuple to dictionary : ",end="")
print (c)
输出:
将整数转换为复数后:(1 + 2j)
将整数转换为字符串后:1
将元组转换为字典后:{'a':1,'f':2,'g':3}






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
0条评论