Dictionary 114722
Dictionary 114722
Dictionary is a data structure in which we store values as a pair of key and value. Each key is separated
from its value by a colon (:), and consecutive items are separated by commas. The entire items in a
dictionary are enclosed in curly brackets “{}”.
• To create a dictionary with one or more key_value pairs you can also use the dict() function. The
dict() creates a dictionary directly from a sequence of key value pairs.
Example:
d = dict([("Name", "John"), ("Roll_no", 45), ("class", "B.Tech")])
print(d)
Output: {'Name': 'John', 'Roll_no': 45, 'class': 'B.Tech'}
print(d["John"])
Output: KeyError: 'John'