Skip to content

Commit d1e7f06

Browse files
committed
Added dictionary to Python
1 parent bd21a18 commit d1e7f06

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Dictionaries/dictionary.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Dictionary is collection of key-value pairs. Dictionary is ordered(from
2+
# python 3.6 onwards), changeable and does not contain any duplicates
3+
4+
capitals = {'US':'Washington',
5+
'UK':'London',
6+
'Germany':'Berlin',
7+
'South Korea':'Seoul'}
8+
print(capitals)
9+
10+
# To print value we will make use of specific key
11+
print(capitals['US'])
12+
# print(capitals['France']) # This will generate error as this not defined in dictionary
13+
# So to overcome this we will make use of .get
14+
print(capitals.get('France')) #This will give output 'NONE'
15+
16+
# To add a new key to dictionary we will make use of update
17+
capitals.update({'France':'Paris'})
18+
print("Dictionary was updated :",end="")
19+
print(capitals)
20+
21+
# To change an existing value of a key we make use of update
22+
capitals.update({'US':'Chicago'})
23+
print("Value for Washington was updated",end="")
24+
print(capitals)
25+
26+
# To remove a particular key-value pair we will make use of pop
27+
capitals.pop('US')
28+
print("Value popped is US: ",end="")
29+
print(capitals)
30+
31+
# To clear a list make use of clear
32+
capitals.clear()
33+
print("Dictionary Cleared: ",end="")
34+
print(capitals)

0 commit comments

Comments
 (0)