Python 中常用的标准库
-
python中获取当前时间和日期的模块:
datetime
-
python中具有更改当前工作目录的方法
os
-
哪个模块可以将逗号分隔 (.csv) 文件中的每行数据读取到 Python 中?
csv
-
哪个模块可以帮助我们从 zip 文件中提取所有文件?
zipfile
-
哪个模块可以显示代码的运行时间?
time
Python 中常用的标准库[相关练习]
-
使用
math
模块,计算e
的3次幂,然后 输出 答案import math print(math.exp(3)) # 20.085536923187668
-
写一个叫做
generate_password
的函数,该函数会从提供的单词文件中随机选择三个单词,并将它们连接成一个字符串。我们已经在起始代码中提供了从文件中读取数据的代码,你需要利用这些部分构建一个密码。words.txt:
Alice was beginning to get very tired of sitting by her sister bank having nothing Once twice she had peeped into the book her sister was reading but it had no pictures or conversations in it and what is the use of a book thought Alice without pictures or conversations
password_generator.py :
# Use an import statement at the top import random word_file = "words.txt" word_list = [] #fill up the word_list with open(word_file,'r') as words: for line in words: # remove white space and make everything lowercase word = line.strip().lower() # don't include words that are too long or too short if 3 < len(word) < 8: word_list.append(word) # Add your function generate_password here # It should return a string consisting of three random words # concatenated together without spaces def generate_password() : return random.choice(word_list) + random.choice(word_list) + random.choice(word_list) # test your function print(generate_password())
运行 $
python password_generator.py
, 随机输出三个单词连成的字符串