본문 바로가기
IT관련/Python

[확고의 쓱~ 훑어보는 기록] Python - dict 사전 예제(2) (데이터 확인, dict함수(get, update, clear))

by 확고 2022. 7. 28.
728x90
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#-*- coding:utf-8
'''
Created on 2022. 7. 28.
 
@author: hwakgo
'''
 
score = {'방구뽕':100,'우투더영투더우':1000}
print(score)#▶{'방구뽕': 100, '우투더영투더우': 1000}
 
#우변 점수 갖고오기
print(score['우투더영투더우'])#▶1000
 
#★ dict함수 4 - get(키) : value 값을 가져옴, dict에 없는 키를 넣으면 None
print(score.get('우투더영투더우'))#▶1000
print(score.get('준ㄷ'))#▶None
 
if score.get('봄날의햇살최수연'== None :#만약 dict 변수 score에 봄날의햇살최수연의 value가 None이면 'dict에추가하시오'출력
    print('dict에추가하시오')
else :
    print(score.get('봄날의햇살최수연'))#아니면 value값 출력
#▶dict에추가하시오
 
#(문제) 이름을 입력받아서 dict에 포함되어 있는지 비교하고 없으면 '조회불가'를 출력하고 있으면 value값을 출력하시오
name = input('이름: ')#방구뽕 입력
if score.get(name) == None :
    print('조회불가')
else :
    print('점수:',score.get(name))
#▶점수: 70
 
score2 = {'준호':1000,'권모술수':999,'우투더영투더우':1004}
 
#score의 데이터를 score2로 변경하기
#★ dict함수 5 - update() : 데이터 업데이트할 때 사용(데이터 변경 또는 추가)
score.update(score2)
print(score)#▶{'방구뽕': 100, '우투더영투더우': 1004, '준호': 1000, '권모술수': 999}
 
#★ dict함수 6 - clear() : 데이터 전체 비울 때 사용(dict 전체 삭제X / 데이터만 삭제함)
score.clear()
print(score)#▶{}
cs

 

▼ (이전 게시물) [확고의 쓱~ 훑어보는 기록] Python - 사전 예제(1) (생성/추가/변경, dict함수(keys, values, items))

 

[확고의 쓱~ 훑어보는 기록] Python - dict 사전 예제(1) (생성/추가/변경, dict함수(keys, values, items))

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69..

j-growthdiary.tistory.com

 

▼ (첫 번째 게시물) [확고의 쓱~ 훑어보는 기록] Python - 출력 예제 (print / str, int 함수 / 서식 문자 사용)

 

[확고의 쓱~ 훑어보는 기록] Python - 출력 예제 (print / str, int 함수 / 서식 문자 사용)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 #-*- coding:utf-8 ''' Created on ..

j-growthdiary.tistory.com

728x90
반응형

댓글