1. 浮點(diǎn)數(shù)的四舍五入
round(value, ndigits),能夠解決浮點(diǎn)數(shù)的四舍五入,但是仍然需要注意區(qū)別。
>>> round(1.23, 1)
1.2
>>> round(1.27, 1)
1.3
>>> round(-1.27, 1)
-1.3
>>> round(1.25361,3)
1.254
>>>
當(dāng)一個(gè)值剛好在兩個(gè)邊界的中間的時(shí)候, round 函數(shù)返回離它最近的偶數(shù)。 也就是說(shuō),對(duì)1.5或者2.5的舍入運(yùn)算都會(huì)得到2。
>>> round(1.5)
2
>>>
>>> round(2.5)
2
當(dāng)精度為負(fù)數(shù)時(shí):
>>> a = 1627731
>>> round(a, -1)
1627730
>>>
>>> round(a, -2)
1627700
>>>
>>> round(a, -3)
1628000
2. 執(zhí)行精確的浮點(diǎn)數(shù)運(yùn)算
我們?cè)谶\(yùn)算時(shí)經(jīng)常會(huì)出現(xiàn)以下情況。
>>> a = 4.2
>>> b=2.1
>>> a+b
6.300000000000001
>>> (a+b) == 6.3
False
所以我們?yōu)榱司_,可以使用 decimal 模塊:
>>> from decimal import Decimal
>>> a = Decimal('4.2')
>>> b = Decimal('2.1')
>>> a + b
Decimal('6.3')
>>> print(a + b)
6.3
>>> (a + b) == Decimal('6.3')
True
3. 數(shù)字的格式化輸出
再格式化單個(gè)數(shù)字時(shí),我們會(huì)和字符串一樣,使用 format() 函數(shù):
當(dāng)指定數(shù)字的位數(shù)后,結(jié)果值會(huì)根據(jù) round() 函數(shù)同樣的規(guī)則進(jìn)行四舍五入后返回
>>> x = 1234.56789
>>> 到小數(shù)點(diǎn)后兩位
>>> format(x, '0.2f')
'1234.57'
>>>
>>> 右對(duì)齊
>>> format(x, '>10.1f')
' 1234.6'
>>>
>>> 使用#補(bǔ)齊
>>> format(x, '#>10.1f')
'####1234.6'
>>>
>>> 左對(duì)齊
>>> format(x, '<10.1f')
'1234.6 '
>>>
>>> 居中
>>> format(x, '^10.1f')
' 1234.6 '
>>> 千位分離器
>>> x = 123456789.12345
>>>
>>> format(x, ',')
'123,456,789.12345'
>>>
>>> format(x, '0,.1f')
'123,456,789.1'
指數(shù)計(jì)法:
format(x, 'e')
>>> format(x, 'e')
'1.234568e+03'
>>> format(x, '0.2E')
'1.23E+03'
字符串中插入變量
>>> x = 1234.56789
>>>
>>> 'The value is {:0,.2f}'.format(x)
'The value is 1,234.57'
4. 二八十六進(jìn)制整數(shù)
整數(shù)轉(zhuǎn)換為二進(jìn)制、八進(jìn)制或十六進(jìn)制的文本串, 可以分別使用 bin() , oct() 或 hex() 函數(shù):
>>> x=1234
>>> bin(x)
'0b10011010010'
>>>
>>> oct(x)
'0o2322'
>>>
>>> hex(x)
'0x4d2'
>>>
>>> x=-1234
>>> bin(x)
'-0b10011010010'
>>>
>>> oct(x)
'-0o2322'
>>>
>>> hex(x)
'-0x4d2'
另外,如果你不想輸出 0b , 0o 或者 0x 的前綴的話,請(qǐng)使用 format() 函數(shù)。比如:
>>> x=1234
>>> format(x, 'b')
'10011010010'
>>> format(x, 'o')
'2322'
>>> format(x, 'x')
'4d2'
>>>
>>> x = -1234
>>> format(x, 'b')
'-10011010010'
>>> format(x, 'x')
'-4d2'
各進(jìn)制轉(zhuǎn)化
>>> int('4d2', 16)
1234
>>> int('10011010010', 2)
1234
>>> int('0o2322', 8)
1234
5. 無(wú)窮大與NaN
你想創(chuàng)建或測(cè)試正無(wú)窮、負(fù)無(wú)窮或NaN(非數(shù)字)的浮點(diǎn)數(shù)。
>>> a = float('inf')
>>> b = float('-inf')
>>> c = float('nan')
>>> a
inf
>>> b
-inf
>>> c
nan
>>>
為了測(cè)試這些值的存在,使用 math.isinf() 和 math.isnan() 函數(shù)。比如:
>>> math.isinf(a)
True
>>> math.isnan(c)
True
6. 隨機(jī)選擇
random 模塊有大量的函數(shù)用來(lái)產(chǎn)生隨機(jī)數(shù)和隨機(jī)選擇元素。
要想從一個(gè)序列中隨機(jī)的抽取一個(gè)元素,可以使用 random.choice():
>>> import random
>>> values = [1, 2, 3, 4, 5, 6]
>>>
>>> random.choice(values)
>>> 2
提取出N個(gè)不同元素的樣本用來(lái)做進(jìn)一步的操作,可以使用 random.sample() :
>>> random.sample(values,2)
[2, 5]
打亂序列中元素的順序,可以使用 random.shuffle() :
>>> random.shuffle(values)
>>> values
[4, 3, 2, 6, 1, 5]
>>>
>>> random.shuffle(values)
>>> values
[6, 3, 4, 1, 2, 5]
生成隨機(jī)整數(shù),請(qǐng)使用 random.randint()
>>> random.randint(0,10)
6
7. 基本的日期與時(shí)間轉(zhuǎn)換
獲取當(dāng)前時(shí)間
>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2018, 2, 23, 17, 8, 36, 311740)
>>>
>>> datetime.today()
datetime.datetime(2018, 2, 23, 17, 8, 42, 232123)
>>>
>>> today=datetime.today()
>>> print(today)
2018-02-23 17:09:06.168817
>>>
>>> now=datetime.now()
>>> print(now)
2018-02-23 17:09:17.158419
>>> now.day
23
>>> now.year
2018
>>> now.month
2
>>> now.hour
17
>>> now.minute
9
>>> now.second
17
兩個(gè)日期的比較
>>> from datetime import timedelta
>>> a = datetime(2012, 9, 23)
>>> b = datetime(2013, 9, 23)
>>> a < b
True
>>>
>>> (a + timedelta(days=365)) == b
True
>>> type(b-a)
<class 'datetime.timedelta'>
>>> (b-a).days
365
獲取當(dāng)前月份的第一天
>>> from datetime import date
>>> date.today().replace(day=1)
datetime.date(2018, 2, 1)
獲取本月的最后一天
>>> from datetime import date
>>> import calendar
>>> firstDay=date.today().replace(day=1)
>>> calendar.monthrange(firstDay.year, firstDay.month)[1]
28
8. 字符串轉(zhuǎn)換為日期
>>> text = '2012-09-20'
>>> y = datetime.strptime(text, '%Y-%m-%d')
>>>
>>> z= datetime.now()
>>>
>>> z - y
datetime.timedelta(1982, 62661, 581312)
另一種格式化
>>> nice_z = datetime.strftime(z, '%A %B %d, %Y')
>>> nice_z
'Sunday September 23, 2012'