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 type of online reference book which has thousands of terms and definitions

  A fixed string that operates like an integer

  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

  A type of array that holds only numbers and special characters

 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 2

  Option 1

  Option 4

  Option 3

 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

  Christianity

  Jesus

  Atheism

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

  print all keys

  print(keys in d)

  print(d.keys())

  None of the above

 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

  Sorts by values and prints them in reverse order

  Loops over the dictionary in order (ascending)\

  None of the above

 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()

  FALSE

  TRUE

 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)

  No Dictionary has been used

  None of the above

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

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

 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?

  print[key]super_pog

  print(super_pog[2]

  print(super_pog['age'])

  None of the above

 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 / update existing entry

  delete existing entry / add new 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