반응형
# create new dictionary.
phone_book = {"John": 123, "Jane": 234, "Jerard": 345} # "John", "Jane" and "Jerard" are keys and numbers are values
print(phone_book)
# Add new item to the dictionary
phone_book["Jill"] = 345
print(phone_book)
# Remove key-value pair from phone_book
del phone_book['John']
print(phone_book['Jane']) # Print Jane's phone number from "phone_book".
산출물 (Output)
{'Jane': 234, 'Jerard': 345, 'John': 123}
{'Jane': 234, 'Jerard': 345, 'John': 123, 'Jill': 345}
234
{'Jane': 234, 'Jerard': 345, 'John': 123, 'Jill': 345}
234
* dictionary는 list와 비슷한데, index 대신 key를 찾음으로써 그 값에 접근할 수
있다는 점만 다릅니다.
key는 어떤 string 또는 number도 될 수 있습니다.
Dictionaries는 중괄호(curly braces)로 감쌉니다. 예, dct = {'key1' : "value1", 'key2' : "value2"}.
반응형
'Python 배우기' 카테고리의 다른 글
in_keyword.py (0) | 2016.03.08 |
---|---|
dict_key_value.py (0) | 2016.03.07 |
tuples.py (0) | 2016.03.07 |
list_items.py (0) | 2016.03.07 |
list_operations.py (0) | 2016.03.07 |
댓글