Hello Guys ,
In previous tutorial you learned about List , List slicing , List function , Tuples and Tuples functions in python.
In this tutorial you will learn below points:
- Dictionary
- Nested Dictionary
- Dictionary Method
- Set
- Set Method
Dictionary
- Dictionary is a collection of key-value pairs.
- Dictionary is unordered.
- Dictionary is mutable.
- Dictionary is indexed.
- Dictionary can not contain duplicate keys.
- We can access specific value using specific key.
Example :
# Dictionary
'''
Dictionary is collection of key-value pairs
'''
MyDictionary = {
"Ps" : "Inf0" ,
1 : "psh" ,
"language" : "python"
}
print(MyDictionary)
'''
We can retrieve specific value using key.
Please define key in these [] brackets.
'''
print(MyDictionary["language"])
Output :
{'Ps': 'Inf0', 1: 'psh', 'language': 'python'}
python
Nested Dictionary
- We create dictionary in another dictionary called nested dictionary.
# Nested Dictionary
'''
We create dictionary in another dictionary
called Nested Dictionary.
'''
MyDictionary = {
"Ps" : "Inf0" ,
1 : "psh" ,
"language" : "python" ,
"anotherDictionary" : { "Blog" : "Ps_Inf0"}
}
print(MyDictionary)
Output :
{'Ps': 'Inf0', 1: 'psh', 'language': 'python',
'anotherDictionary': {'Blog': 'Ps_Inf0'}}
Dictionary Method
- items() method : items() method is used to display all key and values in pair.
Example :
MyDictionary = {
"Ps" : "Inf0" ,
1 : "psh" ,
"language" : "python"
}
# items() method is used to display all key and values in pair.
print(MyDictionary.items())
Output :
dict_items([('Ps', 'Inf0'), (1, 'psh'), ('language', 'python')])
- keys() method : keys() method is used to display all keys.
MyDictionary = {
"Ps" : "Inf0" ,
1 : "psh" ,
"language" : "python"
}
# keys() method is used to display all keys
print(MyDictionary.keys())
Output :
dict_keys(['Ps', 1, 'language'])
- update() method : update() method is used to add key values in dictionary.
MyDictionary = {
"Ps" : "Inf0" ,
1 : "psh" ,
"language" : "python"
}
# update() method is used to add key values in dictionary.
MyDictionary.update({"Hello" : "World"})
print(MyDictionary)
Output :
{'Ps': 'Inf0', 1: 'psh', 'language': 'python', 'Hello': 'World'}
Set
- Set is collection of non repetitive elements.
- Set are unordered.
- Set are immutable means we can not change value after creation of set.
- Set are unindexed.
- Set can not contain duplicate values.
# set
'''
We can create set using curly brackets {}.
'''
number = {1 , 2 , 3 , 4 , 5}
print(number)
'''
We can also check type of set.
'''
print(type(number))
'''
We can create empty set using set() function and
check the type of set.
'''
# Empty set
no = set()
print(type(no))
Output :
{1, 2, 3, 4, 5}
<class 'set'>
<class 'set'>
Set Method
- set() method : set () method is used to create empty set.
# Using set() method we create empty set
no = set()
print(type(no))
Output :
<class 'set'>
- add() method : add() method is used to add value in set.
# add() method is used to add value in set
no.add(1)
no.add(2)
no.add(3)
no.add(4)
no.add(5)
print(no)
Output :
{1, 2, 3, 4, 5}
- len() method : len() method is used to find length of the set.
number = {1 , 2 , 3 , 4 , 5}
print(len(number))
Output :
5
- remove() method : remove() method is used to remove specific value from set.
# remove() method is used to remove specific value from set
number = {1 , 2 , 3 , 4 , 5}
no.remove(4)
print(no)
Output :
{1, 2, 3, 5}
- pop() method : pop() method is used to remove element.
# pop() method is used to remove an element
print(no.pop())
print(no)
Output :
1
{2, 3, 5}
If you liked this post do comment ,share and promote the post 🙏 . Please stay with us and support. 🙏
0 Comments