每天至少打卡一道python面试题。以尽量多的方式解锁题目,如果有遗漏的方法,欢迎在评论区补充。希望大家一起提高!
常用的有
- os 提供了与操作系统关联的函数
import os
os.getcwd()
os.path.join('/usr/temp','test.log')
- datatime 提供了日期和时间处理的常用方法
from datetime import datetime
# 将时间戳转换为日期类型
datetime.fromtimestamp(1638175727)
datetime.strptime('2021-12-01 13:13:13','%Y-%m-%d %H:%M:%S')
datetime.now()
- random 随机数生成库
import random
# 生成1-100的随机数
print(random.randint(1,100))
# 在abcdef中随机选择一个
print(random.choice('abcdef'))
- math 数学运算库
import math
print(math.pow(2,4))
print(math.sin(1))
print(math.floor(2.5))
- re 正则匹配模块
import re
# 将正则表达式编译成Pattern对象
pattern = re.compile(r'hello')
# 使用Pattern匹配文本,获得匹配结果,无法匹配时将返回None
match = pattern.match('hello world!')
if match:
# 使用Match获得分组信息
print(match.group())
# 搜索整个字符串
m = re.search('[a-z]+','abcdef123ghh')
print(m.group(0))
m2 = re.search('[0-9]+','abcdef123ghh')
print(m2.group(0))