random 库
>>>import random
>>>random.seed(23)
#设置随机数种子
>>>random.random() #[0.0-1.0)中的随机小数
>>>0.9248652516259452
>>> import random
>>> random.randint(2,5)
2
>>> random.randint(2,5)
4
>>>
- 生成一个[a,b]之间的随机整数步长为k的随机整数
>>> random.randrange(1,12,2)
3
>>> random.randrange(1,12,2)
5
>>>random.getrandbits(10)#(k) #生成一个10比特长度的数
971
>>>random.uniform(2.3,5.4) #[a,b]之间的随机小数
5.066543366550596
>>>random.choice([1,2,3,4,5])
1
>>>a = [1,2,3,4,5]
>>>random.shuffle(a) #随机排序
>>>a
>>>[2,3,1,5,4]
>>>c = random.sample(a,2) #随机抽样
>>>c
c = [1,4]
time库
>>>import time
>>>time.localtime() #生成当地时间
>>>time.struct_time(tm_year=2022, tm_mon=6, tm_mday=25, tm_hour=16, tm_min=39, tm_sec=10, tm_wday=5, tm_yday=176, tm_isdst=0)
- 获取时间戳 自纪元年起以秒为单位返回的浮点数的时间
>>>time.time() #获取时间戳
>>>time.gmtime() #生成当地时间(UTC标准时间)
>>>time.struct_time(tm_year=2022, tm_mon=6, tm_mday=25, tm_hour=8, tm_min=41, tm_sec=22, tm_wday=5, tm_yday=176, tm_isdst=0)
>>>time.ctime()
>>>'Sat Jun 25 16:42:07 2022'
time.sleep(2)
time.strftime('下午%H时:%M分:%S秒')
'下午17时:53分:42秒'
字符串 | 说明 |
%Y | 年份 |
%m | 月份 |
%B | 月份名称 |
%b | 月份名称缩写 |
%d | 日期 |
%A | 星期 |
%a | 星期缩写 |
%H | 小时(24) |
%I | 小时(12) |
%p | 上/下午 (返回PM/AM) |
%M | 分钟 |
%S | 秒 |
| |
faker库
有趣的模块
calendar模块
import calendar
year = eval(input("Enter year"))
month = eval(input("Enter month"))
print(calendar.month(year,month))
###运行
Enter year2021
Enter month2
February 2021
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
弹出警报框
import pyautogui
num=int(input("Enter a value to divide 100"))
if num == 0:
pyautogui.alert(" Alert!!! 100 cannot be divided by 0")
else:
print(f'The value is {100/num}')
print("continue")
播放嘟嘟嘟
import winsound
duration = 100
freq = 440
for i in range(100):
winsound.Beep(freq,duration)
手写转换
import pywhatkit
pywhatkit.text_to_handwriting("Happy everyday!")
jieba库
- 中文分词第三方库
- 名字起得不错,意思是结巴
- cut 精确分词
import jieba
s = "快乐的生活就要结束了。新冠疫情也会结束的。\
本科也会毕业的。北溪二号可能是阿美莉卡炸坏的。生活还是要继续的。\
祝全站的童鞋都能幸福生活,快乐每一天!"
s1 = jieba.cut(s)
>>> s1
<generator object Tokenizer.cut at 0x0000029DE9CD5A80>
>>> next(s1)
'快乐'
s2 = jieba.cut(s,cut_all=True)
>>> s2
<generator object Tokenizer.cut at 0x0000029DE9CD7300>
>>> next(s2)
'快乐'
s3 = jieba.cut_for_search(s)
>>> s3
<generator object Tokenizer.cut_for_search at 0x0000029DE9D600B0>
>>> next(s3)
'快乐'
s4 = jieba.lcut(s)
>>> s4
['快乐', '的', '生活', '就要', '结束', '了', '。', '新冠', '疫情', '也', '会', '结束', '的', '。', '本科', '也', '会', '毕业', '的', '。', '北溪', '二号', '可能', '是', '阿美莉', '卡', '炸坏', '的', '。', '生活', '还是', '要', '继续', '的', '。', '祝', '全站', '的', '童鞋', '都', '能', '幸福生活', ',', '快乐', '每', '一天', '!']
w = "刘星"
jieba.add_word(w)