美文网首页Python3之路
Python3 CookBook学习笔记 -- 数字日期和时间

Python3 CookBook学习笔记 -- 数字日期和时间

作者: faris_shi | 来源:发表于2018-02-24 06:37 被阅读15次

    1. 浮点数的四舍五入

    round(value, ndigits),能够解决浮点数的四舍五入,但是仍然需要注意区别。

    >>> round(1.23, 1)
    1.2
    >>> round(1.27, 1)
    1.3
    >>> round(-1.27, 1)
    -1.3
    >>> round(1.25361,3)
    1.254
    >>>
    

    当一个值刚好在两个边界的中间的时候, round 函数返回离它最近的偶数。 也就是说,对1.5或者2.5的舍入运算都会得到2。

    >>> round(1.5)
    2
    >>> 
    >>> round(2.5)
    2
    

    当精度为负数时:

    >>> a = 1627731
    >>> round(a, -1)
    1627730
    >>> 
    >>> round(a, -2)
    1627700
    >>> 
    >>> round(a, -3)
    1628000
    

    2. 执行精确的浮点数运算

    我们在运算时经常会出现以下情况。

    >>> a = 4.2
    >>> b=2.1
    >>> a+b
    6.300000000000001
    >>> (a+b) == 6.3
    False
    

    所以我们为了精确,可以使用 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. 数字的格式化输出

    再格式化单个数字时,我们会和字符串一样,使用 format() 函数:

    当指定数字的位数后,结果值会根据 round() 函数同样的规则进行四舍五入后返回

    >>> x = 1234.56789
    >>> 到小数点后两位
    >>> format(x, '0.2f')
    '1234.57'
    >>> 
    >>> 右对齐
    >>> format(x, '>10.1f')
    '    1234.6'
    >>> 
    >>> 使用#补齐
    >>> format(x, '#>10.1f')
    '####1234.6'
    >>> 
    >>> 左对齐
    >>> 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'
    

    指数计法:

    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. 二八十六进制整数

    整数转换为二进制、八进制或十六进制的文本串, 可以分别使用 bin() , oct()hex() 函数:

    >>> 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 的前缀的话,请使用 format() 函数。比如:

    >>> x=1234
    >>> format(x, 'b')
    '10011010010'
    >>> format(x, 'o')
    '2322'
    >>> format(x, 'x')
    '4d2'
    >>> 
    >>> x = -1234
    >>> format(x, 'b')
    '-10011010010'
    >>> format(x, 'x')
    '-4d2'
    

    各进制转化

    >>> int('4d2', 16)
    1234
    >>> int('10011010010', 2)
    1234
    >>> int('0o2322', 8)
    1234
    

    5. 无穷大与NaN

    你想创建或测试正无穷、负无穷或NaN(非数字)的浮点数。

    >>> a = float('inf')
    >>> b = float('-inf')
    >>> c = float('nan')
    >>> a
    inf
    >>> b
    -inf
    >>> c
    nan
    >>>
    

    为了测试这些值的存在,使用 math.isinf()math.isnan() 函数。比如:

    >>> math.isinf(a)
    True
    >>> math.isnan(c)
    True
    

    6. 随机选择

    random 模块有大量的函数用来产生随机数和随机选择元素。

    要想从一个序列中随机的抽取一个元素,可以使用 random.choice():

    >>> import random
    >>> values = [1, 2, 3, 4, 5, 6]
    >>>  
    >>> random.choice(values)
    >>> 2
    

    提取出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]
    

    生成随机整数,请使用 random.randint()

    >>> random.randint(0,10)
    6
    

    7. 基本的日期与时间转换

    获取当前时间

    >>> 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
    

    两个日期的比较

    >>> 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
    

    获取当前月份的第一天

    >>> 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. 字符串转换为日期

    >>> 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'
    

    相关文章

      网友评论

        本文标题:Python3 CookBook学习笔记 -- 数字日期和时间

        本文链接:https://www.haomeiwen.com/subject/uewfxftx.html