數(shù)字的四舍五入
- 想對浮點數(shù)執(zhí)行指定精度的舍入運算
簡單的傳入運算可以使用round(value, ndigits)函數(shù)即可.
# 舍入
print(round(1.234, 2)) #1.23
print(round(1.2345, 1)) #1.2
# 格式化的方式
print(format(1.23456, '0.2f'))
print('value is {:0.3f}'.format(1.223243)) #value is 1.223
- 更加精確舍入操作,使用decimal模塊
from decimal import Decimal
a = Decimal('4.2')
b = Decimal('2.1')
print(a + b)
大數(shù)組運算
涉及到數(shù)組的重量級運算操作,可以使用 NumPy庫。 NumPy 的一個主要特征是它會給 Python提供一個數(shù)組對象,相比標(biāo)準(zhǔn)的Python列表而已更適合用來做數(shù)學(xué)運算
x= [1,2, 3,4]
y= [5,6, 7,8]
import numpy as np
ax = np.array(x)
ay = np.array(y)
print(ax*2) #[2 4 6 8]
print(ax + 10) # print(ax + 10)
print(ax + ay) # [ 6 8 10 12]
NumPy 是Python領(lǐng)域中很多科學(xué)與工程庫的基礎(chǔ),同時也是被廣泛使用的最大最復(fù)雜的 模塊, 可以詳細(xì)研究,這里只是一個小例子,知道這個庫的存在~
** 隨機選擇 **
- 想從一個序列中隨機抽取若干元素,或者想生成幾個隨機數(shù)
import random
values = [1,2,3,4,5,6]
print(random.choice(values)) #隨機選擇一個
print(random.sample(values, 2)) # 隨機提取N個元素
print(random.shuffle(values)) #僅打亂序列元素中的順序
print(random.randint(0,10)) #生成隨機整數(shù)
print(random.random()) #生成隨機0~1之間的浮點數(shù)0.7638343860063255
print(round(random.random(),2)) #生成隨機0~1之間的浮點數(shù)0.76
基本的日期與時間轉(zhuǎn)換
- 要執(zhí)行簡單的時間轉(zhuǎn)換,比如天到秒,小時到分鐘等的轉(zhuǎn)換
# 執(zhí)行不同時間單位的轉(zhuǎn)換和計算,請使用 datetime 模塊
from datetime import timedelta
a = timedelta(days=2, hours=6)
b = timedelta(hours=4.5)
c = a+b
print(c.days)
print(c.seconds) #37800
print(c.seconds / 3600) # 10.5
print(c.total_seconds()/3600) # 58.5
- 你想表示指定的日期和時間,先創(chuàng)建一個 datetime 實例然后使用標(biāo)準(zhǔn)的數(shù)學(xué)運算來 操作它們
from datetime import datetime
a = datetime(1989,12,6)
print(a + timedelta(days=10)) # 2020-07-13 00:00:00
b = datetime(2020, 7,3)
d = b - a
print(d.days)
now = datetime.today()
print(now) # 2020-07-03 12:05:15.935663
print(now + timedelta(minutes=10)) # 2020-07-03 12:15:51.011784
print(datetime.now()) # 2020-07-03 12:10:26.076258
am_10 = datetime.now().replace(hour=10, minute=0, second=0)
print(am_10) # 2020-07-03 10:00:00.076398
- 日期格式化,strptime()由字符串格式化為日期格式,轉(zhuǎn)換后可以加關(guān)系
print(datetime.strptime('2020-7-1 5:54:45', '%Y-%m-%d %H:%M:%S')) #2020-07-01 05:54:45
for pr in merged_prs:
pr_update = datetime.datetime.strptime(
pr.updated_at,
"%Y-%m-%dT%H:%M:%S.%fZ") + datetime.timedelta(hours=8)
if (pr_update - am_10).total_seconds() > 0:
pass
- 日期格式轉(zhuǎn)化為字符串格式, 可以strftime()
print(datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M')) #2020-07-03 14:05
計算當(dāng)前月份的日期范圍
- 需要在當(dāng)前月份中循環(huán)每一天,想找到一個計算這個日期范圍的高效方法
import calendar
from datetime import datetime, date, timedelta
def get_month_range(start_date=None):
if start_date is None:
start_date = date.today().replace(day=1)
_, days_in_month = calendar.monthrange(start_date.year, start_date.month) # monthrange() 函數(shù)會返回包含星期和該月天 數(shù)的元組。eg: (0,31)
end_date = start_date + timedelta(days=days_in_month)
return (start_date, end_date)
print(get_month_range()) # (datetime.date(2020, 7, 1), datetime.date(2020, 8, 1))
a_day = timedelta(days=1)
firstday , last_day = get_month_range()
while firstday < last_day:
print(firstday)
firstday += a_day
結(jié)果:
2020-07-01
2020-07-02
2020-07-03
2020-07-04
2020-07-05
2020-07-06
2020-07-07...