python学習ステップ--2数値日付と時間
13493 ワード
基礎の大概は理解して、残ったのはあれらの比較的に奇抜で、役に立つ使い方で、これも1冊の熱い進級の書籍の上で少しずつ学んで、いくつかの年になりました
# region 3.1
# round(value, ndigits)
round(1.23, 1)
round(1.25361, 3)
# ndigits
a = 1627731
round(a, -2) # 1627700
#
a = 1.65368
print(format(a, "0.2f"))
"value is {:0.3f}".format(a)
# endregion
# region 3.2
#
a = 1.2
b = 1.3
c = a + b
# decimal
from decimal import Decimal
a = Decimal('4.2')
b = Decimal('2.1')
a + b
# endregion
# region 3.3
# format
x = 123.4567
format(x, "0.2f")
#
format(x, "<10.2f") # 10
#
format(x, "e")
format(x, "0.2E")
# endregion
# region 3.4
x = 1234 # 2 8 16
bin(x)
oct(x)
hex(x)
# format b o x
format(a, "b")
# 10 int
int("101101", 2)
int("bf", 16)
# endregion
# region 3.5
#
data = b'\x00\x124V\x00x\x90\xab\x00\xcd\xef\x01\x00#\x004' #
len(data)
int.from_bytes(data, byteorder="little")
aa = int.from_bytes(data, byteorder="big")
#
int.to_bytes(aa,byteorder="big")
#
# int.bit_length()
# endregion
# region 3.6
# complex(real, imag)
a = complex(2, 4)
a.conjugate() #
#
b = complex(1, 2)
a + b
a / b
abs(a)
# cmath
import cmath
cmath.sin(a) #
# numpy
import numpy as np
a = np.array(1+2j, 3+4j, 3+1j)
#
# Python
# endregion
# region 3.7 Nan
# 、 NaN( )
a = float('inf')
b = float("-inf")
c = float("nan")
# math
cmath.isinf(a)
cmath.isnan(c)
#
# NaN
a/a
a + b
# NaN ,
# NaN False
# endregion
# region 3.8
from fractions import Fraction
a = Fraction(5, 4)
b = Fraction(7, 16)
print(a + b) #
c = a * b #
c.numerator
c.denominator
# endregion
# region 3.9
#
x = [1, 2, 3, 4]
y = [2, 3, 4, 5]
# x * y
# x * 2
import numpy as np
ax = np.array([1, 2, 3, 4])
ay = np.array([5, 6, 7, 8])
ax * 2
ax + 10
ax * ay
#
#
def dd(x):
return 2 * x ** 2 + 3 * x + 4
np.sqrt(ax)
dd(ax)
# NumPy
# np.sin() np.cos() np.abs()
grid = np.ones(shape=(100, 100), dtype=int)
#
# endregion
# region 3.10
# numpy
import numpy as np
m = np.matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
#
#
# http://www.numpy.org
# endregion
# region 3.11
import random
values = [1, 2, 3, 4, 5]
#
random.choice(values)
# N
random.sample(values, 2)
#
random.shuffle(values)
#
random.randint(0, 10)
# endregion
# region 3.12
from datetime import timedelta
a = timedelta(days=2, hours=6)
b = timedelta(hours=4.5)
x = a + b
x.days
x.seconds
x.seconds / 3600
c.total_second() / 3600
#
from datetime import datetime
a = datetime(2012, 9, 23)
b = datetime(2012, 12, 21)
d = b - a
now = datetime.today()
print(now)
print(now + timedelta(minutes=10))
# endregion
# region 3.13
from datetime import datetime, timedelta
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
def find_previous_day(day, start_day = None):
if start_day is None:
start_day = datetime.today()
num = start_day.weekday() # 0-6
target = days.index(day)
chazhi = (num - target)
if chazhi == 0:
chazhi = 7
return start_day + timedelta(days=chazhi) # timedelta
find_previous_day( 'Wednesday')
# endregion
# region 3.15
from datetime import datetime
text = '2012-09-20'
y =datetime.strptime(text, "%Y-%m-%d")
#
y = datetime.now()
datetime.strftime(y, '%A %B %d, %Y')
# endregion
# region 3.16
from datetime import datetime
from pytz import timezone
d = datetime(2012, 12, 21, 9, 30, 0) #
print(d)
newlocal = timezone('US/Central') #
newd = newlocal.localize(d) #
print(newd)
# 2013 , 3 13 2:00( , )。
# ,
d = datetime(2013, 3, 10, 1, 45)
loc_d = newlocal.localize(d) #
print(loc_d) # 2013-03-10 01:45:00-06:00
later = loc_d + timedelta(minutes=30) #
print(later) # 2013-03-10 02:15:00-06:00
# ,
later = newlocal.normalize(loc_d + timedelta(minutes=30))
print(later) # 2013-03-10 03:15:00-05:00
# endregion