1. What are dictionaries in Python
-
Dictionary is among the knowledge varieties which is used to retailer knowledge within the pairs of key : knowledge.
-
It’s ordered.
-
It’s mutable.
-
Keys shouldn’t be duplicated however knowledge could be identical.
-
Dictionaries are written with curly brackets.
2. Making a Dictionary
- It’s encloses in curly {} braces.
- Separated by a comma (,).
- It holds values in pairs of keys corresponding with knowledge.
- Keys can’t be repeated and are immutable.
- Information could be of any Datatype.
2.1 Instance
dict = {1: 'A', 2: 'B', 3: 'C'}
print(dict)
OUTPUT
{1: ‘A’, 2: ‘B’, 3: ‘C’}
3. Accessing knowledge of a dictionary
We are able to entry the information by their respective keys.
3.1 Syntax
dict = {1: 'A', 2: 'B', 3: 'C'}
print(dict[1])
OUTPUT
A
4. Including components to a dictionary
Syntax:
dict = {1: 'A', 2: 'B', 3: 'C'}
print("outdated dictionary")
print(dict)
new_dict=dict[4]='a'
print("Up to date dictionary")
print(dict)
OUTPUT:
outdated dictionary
{1: ‘A’, 2: ‘B’, 3: ‘C’}
Up to date dictionary
{1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘a’}