반응형
: key와 value로 이루어져 있는 자료구조
x = {
"name": "워니", # name에 워니를 넣어라
"age": 20,
}
print(x["name"])
print(x["age"])
//워니
//20
- key값에는 불변하는 값들만 들어갈 수 있다ex)문자열, 숫자
- 리스트는 가변이니까 dictionary의 key로 쓸 수 x
x = {
0: "Wonie",
1: "hello",
"age": 20,
}
print(x[0])
print(x[1])
print(x["age"])
//Wonie
//hello
//20
x = {
0: "Wonie",
1: "hello",
"age": 20,
}
print("age" in x) # "age"라는 key가 x안에 있는지 확인해보기
//True
x = {
0: "Wonie",
1: "hello",
"age": 20,
}
print(x.keys()) //dictionary에 있는 key들 다 출력
print(x.values())
//dict_keys([ 0, 1, 'age' ])
//dict_values(['Wonie', 'hello', 20])
x = {
0: "Wonie",
1: "hello",
"age": 20,
}
for key in x:
print("key: " + str(key))
print("value: " + str(x[key]))
//key: 0
//value: Wonie
//key: 1
//value: hello
//key: age
//value: 20
x = {
0: "Wonie",
1: "hello",
"age": 20,
}
# 0 key에 값을 "워니"로 바꾸기
x[0] ="워니"
print(x)
# 새로운 key값에 새로운 value넣기
x["school"] = "한빛"
print(x)
//{ 0: '워니', 1: 'hello', 'age': 20 }
//{ 0: '워니', 1: 'hello', 'age': 20 , 'school: "한빛' }
- dictionary는 원래 있는 값을 바꾸거나 새로운 값을 넣을 수 있다
반응형
'Python' 카테고리의 다른 글
[Python]class, object (0) | 2021.11.15 |
---|---|
[Python]과일 숫자 세는 프로그램 만들기(list, 조건문 사용) (0) | 2021.11.15 |
[Python]Tuple (0) | 2021.11.14 |
[Python]List (0) | 2021.11.14 |
[Python]조건문 (0) | 2021.11.13 |