Bonus Exercise
Valid Dictionary Keys
Given this dictionary
h = {
    0: "Zero",
    "1": "One",
    2: 2
}Access and
printthe string "Zero"Access and
printthe string "One"Access and
printthe number 2
Wat Numbers as dictionary keys? Yup.
Another one. Given this dictionary
is_it = {
    True: "It's true!",
    False : "It's false"
}What will print here?:
print(is_it[2 + 2 == 4])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
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
Was this helpful?