# python 轉換資料類型 ###### tags: `python` `int` `float` `str` `list` `tuple` --- ![](https://i.imgur.com/mRnVpTA.png) ## 練習 ## 轉換整數 --- pw = input('輸入密碼:') print(f'your pass word is {pw}') print(type(pw)) #int(pw) #type(int(pw)) print(type(int(pw))) ![](https://i.imgur.com/g8cerAN.png) 結果 ![](https://i.imgur.com/o4Md8HP.png) --- ## 轉換浮點數 --- #float() num1 = 1 str1 = '10' print(type(float(num1))) #float轉換成浮點 print(float(num1)) #輸出1.0 print(float(str1)) #輸出10.0 --- ![](https://i.imgur.com/phMb99z.png) 結果 ![](https://i.imgur.com/XyZsjVV.png) --- ### 轉換成字串 num1 = 1 str1 = '10' type(str(num1)) print(type(str(num1))) print(str(num1)) print(str(str1)) ![](https://i.imgur.com/4h4sXCD.png) 結果 ![](https://i.imgur.com/j2z2I86.png) --- ## 轉換成元組 #tuple() list1 = [10 , 20 , 30] type(tuple(list1)) print(type(tuple(list1))) print(tuple(list1)) ![](https://i.imgur.com/tOAOGlA.png) 結果 ![](https://i.imgur.com/lYZub40.png) --- ## 序列轉換成列表 #list() t1 = (100 , 200 , 300) print(type(list(t1))) print(list(t1)) ![](https://i.imgur.com/LKRkct0.png) 結果 ![](https://i.imgur.com/tc3HhRl.png) --- ## 計算在字符串中的有效python標達式,並返回一個對象 #eval() str2 = '1' str3 = '1.1' str4 = '(1000, 2000 , 3000)' str5 = '[1000, 2000 , 3000]' print(type(eval(str2))) print(type(eval(str3))) print(type(eval(str4))) print(type(eval(str5))) ![](https://i.imgur.com/VSN0I39.png) 結果 ![](https://i.imgur.com/KOwMIla.png) --- ![](https://i.imgur.com/RJpJ1CA.png)