整数
可对整数执行加(+ )减(- )乘(* )除(/ )运算,此外还应注意运算次序。
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
‘’’