import time
时间戳:通过时间差来表示具体的时间(指定时间到1970年1月1日0时0分0秒(格林威治时间)之间的时间差,单位是秒)
时间戳保存时间的优点:
t1 = time.time()
print(t1) # 1661476346.3788147
t2 = time.localtime()
print(t2)
# time.struct_time(tm_year=2022, tm_mon=8, tm_mday=26, tm_hour=9, tm_min=28, tm_sec=1, tm_wday=4, tm_yday=238, tm_isdst=0)
t3 = time.localtime(0)
print(t3)
#time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
t4 = time.localtime(time.time())
print(t4)
# time.struct_time(tm_year=2022, tm_mon=8, tm_mday=27, tm_hour=9, tm_min=21, tm_sec=8, tm_wday=5, tm_yday=239, tm_isdst=0)
result = time.mktime(t4)
print(result)
# 1661563629.0
print(f'{t4.tm_year}年{t4.tm_mon}月{t4.tm_mday}日')
# 2022年8月27日
result = time.strftime('%Y年%m月%d日',t4)
print(result)
# 2022年8月27日
# 星期xx.xx
result = time.strftime('%a %H:%M',t4)
print(result)
# Sat 09:21
result = time.strptime('2021/8/27','%Y/%m/%d')
print(result)
time.struct_time(tm_year=2021, tm_mon=8, tm_mday=27, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=238, tm_isdst=-1)
from datetime import datetime,timedelta
t1 = datetime.today()
print(t1,type(t1))
# 2022-08-27 09:29:45.723203
t2 = datetime.now()
print(t2,type(t2))
# 2022-08-27 09:29:45.723203
t3 = datetime(2022, 12, 5)
t4 = datetime(2022, 8, 26, 10, 20, 30)
print(t3,t4)
# 2022-12-05 00:00:00
# 2022-08-26 10:20:30
t5 = datetime.strptime('2022/8/27','%Y/%m/%d')
print(t5)
# 2022-08-27 00:00:00
print(t5.year) # 2022
print(t5.month) # 8
print(t5.day) # 27
print(t5.weekday()) # 5 # 星期值的取值范围:0~6,0表示星期1,此处5代表星期六
# xxxx年xx月xx日
result = t4.strftime('%Y年%m月%d日')
print(result) # 2022年08月26日
t1 = datetime(2022, 5, 3, 9, 10)
t2 = datetime(2022, 10, 1, 10, 5)
result = t2 - t1
print(result, type(result))
# # 151 days, 0:55:00
# 返回时间之间的差值,为时间间隔对象
# 也可单独获取总天数和不满一天的秒数
print(result.days) # 151
print(result.seconds) # 3300
result = t1.timetuple()
print(result)
# time.struct_time(tm_year=2022, tm_mon=5, tm_mday=3, tm_hour=9, tm_min=10, tm_sec=0, tm_wday=1, tm_yday=123, tm_isdst=-1)
result = t1.timestamp()
print(result) # 1651540200.0
t1 = datetime(2022, 10, 1, 10, 8, 30)
print(t1) # 2022-10-01 10:08:30
# 10天以后对应的时间
result = t1 + timedelta(days=10)
print(result) # 2022-10-11 10:08:30
# 32天前对应的时间
result = t1 - timedelta(days=30)
print(result) # 2022-09-01 10:08:30
# 30分钟以后
result = t1 + timedelta(minutes=30)
print(result) # 2022-10-01 10:38:30
# 5天6小之前
result = t1 - timedelta(days=5, hours=6)
print(result) # 2022-09-26 04:08:30
# 3周以后
result = t1 + timedelta(weeks=3)
print(result) # 2022-10-22 10:08:30
from random import randint, random, randrange, sample, choice, choices, shuffle
print(random())
# 产生0~20之间的随机小数
# 0*20~1*20 -> 0 ~ 20
print(random()*20)
# 产生5~30之间的随机小数
# 0*25~1*25 -> 0+5 ~ 25+5 -> 5 ~ 30
print(random()*25+5)
# 控制小数的小数位数(四舍五入):round(小数, 需要保留的小数位数)
print(round(3.1415926, 2)) # 3.14
print(range(1, 10, 2)) # 1, 3, 5, 7, 9
print(randrange(1, 10, 2))
result = sample([10, 34, 78, 90], 3)
print(result) # 10,34,90
result = sample('abcd', 1)
print(result) # c
result = choice([10, 34, 78, 90])
print(result) # 78
# 有放回抽样
result = choices([10, 34, 78, 90], k=3)
print(result)
# 权重列表中的数字代表对应选项的份数
result = choices(['1000万', '10000', '100', '0'], [1, 2, 100, 200], k=2)
print(result)
nums = [10, 34, 78, 90]
shuffle(nums)
print(nums)
from random import randint
# 思路一:可以先完成一个有所有大小写字母和数字的容器,在定义函数,根据指定长度每次随机在容器获取元素。
def create_code(length):
# 创建验证码对应的所有符号的容器
all_code = ''
# 根据大小写字母对应的编码生成所有大小写字母
small = 97
big = 65
for i in rang(26)
all_code += cha(s) + chr(b)
small += 1
big += 1
# 根据数字对应的编码生成所有数字
num = 48
for i in range(10)
all_code += chr(num)
num += 1
# 根据长度获取验证码
code = ''
for i in range(lenth):
code += all_code[randint(0,len(all_code)-1)]
return code
# 思路2:创建三个变量,产生随机的大小写字母和数字,在创建容器保存,随机获取其中一个元素
def create_code2(length: int):
code = ''
for i in range(lenght)
c1 = chr(randint(97,26-1))
c2 = chr(randint(65,26-1))
c3 = str(randint(0, 9))
code += [c1, c2, c3][randint(0,2)]
return code
# 方法1:
def even_sum(start: int, end: int):
sum1 = 0
for x in range(start, end+1):
if x % 2 == 0:
sum1 += x
return sum1
# 方法2:
def even_sum2(start: int, end: int):
if start % 2:
start += 1
return sum(range(start, end+1, 2))
# 方法1:
def del_null_value(list1: list):
new_list = []
for x in list1:
if x or x == 0:
new_list.append(x)
return new_list
# 方法2:
def del_null_value2(list1: list):
for x in list1[:]:
if (not x) and (x != 0):
list1.remove(x)
def num_of_letter_number(str1: str):
count1 = count2 = 0
for x in str1:
if x.isdigit():
count1 += 1
elif x.islower() or x.isupper():
count2 += 1
return count2, count1
# 例如:'abc12你好23mmn1.23' -> 求 12 + 23 + 1.23 的结果
def sum_num(str1: str):
from re import findall
result = findall(r'-?\d+\.?\d*', str1)
return sum([float(x) for x in result])
要求创建PlainRect对象的时候width和height必须赋值,start_x和start_y可以赋值可以不赋值。
添加一个方法:判断某个点是否在矩形内部的方法is_inside()。如在矩形内,返回True, 否则,返回False。
class Rect:
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
def perimeter(self):
return self.width*2 + self.height*2
class PlainRect(Rect):
def __init__(self, width, height, start_x=0, start_y=0):
super().__init__(width, height)
self.start_x = start_x
self.start_y = start_y
def is_inside(self, p_x, p_y):
return self.start_x <= p_x <= self.start_x + self.width and self.start_y <= p_y <= self.start_y + self.height