iT邦幫忙

2021 iThome 鐵人賽

DAY 4
0
自我挑戰組

Python 基礎學習之旅系列 第 4

Day4- 整數與變數 和 型態轉換

  • 分享至 

  • twitterImage
  •  

數字延伸如何使用將變數指定值進行運算,並將不同型態轉成字串與浮點數。

整數與變數

結合算數運算子與賦值 ex: += 、 -= 、 *= 、 /= 、 //= 、 %=
同時產生商數(捨去的)和餘數: divmod(9, 5)
可以混用整數與浮點數

https://ithelp.ithome.com.tw/upload/images/20210917/20130598xYORX81EiX.png

型態轉換

數字常見的型態轉換為整數與浮點數

  • 整數 Integer
    int()函式將資料型別轉換成整數
    如果字串代表非十進制整數,可以加入底數
    int() 轉化的字串不可以包含非數字
    布林與整數或浮點數混合使用,False 被當成 0,True 被當成 1
    https://ithelp.ithome.com.tw/upload/images/20210917/20130598QntGGjdVID.png

int() 轉化的字串無法處理小數點或指數的字串

https://ithelp.ithome.com.tw/upload/images/20210917/20130598YYCEyedtx9.png

  • 浮點數 Float
    float() 函式將其他資料型別轉換成浮點數,與整數用法一樣差別是轉換成浮點數。

https://ithelp.ithome.com.tw/upload/images/20210917/20130598qZo1uQLH7n.png


上一篇
Day-3 Python Bool 整數 整數運算
下一篇
Day-5 註解與斷行
系列文
Python 基礎學習之旅12
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言