24. Quiz: The Standard Library
Quiz: Compute an Exponent
It's your turn to import and use the math
module. Use the math
module to calculate e
to the power of 3. print
the answer.
Refer to the math module's documentation to find the function you need!
Start Quiz:
# print e to the power of 3 using the math module
Quiz: Password Generator
Write a function called generate_password
that selects three random words from the list of words word_list
and concatenates them into a single string. Your function should not accept any arguments and should reference the global variable word_list
to build the password.
Start Quiz:
# Use an import statement at the top
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
# test your function
print(generate_password())
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
Explore the Standard Library
In the following quizzes, enter the name of the module that solves each problem. Note that capitalization is important! Every module in the standard library is lowercased. You can browse the library documentation here.
Which Module? 1
QUESTION:
Which module can tell you the current time and date?
SOLUTION:
datetime
Which Module? 2
QUESTION:
Which module has a method for changing the current working directory?
SOLUTION:
os
Which Module? 3
QUESTION:
Which module can read data from a comma separated values (.csv) file into Python dictionaries for each row?
SOLUTION:
csv
Which Module? 4
QUESTION:
Which module can help extract all of the files from a zip file?
SOLUTION:
zipfile
Which Module? 5
QUESTION:
Which module can say how long your code took to run?
SOLUTION:
time
Our favourite modules
The Python Standard Library has a lot of modules! To help you get familiar with what's available, here are a selection of our favourite Python Standard Library modules and why we use them!
csv
: very convenient for reading and writing csv filescollections
: useful extensions of the usual data types includingOrderedDict
,defaultdict
andnamedtuple
random
: generates pseudo-random numbers, shuffles sequences randomly and chooses random itemsstring
: more functions on strings. This module also contains useful collections of letters likestring.digits
(a string containing all characters which are valid digits).re
: pattern-matching in strings via regular expressionsmath
: some standard mathematical functionsos
: interacting with operating systemsos.path
: submodule ofos
for manipulating path namessys
: work directly with the Python interpreterjson
: good for reading and writing json files (good for web work)
We hope you find these useful!