1#Creating dictionaries
2dict1 = {'color': 'blue', 'shape': 'square', 'volume':40}
3dict2 = {'color': 'red', 'edges': 4, 'perimeter':15}
4
5#Creating new pairs and updating old ones
6dict1['area'] = 25 #{'color': 'blue', 'shape': 'square', 'volume': 40, 'area': 25}
7dict2['perimeter'] = 20 #{'color': 'red', 'edges': 4, 'perimeter': 20}
8
9#Accessing values through keys
10print(dict1['shape'])
11
12#You can also use get, which doesn't cause an exception when the key is not found
13dict1.get('false_key') #returns None
14dict1.get('false_key', "key not found") #returns the custom message that you wrote
15
16#Deleting pairs
17dict1.pop('volume')
18
19#Merging two dictionaries
20dict1.update(dict2) #if a key exists in both, it takes the value of the second dict
21dict1 #{'color': 'red', 'shape': 'square', 'area': 25, 'edges': 4, 'perimeter': 20}
22
23#Getting only the values, keys or both (can be used in loops)
24dict1.values() #dict_values(['red', 'square', 25, 4, 20])
25dict1.keys() #dict_keys(['color', 'shape', 'area', 'edges', 'perimeter'])
26dict1.items()
27#dict_items([('color', 'red'), ('shape', 'square'), ('area', 25), ('edges', 4), ('perimeter', 20)])
1#title : Dictionary Example
2#author : Joyiscold
3#date : 2020-02-01
4#====================================================
5
6thisdict = {
7 "brand": "Ford",
8 "model": "Mustang",
9 "year": 1964
10}
11
12#Assigning a value
13thisdict["year"] = 2018
1#title :Dictionary Example
2#author :Josh Cogburn
3#date :20191127
4#github :https://github.com/josh-cogburn
5#====================================================
6
7thisdict = {
8 "brand": "Ford",
9 "model": "Mustang",
10 "year": 1964
11}
12
13#Assigning a value
14thisdict["year"] = 2018
1student_data = {
2 "name":"inderpaal",
3 "age":21,
4 "course":['Bsc', 'Computer Science']
5}
6
7#the keys are the left hand side and the values are the right hand side
8#to print data you do print(name_of_dictionary['key_name'])
9
10print(student_data['name']) # will print 'inderpaal'
11print(student_data['age']) # will print 21
12print(student_data['course'])[0]
13#this will print 'Bsc' since that field is an array and array[0] is 'Bsc'
1thisdict = {
2 "brand": "Ford",
3 "model": "Mustang",
4 "year": 1964
5}
6print(thisdict["brand"])