Advanced Topics in Python! Unit 10 Code Academy

CodeAcadmey CodeAcadmey.png(2) CodeAcadmey.png(3( CodeAcadmey.png(4)

Lab Description:

In this lab, we learn more about iteration, list slicing, list comprehensions and lambda expressions. We create our own Python dictionary using my_dict and include two/three value/key pairs and print our dictionary with those items. Another way of printing those items instead of writing my_dict.items, we can write my_dict.keys and my_dict.values and it would still print out the same result. The for loop was used a lot of times in this lab but it was mainly for printing the number in range and for part 16, I wrote threes_and_fives = [n for n in range(1,16) if n % 3 == 0 or n % 5 == 0], which would print out 3,5,6,9.10,12,15. We also learn about list slicing where we want to reverse the message that is given for example garbled = “!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI”. Since the message is backwards, we would write ssage = garbled [::-2] print message and it would print, “I am the secret message!” There is another message where we would have to use filter in order for us to fix the message. For instance garbled = “IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX” message = filter (lambda x : x != “X”, garbled) print message. This would filter the x’s and remove them to create the message “I am another secret message!” I learned a lot in this lab and it was incredible to see that you can scramble the words when they are backwards.