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