Dictionary
Dictionary
Another Example
class_room{"name":"Shahid","age":43}
# dict declration in a var called class_room
# having two key and value pairs
# one is name in which value is Shahid
# second is age in which value is 43
#Note sometime when you print out the dict it will be different orders
Need to be clarified
#exception handling
try:#error handling
print(my_dictionary [1])
except Exception as e:
print(Exception)
#result 1
Or if I pass index value
Adding more values in a dict a we can’t able to do that in tuple which is
immutable in nature
Mutability:
List is mutable so we can easily add similarly like list dict is also mutable
Take your variable dict open and closing square brackets all you have to
pass name of key and a value associate with this key to add new element in
the dict
Adding New Elements in the Dict
emp["salary"]=10000
print(emp["salary"])
print(emp)#salary added to the last value
Note :change of one object is also applied to other one if you don’t
want this happened create dcopy of the object
# List Example
user = ['Harshit', 24, ['coco', 'kimi no na wa'], ['awakening', 'fairy tale']]
# this list contains user name , age ,
Nested list me index me user ki fav movies ki list , fav tunes
# you can do this but this is not a good way to do this.
user_info = {
'name' : 'harshit',
'age' : 24,
("h","w"):(6,75),#tuple
'fav_movies' : ['coco', 'kimi no na wa'],#dictionary ke andar list
'fav_tunes' : ['awakening', 'fairy tale'],
}
print(user_info)
# if 'name' in d:
# print('present')
# else:
# print('not present')
# if d.get('names'):
# print('present')
# else:
# print('not present')