Posted on 2011-08-31 21:14
RTY 閱讀(489)
評(píng)論(0) 編輯 收藏 引用 所屬分類:
Python
operator提供了常用的數(shù)字操作,如:
- >>> import operator
- >>> operator.add(2,5)
- 7
round函數(shù):
- >>> round(3.2344,2)
- 3.23
- >>> round(3.2354,2)
- 3.2400000000000002
- >>>
random模塊中包含了許多隨機(jī)函數(shù),如:
random() ,返回[0.0, 1.0]范圍中的浮點(diǎn)數(shù)
uniform(),返回[0.0, 1.0)范圍中的浮點(diǎn)數(shù)
randint(a,b) 返回[a,b]之間的一個(gè)隨機(jī)整數(shù)
randrange()和randint類似
choice([a,b,c])返回序列[a,b,c]中的一項(xiàng)
decimal模塊中包含了許多十進(jìn)制的整數(shù)算法:
- >>> import decimal
- >>> d1 = decimal.Decimal('2.3456')
- >>> d1.to_integral()
- Decimal("2")
- >>> d1 = decimal.Decimal('2.53456')
- >>> d1.to_integral()
- Decimal("3")
- >>>