Python學習筆記一:資料類型轉換

來源:互聯網
上載者:User

一、int函數能夠
     (1)把符合數學格式的數字型字串轉換成整數
     (2)把浮點數轉換成整數,但是只是簡單的取整,而非四捨五入。

 

舉例:

 1 aa = int("124")    #Correct
2  print "aa = ", aa #result=124
3  bb = int(123.45) #correct
4  print "bb = ", bb #result=123
5  cc = int("-123.45") #Error,Can't Convert to int
6  print "cc = ",cc
7 dd = int("34a") #Error,Can't Convert to int
8 print "dd = ",dd
9 ee = int("12.3") #Error,Can't Convert to int
10 print ee
11

二、float函數將整數和字串轉換成浮點數。

 

舉例:

 1 aa = float("124")     #Correct
2 print "aa = ", aa #result = 124.0
3 bb = float("123.45") #Correct
4 print "bb = ", bb #result = 123.45
5 cc = float(-123.6) #Correct
6 print "cc = ",cc #result = -123.6
7 dd = float("-123.34") #Correct
8 print "dd = ",dd #result = -123.34
9 ee = float('123v') #Error,Can't Convert to float
10 print ee

 

三、str函數將數字轉換成字元

 

舉例:

 1 aa = str(123.4)     #Correct
2 print aa #result = '123.4'
3 bb = str(-124.a) #SyntaxError: invalid syntax
4 print bb
5 cc = str("-123.45") #correct
6 print cc #result = '-123.45'
7 dd = str('ddd') #correct
8 print dd #result = ddd
9 ee = str(-124.3) #correct
10 print ee #result = -124.3

 

 

 

{網購拿返利,購物新選擇}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.