1+ # Dictionaries
2+ letslearnuser = {
3+ "name" :"saugat" ,
4+ "age" : 22 ,
5+ "course" :["python" ,"django" ,"js" ]
6+
7+
8+ }
9+ print (letslearnuser )
10+ #couse python
11+ print (letslearnuser ["course" ][0 ]) # this will give us the first element of the course list which is python
12+ #length
13+ print (len (letslearnuser )) # this will give us the number of key-value pairs in the dictionary which is 3
14+
15+
16+ #modifying
17+ letslearnuser ["age" ] = 23 # this will modify the value of the age key to 23
18+
19+ #adding a new key-value pair
20+ letslearnuser ["email" ] = "saugat@example.com"
21+ print (letslearnuser )
22+
23+ #keys
24+ keys = letslearnuser .keys () # this will give us a list of all the keys in the dictionary
25+ #keys after mutating
26+ print (keys ) # this will give us a list of all the keys in the dictionary after mutating it
27+ letslearnuser ["name" ] = "saugat shrestha" # this will modify the value of the name key to saugat shrestha
28+ letslearnuser ["surname" ] = "shrestha" # this will add a new key-value pair to the dictionary with the key surname and the value shrestha
29+ print (keys ) # this will give us a list of all the keys in
30+
31+ #values
32+ values = letslearnuser .values () # this will give us a list of all the values in the dictionary
33+ print (values ) # this will give us a list of all the values in the dictionary
34+ letslearnuser ["age" ] = 24 # this will modify the value of the age key
35+ print (values ) # this will give us a list of all the values in the dictionary after mutating it
36+
37+
38+ # items
39+ items = letslearnuser .items () # this will give us a list of all the key-value pairs in the dictionary as tuples
40+ print (items ) # this will give us a list of all the key-value pairs in the dictionary
41+ letslearnuser ["course" ].append ("react" ) # this will modify the value of the course key by appending react to the list
42+ print (items ) # this will give us a list of all the key-value pairs in the dictionary after mutating it
43+
44+
45+
46+ #if i want to acess the value name
47+ Items_dict = letslearnuser .items () # this will give us a list of all the key-value pairs in the dictionary as tuples
48+ print (Items_dict ) # this will give us a list of all the key-value pairs in the dictionary
49+
50+ # removing items
51+ letslearnuser .pop ("surname" ) # this will remove the key-value pair with the key surname from the dictionary
52+ print (letslearnuser )
53+ #del
54+ del letslearnuser ["email" ] # this will remove the key-value pair with the key email from the dictionary
55+ print (letslearnuser )
56+
57+ #clear
58+ letslearnuser .clear () # this will remove all the key-value pairs from the dictionary
59+ #del list
60+ del letslearnuser # this will delete the dictionary from memory
61+
62+ #nested dictionary
63+ nested_dict = {
64+ "name" :"saugat" ,
65+ "age" : 22 ,
66+ "course" :{
67+ "python" :"beginner" ,
68+ "django" :"intermediate" ,
69+ "js" :"advanced"
70+ }
71+ }
72+ print (nested_dict )
73+ #acessing begainers
74+ print (nested_dict ["course" ]["python" ]) # this will give us the value of the python key in the course dictionary which is beginner
75+
76+ #copy
77+ copy_dict = letslearnuser .copy () # this will give us a new dictionary with the same key-value pairs as letslearnuser
78+ print (copy_dict ) # this will give us a new dictionary with the same key-value pairs as
79+
80+ #copy using dict constructor
81+ copy_dict2 = dict (letslearnuser ) # this will give us a new dictionary with the same key-value pairs as letslearnuser
82+ print (copy_dict2 ) # this will give us a new dictionary with the same key-value pairs
0 commit comments