Dictionary
宣告字典,與字典操作
emptyDictionary = {}
print(emptyDictionary)
print(type(emptyDictionary))
tom = {'name': 'Tom Wang', 'age': 20, 'height': 170, 'weight': 60, 'blood': 'B'}
mary = {'name': 'Mary Jane', 'age': 21, 'height': 165, 'weight': 55, 'blood': 'A'}
def printData(person):
print('{} age is {}, height is {}, weight is {}, blood is {}'
.format(person['name'], person['age'], person['height'], person['weight'], person['blood']))
printData(tom)
printData(mary)
# 這邊我們看到,就算dictionary裡面的item順序不一樣,只要值完全一樣,互相比對還是會相等的
tom2 = {'blood': 'B', 'name': 'Tom Wang', 'age': 20, 'height': 170, 'weight': 60}
print(tom == tom2)
print('next year~')
tom['age'] = 21
tom['height'] = 172
tom['weight'] = 61
printData(tom)Last updated