변수명 = { key1 : value1, key2 : value2 }lux = {'health' : 490, 'health' : 800}
>>lux['health']
800
lux = {'health': 490, 'health': 800, 'mana': 334, 'melee': 550, 'armor': 18.72}
lux
#{'health': 800, 'mana': 334, 'melee': 550, 'armor': 18.72}
x = {[10, 20]: 100}
#TypeError: unhashable type: 'list'
딕셔너리 = { }딕셔너리 = dict()딕셔너리 = dict(키1 = 값1, 키2 = 값2) : 키 - 값 형식으로 딕셔너리 만듦lux1 = dict(health=490, mana=334, melee=550, armor=18.72)
lux1
# {'health': 490, 'mana': 334, 'melee': 550, 'armor': 18.72}
딕셔너리 = dict(zip([키1, 키2], [값1, 값2])) : zip 함수로 키와 값 리스트를 묶음lux2 = dict(zip(['health', 'mana', 'melee', 'armor'], [490, 332, 550, 18.72]))
lux2
# {'health': 490, 'mana': 334, 'melee': 550, 'armor': 18.72}
딕셔너리 = dict([(키1, 값1), (키2, 값2)]) : (키, 값) 형식의 튜플로 딕셔너리를 만듦lux3 = dict([('health', 490), ('mana', 332), ('melee', 550), ('armor', 18.72)])
lux3
# {'health': 490, 'mana': 334, 'melee': 550, 'armor': 18.72}
딕셔너리 = dict({키1:값1, 키2:값2})lux4 = dict({'health': 490, 'mana': 332, 'melee': 550, 'armor': 18.72})
lux4
# {'health': 490, 'mana': 334, 'melee': 550, 'armor': 18.72}
딕셔너리 [키]lux = {'health': 490, 'mana': 334, 'melee': 550, 'armor': 18.72}
lux['health']
# 490
lux
#{'health': 490, 'mana': 334, 'melee': 550, 'armor': 18.72}
딕셔너리[키] = 값기존에 있던 값을 변경
lux = {'health': 490, 'mana': 334, 'melee': 550, 'armor': 18.72}
lux['health'] = 300
lux
#{'health': 300, 'mana': 334, 'melee': 550, 'armor': 18.72}
딕셔너리에 없는 키에 값을 할당하는 경우 - 해당 키가 추가된 후에 값이 할당
lux = {'health': 490, 'mana': 334, 'melee': 550, 'armor': 18.72}
lux['mana_regen'] = 3.28
lux
# {'health': 300, 'mana': 334, 'melee': 550, 'armor': 18.72, 'mana_regen' : 3.28}
lux = {'health': 490, 'mana': 334, 'melee': 550, 'armor': 18.72}
lux['attack_speed']
# KeyError: 'attack_speed'
키 in 딕셔너리lux = {'health': 490, 'mana': 334, 'melee': 550, 'armor': 18.72}
'health' in lux
# True
'attack_speed' in lux
#False
키 not in 딕셔너리'attack_speed' not in lux
# True
'health' not in lux
# False
코딩도장 에서 학습하면서 기록하고 있습니다😉