Dictionary data types are mutable.
Curly brackets are used in Dictionary data type.
How to give a value in Dictionary data type can be given only in key and value format. Key and value should be given inside curly brackets.
Syntax:
{key:value}
Ex:
{1:”java”}
That is, what is before colon is taken as key and what is after colon is taken as value.
Let's see how to create an empty dictionary.
a={}
print(type(a))
''''
output:
<class 'dict'>
''''
a={"name":"java","age":20}
print(a)
''''
output:
{'name': 'java', 'age': 20}
''''
len()
a={"name":"java","age":20}
print(len(a))
''''
output:
2
''''
Description:-
The value stored in the variable a is a dictionary data type.
len(a) -> since there are 2 values inside the dictionary, it displays 2 in the output.
The value inside the dictionary data type can be predicted using the len() method.
Concatenation
c1={"name":"java"}
c2={"age":27}
print(c1+c2)
''''
output
print(c1+c2)
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
''''
Description:-
Dictionary data type cannot be concatenated using (+) operator.
Repetition
c1={"name":"java"}
print(c1*2)
''''
output:
print(c1*2)
TypeError: unsupported operand type(s) for *: 'dict' and 'int'
''''
Description:-
We can't do repetition in dictionary data type like we did in list.
Accessing dictionary element
info={"name":"java","age":20}
print(info["name"])
''''
output:
java
''''
Description:-
print(info[‘name’]) This statement prints the value java of the key named name.
What we know from this is that if we want to get a value from the dictionary, we can only get it using the key of that value.
var={"name":"sakthi","age":20,"age":89}
print(var["age"])
''''
output:
89
''''
Description:-
print(var[‘age’]) -> This statement displays the value of age key which is 89. But why “age”: 20 does not print these values.
The reason is that in this dictionary type, there are 2 keys with the same name as age. Similarly, if you give a key with the same name many times, only the last key will be taken.
That's why it prints the value 89 of the last key age.
What we can learn from this is that a key with the same name should not be used repeatedly in the dictionary. If used in this way, it takes only the last existing key.
info={"name":"java","age":20}
info["name"]="php"
print(info)
''''
output:
{'name': 'php', 'age': 20}
''''
Description:-
In the variable named Info there are two dictionary elements named name and age. In this, the value java is given to the key named name and the value 20 is given to the key named age.
ifo[‘name’] = “php” This statement takes the java value already given in the name key and stores the php value in it.
When you try to print using the print(info) statement, instead of java, the value of php appears in the output.
info = {"name" : "php","age" : 20}
info["address"] = "madurai"
print(info)
''''
O/P
{'name' : 'php', age : 20, 'address' : 'madurai'}
''''
Description:-
info[“address”] = “madurai” -> This statement adds a third element called address along with the two dictionary elements named name and age given inside the variable named info.
Delete the dictionary element
info={"name":"php","age":20}
del info["name"]
print(info)
''''
output:
{'age': 20}
''''
Description:-
del info[“name”] -> This statement deletes the dictionary element named “name” in the dictionary named info.
When you try to print using the print(info) statement, only the age element is visible on the output screen.
Let's see how to delete the entire dictionary.
info={"name":"php","age":20}
del info
print(info)
''''
output:
print(info)
NameError: name 'info' is not defined
''''
Explain:-
del info -> This statement will delete the entire dictionary.
Note:-
If you want to delete an element in the dictionary or delete the entire dictionary, you must use the del keyword.