Chapter 6 exercise set 1¶
Dictionaries and sets doctest exercises¶
""" >>> type(thing1) <class 'dict'> >>> type(thing2) <class 'set'> """
""" >>> 4 in d True >>> 'name' in d False >>> d['name'] = 'Eric' >>> 'name' in d True """
Letter Counts¶
Write a program that prompts the user for a string and returns a table of each letter that occurs in the string together with its number of occurances, irrespective of case. The table should list the letters in alphabetical order and display them in lower case. White space, punctuation, digits, or any other non-alphabetic characters should be ignored.
A sample run of the program would look this this:
Please enter a string: ThiS is 1 String, with Upper and lower case Letters.
a 2
c 1
d 1
e 5
g 1
h 2
i 4
l 2
n 2
o 1
p 2
r 4
s 5
t 5
u 1
w 2
Mad Lib¶
Write a program named madlib.py
that makes a
Mad Lib, prompting the user for
parts of speech and then substituting their responses into a piece of text
in place of orginial words from the text for which they are substituted.
The following is a sample run of a program like this that uses the first paragraph from Alice’s Adventures in Wonderland as it’s source text:
$ python3 madlib.py
Please enter a verb ending in 'ing': sitting
Please enter a adjective: silly
Please enter a kind of relative: cousin
Please enter a noun: apricot
Please enter a verb ending in 'ing': babbling
Please enter a girl's name: Marisol
Please enter a noun: rug
Please enter a noun: pin
Please enter a noun: computer
Marisol was beginning to get very silly of sitting by her cousin on
the computer, and of having nothing to do: once or twice she had peeped
into the rug her cousin was babbling, but it had no apricot or pin in it,
‘and what is the use of a rug,’ thought Marisol ‘without apricots or
pins?’
Sorted list in a text file¶
Write a program that asks the user for the name of a file containing a list
of items, one per line, in a text file. The program should load the list from
the file, sort it, and then write it back out to a file with the name
sorted
appended to the original name.
For example, if the user types stuff.txt
as the name of the input file,
the program will load the list of items from stuff.txt
, sort it, and
write it back out to a file named stuffsorted.txt
.
Assuming the file states.txt
contains the following:
New Jersey
Maine
Virginia
Rhode Island
Maryland
Alabama
After running your program there would be a new file named statessorted.txt
containing the following:
Alabama
Maine
Maryland
Rhode Island
New Jersey
Virginia
Project: counting words with e’s¶
Write a program that takes a text file as an argument containing some of your favorite text - perhaps a poem, a speech, instructions to bake a cake, some inspirational verses to a song, etc.
Write a function which removes all punctuation from string, breaks the string into a list of words, and counts the number of words in your text that contain the letter ‘e’. Your program should print an analysis of the text like this:
Your text contains 243 words, of which 109 (44.8%) contain an 'e'.
The first three words starting with each letter¶
The file /usr/share/dict/words
should exist on unix-based systems. If it
does it contains a list of words in alphabetical order.
Write a program that prints out the first three words beginning with each letter of the alphabet.
Project: Counting Words in Alice in Wonderland¶
Write a program called alice_words.py
that creates a text file named
alice_words.txt
containing an alphabetical listing of all the words found
in alice_in_wonderland.txt
together with the number of times each word occurs. The first 10 lines of your
output file should look something like this:
Word Count
=======================
a 631
a-piece 1
abide 1
able 1
about 94
above 3
absence 1
absurd 2
How many times does the word, alice
, occur in the book?
What is the longest word in Alice in Wonderland ? How many charactes does it have?
pydoc¶
You can use pydoc to search through the Python libraries installed on your system. At the command prompt type the following:
$ pydoc3 -b
and the following will appear:
This is a listing of all the python libraries found by Python on your system.
Clicking on a module name opens a new page with documenation for that module.
Scrolling down to the /usr/lib/python3.4
section and clicking keyword
,
for example, opens the following page:
Documentation for most modules contains three color coded sections:
Classes in pink
Functions in orange
Data in green
Classes will be discussed in later chapters, but for now we can use pydoc to see the functions and data contained within modules.
The keyword
module contains a single function, iskeyword
, which as its
name suggests is a boolean function that returns True
if a string passed to
it is a keyword:
>>> from keyword import *
>>> iskeyword('for')
True
>>> iskeyword('all')
False
>>>
The data item, kwlist
contains a list of all the current keywords in
Python:
>>> from keyword import *
>>> print kwlist
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif',
'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import',
'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try',
'while', 'with', 'yield']
>>>
We encourage you to use pydoc
to explore the extensive libraries that come
with Python. There are so many treasures to discover!