l1 = [120171122, 'Ahmed', 'MM Dept', 1]
# index 0 : student id
# index 1 : student name
# index 2 : department
# ....
print('department:', l1[2])
print('name:', l1[1])
dic = {}
print(dic)
# add using label (key)
# dic[0] use index in list but not in dictionary
dic['id'] = 120171122 # key = id, value = 120171122
dic['name'] = 'Ahmed'
dic['dept'] = 'MM'
dic['level'] = 1
print(dic)
print('id:', dic['id'])
key : value
bag = {'tissue': 5, 'money' : 15, 'candy' : 3, 'books' : 0, 'laptop' : 1}
print(bag)
bag_list = [5, 15, 3, 0, 1]
bag['money'] = 30
print(bag)
bag['phone'] = 1
print(bag)
bag['money'] = 0
print(bag)
bag['candy']+=2
print(bag)
print(bag['tissue'])
print(bag['pen'])
# if key in dictonary:
if 'pen' in bag:
print(bag['pen'])
'pen' in bag
# key in dictionary
'candy' in bag
# the value of the key
bag['candy']
# takes a key and return the value of the key
bag.get('pen')
print(bag.get('pen'))
# if key exists, retrurn the value, else return the default (0)
bag.get('pen', 0)
# if key exists, retrurn the value, else return the default ('not found')
bag.get('pen', 'not found')
bag.get('candy', 0)
bag.get('candy', 'not found')
names = ['ahmed', 'mohammed', 'ali', 'hassan', 'khalid', 'ahmed', 'mohammed']
print(names)
name_dict = dict() # same as name_count = {}
for name in names:
if name in name_dict:
name_dict[name] += 1
else:
name_dict[name] = 1
print(name_dict)
print('-------------------------')
print(name_dict)
name_dict['ahmed']
name_dict['khalid']
# name_dict['mahmood']
name_dict.get('mahmood', 0)
# find name count using get
name_dict = dict() # same as name_count = {}
for name in names:
# name_dict[name] += 1 in case name in the dict
# name_dict[name] = 0 + 1 in case name not in dict (add to the dictionary)
name_dict[name] = name_dict.get(name, 0) + 1
print(name_dict)
name_dict.items()
print(dir(name_dict))
bag = {'tissues': 30, 'candy' : 3, 'books': 2}
print(bag)
print(bag.items())
bag.get('candy', 0)
bag.get('pen', 0)
bag.clear()
print(bag)
bag = {'tissues': 30, 'candy' : 3, 'books': 2}
print(bag)
bag.keys()
bag.values()
bag.items()
bag.pop('candy')
print(bag)
bag.popitem()
print(bag)
bag2 = bag.copy()
print(bag2)
bag.update({'books': 2})
print(bag)
bag['pen'] = 2 # add. Similar to update
print(bag)
bag.setdefault('candy', 0)
bag.get('candy')
# bag.get('candy', 0)