Introduction to Programming with Python
Day 4

David Garcia, University of Konstanz

Quiz I

  1. Assume you have the following dictionary:
dictionary = { "applez": 100, "oranges": 120", "bananas": 50}

How can I change the key of the key-value pair ("apples", 100)?

  1. What will be the output of the following code?
num_sum = 10

def doubler():
    num_sum *= 2
    
doubler()
  1. What will be the output of the following code?
def initialize():
    first_value = 1 

print(first_value)
  1. What is the correct output of the following code?
def text_to_list(text):
  t = text.lower()
  return t.split()

some_text = "The Old Man in The Sea"
print(text_to_list(some_text))
  1. [‘the’, ‘old’, ‘man’, ‘in’, ‘the’, ‘sea’]
  2. [‘The’, ‘Old’, ‘Man’, ‘in’, ‘The’, ‘Sea’]
  3. [“the, old, man, in, the, sea”]
  4. “the, old, man, in, the, sea”
  1. Write the output of the following code:
word = 'letters'
letters_d = {x: word.count(x) for x in word}
print(letters_d['e'])
  1. Describe the role of lambda in this code:
countpairs = [(x,y) for (x,y) in counts.items()]
countpairs.sort(key=lambda x: x[1], reverse = True)

Feedback on Day 3