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'
1# decleration
2my_dict = {
3 'spam': 'eggs',
4 'foo': 4,
5 100: 'bar',
6 2: 0.5
7}
8
9# access single values from the dictionary
10print(my_dict['spam']) # eggs
11print(my_dict['foo']) # 4
12print(my_dict[100]) # bar
13print(my_dict[2]) # 0.5
14
15# iterate over the dictionary
16for key, value in my_dict.items():
17 print(key, value)
18
19# get length of the dictionary
20print(len(my_dict)) # 4
21
22# modify the dictionary
23my_dict['baz'] = 'qux' # adds a pair
24my_dict['baz'] = 'quxx' # also updates it
25del my_dict['spam'] # removes a pair
26
27# other methods
28print(my_dict.copy()) # Returns a copy of the dictionary
29print(my_dict.fromkeys('added', 100)) # Returns a dictionary with the specified keys and their values
30print(my_dict.get('foo')) # Returns the value of the specified key
31print(my_dict.items()) # Returns a list containing a tuple for each key value pair
32print(my_dict.keys()) # Returns a list containing the dictionaries keys
33print(my_dict.values()) # Returns a list of all the values in the dictionary
34my_dict.setdefault('a', 'b') # Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
35my_dict.pop('foo') # Removes the element with the specified key
36my_dict.popitem() # Removes the last inserted key-value pair
37my_dict.update({'baz': 'val'}) # Updates the dictionary with the specified key-value pairs
38my_dict.clear() # Removes all the elements from the dictionary
1#dictionary
2programming = {
3 "Bugs": "These are the places of code which dose not let your program run successfully"
4 ,"Functions":"This is a block in which you put a peice of code"
5 ,"shell":"This is a place where the code is exicuted"
6 }
7print(programming["Bugs"])
8print(programming["shell"])
9#error
10#print(programming["pugs"])
1thisdict = {
2 "key1" : "value1"
3 "key2" : "value2"
4 "key3" : "value3"
5 "key4" : "value4"
6}
1thisdict = {
2 "brand": "Ford",
3 "model": "Mustang",
4 "year": 1964
5}
6x = thisdict["model"]
7print(x)
8---------------------------------------------------------------------------
9Mustang