In Python, a dictionary is a built-in data structure that stores data in key-value pairs. Dictionaries are mutable, meaning you can change, add, or remove items. Each key in a dictionary must be unique, immutable, and hashable (e.g., strings, numbers, or tuples).
Creating a Dictionary
Dictionaries can be created using curly braces {} with pairs of keys and values separated by a colon :.
dictData = {
"name": "John",
"age": 35,
"city": "London"
}
Alternatively, you can create an empty dictionary and add items later:
dictData = {}
dictData ["name"] = "John"
dictData ["age"] = 35
dictData ["city"] = "London"
You can also use the dict() constructor
my_dict = dict(name="John", age=35, city="London")
Python Dictionary – Interview Questions
Q 1: What is a dictionary in Python?
Q 2: How do you create a dictionary?
Q 3: Can dictionary keys be duplicated?
Q 4: Are dictionary values mutable?
Q 5: How do you access dictionary values?
Python Dictionary – Objective Questions (MCQs)
Q1. Which of the following correctly creates a dictionary in Python?
Q2. What type of values can a Python dictionary hold?
Q3. Dictionary elements are accessed using:
Q4. What will be the output of the following code?
d = {'a': 1, 'b': 2, 'c': 3}
print(len(d))
Q5. Which of the following statements is true about dictionaries in Python?