군침이 싹 도는 코딩

PYTHON DICTIONARIES - 생성/데이터 억세스(get) 본문

Python/Basic

PYTHON DICTIONARIES - 생성/데이터 억세스(get)

mugoori 2022. 11. 16. 16:24

딕셔너리의 생성

{}
>>> {}

dict()
>>> {}
# 비어 있는 딕셔너리를 생성하는 법

my_phone={'brand' : 'Apple' , 'model':'iPhone X','year':2018}

my_phone
>>> {'brand': 'Apple', 'model': 'iPhone X', 'year': 2018}
# 딕셔너리는 {키:벨류} 의 한쌍으로 되어있다

 

 

 

딕셔너리의 억세스

my_phone
>>> {'brand': 'Apple', 'model': 'iPhone X', 'year': 2018}

my_phone['brand']
>>> 'Apple'

my_phone['model']
>>> 'iPhone X'
# 딕셔너리의 억세스 방법은 대괄호 안에 키값을 입력하는 것이다
# 키값을 입력하면 해당하는 벨류값이 나온다

my_phone.get('brand')
>>> 'Apple'

my_phone.get('model')
>>> 'iPhone X'
# get 함수를 이용하는 방법도 있다
# 이 역시 키값을 입력하면 벨류값을 출력한다