Lab Assignment #8

Advanced Topics in Python (Review)


Description


Iterating Over Dictionaries
Call the appropriate method on movies such that it will print out all the items (hint, hint) in the dictionary—that is, each key and each value.

15iterate


Comprehending Comprehensions
Use a list comprehension to create a list, threes_and_fives, that consists only of the numbers between 1 and 15 (inclusive) that are evenly divisible by 3 or 5.

16compre


List Slicing
The string in the editor is garbled in two ways:
1. First, our message is backwards;
2. Second, the letter we want is every other letter.

Use list slicing to extract the message and save it to a variable called message.
17slicing


Lambda Expressions
1. Create a new variable called message.
2. Set it to the result of calling filter() with the appropriate lambda that will filter out the “X”s. The second argument will be garbled.
3. Finally, print your message to the console.

18lambda


Code1
movies = {
“Monty Python and the Holy Grail”: “Great”,
“Monty Python’s Life of Brian”: “Good”,
“Monty Python’s Meaning of Life”: “Okay”
}
print movies.items()

Code2
threes_and_fives = [i for i in range(1, 16) if i %3 == 0 or i %5 == 0]
print threes_and_fives

Codes3
garbled = “!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI”
message = garbled[::-1]
message = garbled[::-2]
print message

Code4
garbled = “IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX”
message = filter(lambda x:x != “X”, garbled)
print message