# Dictionaries
###########################################################
# 1、 Definition dictionary : Use {}、 Or use dict()
# An empty dictionary
name = {}
print(name)
# Non empty dictionary : left key Do not repeat , Dexter value Values are repeatable , Separated by commas , The last value can be followed by a comma or not
temp = {
'beijing': 28,
'shanghai': 35,
'shenzhen': 37,
'wuhan': 38,
'heilongjiang': 18
}
# print(temp)
# 2、 Dictionary basic operation
# 1) Directly know the temperature value in Beijing
# print(temp['beijing'])
# 2) Want to convert a list directly to a field
# templist = [('beijing', 28), ('shanghai', 35), ('shenzhen', 37), ('wuhan', 38), ('heilongjiang', 18)]
# temp1 = dict(templist)
# print(temp1)
# 3) Add a piece of data
# temp['hunan'] = 40
# print(temp)
# 4) Modify a piece of data in the dictionary
# temp['hunan'] = 39
# print(temp)
# 5) Delete a piece of data from the dictionary
# del temp['wuhan']
# print(temp)
###########################################################
# 2、 function :
# 1) a key : Use for Loop through the iteration dictionary
# for key, value in temp.items():
# print(key, value)
# 2) Value
# print(temp['Beijing']) # This method does not exist , Will report a mistake
# It should be noted that , If not included in the print dictionary key value , Will report a mistake , How can we avoid reporting errors ?
# print(temp.get('Beijing')) # If it doesn't exist , Will return None, This line of code is very important , Avoid us from making non empty judgments
# Give values that do not exist , Take a default value
# print(temp.get('Beijing', 30)) # The result is 30
# Above get Method is equivalent to the following code
# if 'Beijing' in temp:
# print(temp['Beijing'])
# else:
# print('Beijing', 30)
# 3) Print all key Sequence
print(temp.keys()) # Running results :dict_keys(['beijing', 'shanghai', 'shenzhen', 'wuhan', 'heilongjiang'])
# 4) Print all value value
print(temp.values()) # Running results :dict_values([28, 35, 37, 38, 18])
# Print values And
print(sum(temp.values())) # Running results :156