키와 값이 쌍으로 존재하는 형태
형태 - {키 : 값}
첫 번째 키 값이 문자열이면 두 번째 키 값도 문자이어야 한다.
dic = {'애플': 'apple', '파이썬': 'python', '마이크로소프트': 'micro', '구글': 'google'}
for key, value in dic.items():
print(f'키 : {key}, 값 : {value}')
키 검색하기
# 키 검색 1
key = '구글'
if key in dic.keys():
print(f'key {key} 가 딕셔너리에 있습니다.')
else:
print(f'key {key} 가 딕셔너리에 없습니다.')
# 키 검색 2
find_key = '애플'
res = dic.get(find_key)
print('키를 검색해서 value 값을 리턴 : ', res)
# 없다면 None
추가하기
dic['삼성전자'] = 'samsung'
삭제하기
del dic['마이크로소프트']
print('삭제 후 딕셔너리 ', dic)
값 검색하기
value = 'apple'
if value in dic.values():
print(f'value {value} 가 딕셔너리에 있습니다.')
else:
print(f'value {value} 가 딕셔너리에 없습니다.')
연습 - 키 값을 입력받고 없으면 추가 있으면 삭제하기
strKey = input('키 값을 입력 : ')
if dic.get(strKey) is None:
print(f'key {strKey} 가 딕셔너리에 없습니다.')
dic[strKey] = 'samsung'
else:
del dic[strKey]
print(f'key {strKey} 가 딕셔너리에서 삭제했습니다.')
print('결과', dic)
'Python > Python Basic' 카테고리의 다른 글
10. 모듈화 Module (0) | 2023.02.07 |
---|---|
09. 튜플 Tuple (0) | 2023.01.16 |
07. 리스트 List 04 (0) | 2023.01.15 |
07. 리스트 List 03 (0) | 2023.01.15 |
06. 리스트 List 02 (0) | 2023.01.13 |