整數
可對整數執行加(+ )減(- )乘(* )除(/ )運算,此外還應注意運算次序。
2**3 # **表示冪
# 8
(2+3)*4
# 20
浮點數
Python將帶小數點的數字都稱爲浮點數。正常境況下輸入浮點數運算即可。但需要注意的是,結果包含的小數位數可能是不確定的:
0.2+0.1
# 0.30000000000000004
使用函數 str() 避免類型錯誤
當數字和字符在一起時要注意之間的轉化:
age = 1996
s = "我出生於" + str(age) + "年"
print(s)
# 我出生於1996年
# 錯誤示例
age = 1996
s = "我出生於" + age + "年"
print(s)
'''
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-47-e7d56eb09693> in <module>()
1 age = 1996
----> 2 s = "我出生於" + age + "年"
3 print(s)
TypeError: can only concatenate str (not "int") to str
'''
concatenate str (not “int”) to str
‘’’