0
0
Pythonprogramming~5 mins

Dictionary creation in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a dictionary in Python?
A dictionary is a collection of key-value pairs where each key is unique and used to access its corresponding value.
Click to reveal answer
beginner
How do you create an empty dictionary in Python?
You create an empty dictionary using curly braces: {} or the dict() function.
Click to reveal answer
beginner
Create a dictionary with keys 'name' and 'age' and values 'Alice' and 30.
Use {'name': 'Alice', 'age': 30} to create the dictionary.
Click to reveal answer
intermediate
Can dictionary keys be mutable types like lists?
No, dictionary keys must be immutable types like strings, numbers, or tuples. Lists cannot be keys because they can change.
Click to reveal answer
intermediate
What happens if you create a dictionary with duplicate keys?
The last value for the duplicate key will overwrite previous ones, so only the last key-value pair remains.
Click to reveal answer
Which of these is the correct way to create a dictionary with keys 'a' and 'b' and values 1 and 2?
A{'a': 1, 'b': 2}
B['a', 1, 'b', 2]
C('a', 1, 'b', 2)
D[('a', 1), ('b', 2)]
What is the output of type({}) in Python?
A<class 'tuple'>
B<class 'dict'>
C<class 'set'>
D<class 'list'>
Which of these can be used as a dictionary key?
A[1, 2, 3]
B{'key': 'value'}
C[['a', 1]]
D(1, 2, 3)
What will be the value of key 'x' after this dictionary is created? {'x': 1, 'x': 2}
AError
B1
C2
DNone
How do you create an empty dictionary using a function?
Adict()
Blist()
Cset()
Dtuple()
Explain how to create a dictionary in Python and what rules apply to keys.
Think about how you store labeled information and what can be used as labels.
You got /3 concepts.
    Describe what happens if you use duplicate keys when creating a dictionary.
    Consider what happens if you write the same label twice on a list of notes.
    You got /3 concepts.