python数字操作

整数

可对整数执行加(+ )减(- )乘(* )除(/ )运算,此外还应注意运算次序。

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

‘’’


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值