iT邦幫忙

0

小前端的數據分析之路-python的運算符

資料與資料間的運算需要透過各種運算符,這篇記錄python運算符的學習筆記:

一、input命令
如果要寫一個由使用者輸入資料的欄位,可以用input命令。

debt = input("請輸入債務總和: ")
print(debt)

https://ithelp.ithome.com.tw/upload/images/20200811/20129290ETPdWgWoud.png

使用者在上面輸入完資料按下enter鍵即印出資料。

二、算術運算符

運算符 意思
" + " 使運算符兩側運算元相加。
" - " 使運算符兩側運算元相減。
" * " 使運算符兩側運算元相乘。
" / " 使運算符兩側運算元相除。
" % " 取得相除後的餘數。
" // " 取得相除後的商數。
" ** " 計算(運算元1)的(運算元2)次方。

https://ithelp.ithome.com.tw/upload/images/20200811/20129290pmipmRLaUt.png

以上有關相除的運算符/、%、//,運算符右側的運算元不能為0,否則會出現ZeroDivisionError的錯誤。
https://ithelp.ithome.com.tw/upload/images/20200811/20129290DuTsRaECgA.png

不同資料型別的資料也不能進行運算。
https://ithelp.ithome.com.tw/upload/images/20200811/20129290DdK3imLNqf.png

出現了TypeError: can only concatenate str (not "int") to str的錯誤。

若是字串資料型別相加,則會把兩段文字接起來:
https://ithelp.ithome.com.tw/upload/images/20200811/20129290q3iYKN331Q.png

字串型別資料只能相加,放在其他運算符則會出錯。

下班了,今天晚餐吃些什麼呢?


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言