Preview

12 - Dictionaries (Key Value Pairs)

 1. What is a Dictionary (in Python)?
Note: the following embed from www.teachyousrelfpython.com provides a comprehensive and student-friendly overview of dictionaries in python

  A fixed string that operates like an integer

  A type of array that holds only numbers and special characters

  A type of online reference book which has thousands of terms and definitions

  A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values

 2. Which of the following is a dictionary?
#OPTION 1
thisdict = [
  "apple": "green",
  "banana": "yellow",
  "cherry": "red"
  ]




#OPTION 2

thisdict =	{
  "apple": "green",
  "banana": "yellow",
  "cherry": "red"
}


#OPTION 3

thisdict = """
"apple": "green",
  "banana": "yellow",
  "cherry": "red"
"""

  Option 3

  Option 4

  Option 2

  Option 1

 3. Dictionaries have key value pairs and that is what makes them useful. In the following example, which of the following is a valid key?
thisdict =	{
  "Christianity": "Jesus",
  "Islam": "Mohammed",
  "Atheism": "Self"
}

  Self

  Atheism

  Jesus

  Christianity

 4. This is one method (using a for loop) to print all the keys in a dictionary. What is another?

  print(d.keys())

  print all keys

  None of the above

  print(keys in d)

 5. What does the following perform on the dictionary?
python_words = {'list': 'A collection of values that are not connected, but have an order.',
                'dictionary': 'A collection of key-value pairs.',
                'function': 'A named set of instructions that defines a set of actions in Python.',
                }

for word in sorted(python_words.keys()):
    print("%s: %s" % (word.title(), python_words[word]))

  Sorts by keys and values and prints them all

  1

  None of the above

  Loops over the dictionary in order (ascending)\",Sorts by values and prints them in reverse order"

 6. The following shows a list of films with their corresponing number of likes. The max function sorts the list by max to min
def main():
    dictFilms = { 
       'Film 1' : 23, 
       'Film 2' : 44, 
       'Film 3' : 98, 
       'Film 4' : 87, 
       'Film 5' : 92,   }


    a=max(dictFilms,key=dictFilms.get)
    print(a)
   

main()

  TRUE

  FALSE

 7. In the following code, how has a dictionary been used?
print("***CAN YOU SURVIVE?****")
print("Type 'o' to list starting objects......")


objects = {
            "sharp_flint" : 50,

            "grass" : 100,
            "hay" : 0,

            "tree" : 500,
            "log" : 0,

            "Big rock" : 30,
            "Cut rock" : 0,

        }

userinput = input("....So, here you are! What now? : ")


if userinput == "o":
    print("Here are a list of starting objects: ", objects)

  to obtain user input in the format: object and key:

  No Dictionary has been used

  to create an inventory of objects (keys are the objects and the number of objects stored are the values)

  None of the above

 8. How would you retreive and display the age of super pog?
super_pog = {'weapon': 'sword', 'transport': 'chariot', 'age': 2}

#How would you display the age of super pog?

  None of the above

  print(super_pog[2]

  print[key]super_pog

  print(super_pog['age'])

 9. What is happening on lines 8 and 14?
def main():
    #create a dictionary to store teachers and the subject they teach
    teacher_details={"Name": "Mr Moose","Subject": "Philosophy","Hobby":"Chess","School House":"Red House"}
#update a dictionary by adding a new entry or a key-value pair, modify an existing entry or delete an existing entry


    
    teacher_details["Name"]="Mr Hanniganahill"
    print(teacher_details)

    print()
    print()
    
    teacher_details["No. of classes"]="12"
    print(teacher_details)
main()

  add new entry / delete existing entry

  delete existing entry / add new entry

  delete existing entry / update existing entry

  update existing entry / add new entry

 10. Adictionarystores associations between keys of the same type and values of the same type in a collection with no defined ordering

  FALSE

  TRUE