This lab is about review the material of certain advanced codes in Python. These vary from list splices that use stride, the filter and lambda function, list compression, and iterations of Dictionaries. This will educate students on how stride can help slicing lists to get specific parts out by either going by 2, 3, 4, or more. The filter function uses two keys, lambda and the list. The filter function uses lambda to set what would seem like a rule that tells the function what to look for in the list, the list being the second part. List compression uses a function to create a list without having to do all the work of putting numbers in and such, Dictionary iterations makes picking out keys and values simple by using .items(),keys(), and .values().
Code used for stride
garbled = “!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI”
message = garbled[::-2]
print message
Code used for filtering
garbled = “IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX”
message = filter(lambda x: x != “X”, garbled)
print message
Code used for List compression
threes_and_fives = [x for x in range(1,16) if x % 3 == 0 or x % 5 == 0]
print threes_and_fives
Code used for Dictionary Iterations
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()