Bonus Exercise

Valid Dictionary Keys

Given this dictionary

h = {
    0: "Zero",
    "1": "One",
    2: 2
}
  1. Access and print the string "Zero"

  2. Access and print the string "One"

  3. Access and print the number 2

Wat Numbers as dictionary keys? Yup.

Another one. Given this dictionary

is_it = {
    True: "It's true!",
    False : "It's false"
}
  1. What will print here?: print(is_it[2 + 2 == 4])

  2. What will print here?: print(is_it[4 == 2])

WAT Booleans as dictionary keys? Yup.

Read more about what key types are valid here: https://realpython.com/python-dicts/#restrictions-on-dictionary-keys

  1. Can you use a tuple as a key? Try it and find out. Create a dictionary with a tuple as one of its keys then print its value.

Last updated