python_cookbook记录

数字日期和时间(cookbook笔记)

2017-07-23  本文已影响0人  buildbody_coder

数字日期和时间

数字的四舍五入

>>> round(1.23, 1)
1.2
>>> round(1.27, 1)
1.3
>>> round(1.25361,3)
1.254
>>> a = 1627731
#个位
>>> round(a, -1)
1627730
#十位
>>> round(a, -2)
1627700
#百位
>>> round(a, -3)
1628000
>>>

精确的浮点数运算使用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')

复数运算

>>> a = complex(2, 4)
>>> b = 3 - 5j
>>> a
(2+4j)
>>> b
(3-5j)
#对应的实部、虚部和共轭复数可以很容易的获取
>>> a.real
2.0
>>> a.imag
4.0
>>> a.conjugate()
(2-4j)
>>> import cmath
>>> cmath.sin(a)
(24.83130584894638-11.356612711218174j)
>>> cmath.cos(a)
(-11.36423470640106-24.814651485634187j)
>>> cmath.exp(a)
(-4.829809383269385-5.5920560936409816j)
>>>

分数运算

>>> from fractions import Fraction
>>> a = Fraction(5, 4)
>>> b = Fraction(7, 16)
>>> print(a + b)
27/16
>>> print(a * b)
35/64

大型数组运算

>>> import numpy as np
>>> x = [1, 2, 3, 4]
>>> y = [5, 6, 7, 8]
>>> x * 2
[1, 2, 3, 4, 1, 2, 3, 4]
>>> x + 10
Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: can only concatenate list (not "int") to list
>>> x + y
[1, 2, 3, 4, 5, 6, 7, 8]
>>> import numpy as np
>>> ax = np.array([1, 2, 3, 4])
>>> ay = np.array([5, 6, 7, 8])
>>> ax * 2
array([2, 4, 6, 8])
>>> ax + 10
array([11, 12, 13, 14])
>>> ax + ay
array([ 6, 8, 10, 12])
>>> ax * ay
array([ 5, 12, 21, 32])

随机选择

>>> import random
>>> values = [1, 2, 3, 4, 5, 6]
>>> random.choice(values)
2
>>> random.choice(values)
3
>>> random.sample(values, 2)
[6, 2]
>>> random.shuffle(values)
>>> values
[2, 4, 6, 5, 3, 1]
>>> random.randint(0,10)
2
>>> random.randint(0,10)
5
>>> random.random()
0.9406677561675867

基本的日期与时间转换

>>> from datetime import timedelta
>>> from datetime import datetime
>>> a = timedelta(days=2, hours=6)
>>> b = timedelta(hours=4.5)
>>> c = a + b
>>> c.days
2
>>> c.seconds
37800
>>> c.seconds / 3600
10.5
>>> a = datetime(2012, 9, 23)
>>> print(a + timedelta(days=10))
2012-10-03 00:00:00
>>> b = datetime(2012, 12, 21)
>>> d = b - a
>>> d.days
89
>>> now = datetime.today()
>>> print(now)
2012-12-21 14:54:43.094063
>>> print(now + timedelta(minutes=10))
2012-12-21 15:04:43.094063
>>>

字符串转换为日期

>>> from datetime import datetime
>>> text = '2012-09-20'
>>> y = datetime.strptime(text, '%Y-%m-%d')
上一篇 下一篇

猜你喜欢

热点阅读