Instantly share code, notes, and snippets.

@aparrish /worksheet3.py Secret
Created Nov 21, 2014

Embed
What would you like to do?
Final worksheet for Workshop and Tools.
#
# Worksheet #3
#
# This worksheet is also a Python program. Your task is to read the
# task descriptions below and then write one or more Python statements to
# carry out the tasks. There's a Python "print" statement before each
# task that will display the expected output for that task; you can use
# this to ensure that your statements are correct.
#
# In this worksheet, some of the tasks will throw an error that causes
# the program to stop running, and skip the remaining tasks. Because of
# this, you'll have to complete the tasks in the given order!
print "\n------"
print "Task 1: Making a dictionary"
print "Expected output: helium"
# Task 1: A variable "element_names" is defined below, with a value of "None".
# Change the definition of "element_names" so that the variable contains a
# dictionary value, and the print statement below displays "helium" (instead
# of throwing a KeyError).
element_names = None
print element_names['He']
#------------------------------------------------------------------------
print "\n------"
print "Task 2: Adding a new key to a dictionary"
print "Expected output: lithium"
# Task 2: Modify the values of the variables "key" and "val" below so that
# the print statement displays 'lithium'.
key = "???"
val = "???"
element_names[key] = val
print element_names['Li']
#------------------------------------------------------------------------
print "\n------"
print "Task 3: Modifying an existing key"
print "Expected output: 19"
# Task 3: Modifying the values of the variables "key" and "value" below
# so that the print statement displays "19".
key = "???"
val = "???"
word_counts = {'cheese': 10, 'wine': 17, 'arachnids': 24}
word_counts[key] = val
print word_counts['wine']
#------------------------------------------------------------------------
print "\n------"
print "Task 4: Keys"
print "Expected output: 4"
# Task 4: Change the expression on the right-hand side of the assignment
# operator for the variable "student_names" below, so that the print
# statement displays "4". Use the `.keys()` method.
student_ages = {'Alfred': 17, 'Bryson': 18, 'Candace': 19, 'David': 20}
student_names = [] # <-- change this!
print len(student_names)
#------------------------------------------------------------------------
print "\n------"
print "Task 5: Dictionary operations"
print "Expected output: Pluto is not a planet."
# Task 5: Change the value of the variable "planet_name" below so that the
# expected output is printed.
planet_classifications = {'Mercury': 'terrestrial', 'Venus': 'terrestrial',
'Earth': 'terrestrial', 'Mars': 'terrestrial', 'Jupiter': 'gas giant',
'Saturn': 'gas giant', 'Uranus': 'ice giant', 'Neptune': 'ice giant'}
planet_name = 'Neptune'
if planet_name in planet_classifications:
print planet_name + " is a planet."
else:
print planet_name + " is not a planet."
#------------------------------------------------------------------------
print "\n------"
print "Task 6: Keys and loops"
print "Expected output: "
print " Earth"
print " Mars"
print " Mercury"
print " Venus"
# Task 6: Modify the code below in the two following ways. First, change
# the right-hand side of the "planet_list" assignment. Then, add an "if"
# statement inside the "for" loop. Your goal is to produce the expected output
# above. Use the .keys() method and the sorted() function. You're
# checking to see which keys have the value "terrestrial".
planet_list = [] # <-- change this
for planet in planet_list:
# add an if statement here, and tab over the line below
print planet
#------------------------------------------------------------------------
print "\n------"
print "Task 7: Text analysis"
print "Expected output: 4"
# Task 7: Modify the code below in the two following ways. First, change
# the assignment of the variable "word_list" so that it contains a list
# of words in the string "words" (hint: use the ".split()" method). Second,
# replace the word "pass" in the "for" loop with a Python statement that
# adds a new key/value pair to the dictionary "word_dict", indicating the
# length of word. The result should be that the final print statement
# displays the output "4".
words = "Mother said there'd be days like these."
word_list = [] # <-- modify this
word_dict = {}
for word in word_list:
pass # <-- replace this
print word_dict['days']
#------------------------------------------------------------------------
print "\n------"
print "Task 8: Text analysis, part 2"
print "Expected output:"
print " Mother: 6"
print " said: 4"
print " there'd: 7"
print " be: 2"
print " days: 4"
print " like: 4"
print " these.: 6"
# Task 8: Using the "word_dict" variable you created in the previous task,
# write a loop that displays the expected output above. I've written a
# skeleton for you below. (You may need to use the "str()" function to
# append the two parts of the output string for each line together.)
for word in word_list:
pass # <-- replace this!
#------------------------------------------------------------------------
print "\n------"
print "Task 9: Dictionaries with lists"
print "Expected output: ['foo', 'bar', 'baz']"
# Task 9: Modify the values of the variables "key" and "val" below so that
# the expected output is displayed.
key = "???"
val = "???"
widget_characteristics = {'scrumbulator': ['foo', 'bar']}
widget_characteristics[key].append(val)
print widget_characteristics['scrumbulator']
#------------------------------------------------------------------------
print "\n------"
print "Task 10: Advanced!"
print "Expected output: Mercutian, Venutian, Earthling, Martian"
# Task 10: This is an advanced task! Examine the data structure below: a
# list of dictionaries. Replace the word "pass" in the "for" loop below
# with a Python statement that takes the "demonym" key from each dictionary
# and appends it to the list "demonyms," so that the expected output is
# produced.
planets = [
{'name': 'Mercury', 'type': 'terrestrial', 'demonym': 'Mercutian'},
{'name': 'Venus', 'type': 'terrestrial', 'demonym': 'Venutian'},
{'name': 'Earth', 'type': 'terrestrial', 'demonym': 'Earthling'},
{'name': 'Mars', 'type': 'terrestrial', 'demonym': 'Martian'}
]
demonyms = []
for planet in planets:
pass # <-- replace this!
print ", ".join(demonyms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment