1. 程式人生 > >python 資料型別 轉換

python 資料型別 轉換

 轉換為整數
# a = int(3.14)#3
# print(a)
# # 引數1:需要轉換的資料
# # base:資料的進位制型別,預設為十進位制
# a = int('123', base=8)#83
# print(a)
# a = int('abc', base=16)#2748
# print(a)
# 浮點
a = float(250)#250.0

print(a)

# 字串
a = str(123)
print(a,type(a))#123 <class 'str'>

# 列表
a = list('hello')
b=''.join(a)
print(b,type(b))#hello 
<class 'str'> print(a,type(a))#['h', 'e', 'l', 'l', 'o'] <class 'list'> c=(1,2,3) print(type(c))#<class 'tuple'> a = list((1, 2, 3))#元組轉列表 print(a,type(a)) d={1,2,3} print(type(d))#<class 'set'> a = list({1, 2, 3}) print(a,type(a))#[1, 2, 3] <class 'list'>

 

# 可以轉換不會報錯,但是隻保留了鍵['
name', 'age'] # a = list({'name': 'ergou', 'age': 18}) # print(a) # # # 元組 # a = tuple([1, 2, 3]) # print(a)#(1, 2, 3) # 集合 # a = set([1, 2, 3])#列表轉集合{1, 2, 3};集合沒有下標 # print(a) # 字典 # lt = [('name', 'dahua'), ('age', 18)] # print(type(lt)) # a = dict(lt)#列表轉字典 # print(a, type(a))#{'name': 'dahua', '
age': 18} <class 'dict'> #