From ab25567784961fc4ca7c80c20f6c287385173ffa Mon Sep 17 00:00:00 2001 From: ShinYooJung Date: Fri, 9 Dec 2022 15:08:05 +0900 Subject: [PATCH] Add Day 8 Korean Translation --- Korean/08_Day_Dictionaries/08_dictionaries.md | 342 ++++++++++++++++++ 1 file changed, 342 insertions(+) create mode 100644 Korean/08_Day_Dictionaries/08_dictionaries.md diff --git a/Korean/08_Day_Dictionaries/08_dictionaries.md b/Korean/08_Day_Dictionaries/08_dictionaries.md new file mode 100644 index 00000000..a418cac2 --- /dev/null +++ b/Korean/08_Day_Dictionaries/08_dictionaries.md @@ -0,0 +1,342 @@ +
+

30 Days Of Python: Day 8 - Dictionaries

+ + + + + Twitter Follow + + +Author: +Asabeneh Yetayeh
+ Second Edition: July, 2021 +
+ +
+ +[<< Day 7 ](../07_Day_Sets/07_sets.md) | [Day 9 >>](../09_Day_Conditionals/09_conditionals.md) + +![30DaysOfPython](../../images/30DaysOfPython_banner3@2x.png) + +- [πŸ“˜ Day 8](#-day-8) + - [Dictionaries](#dictionaries) + - [Creating a Dictionary](#creating-a-dictionary) + - [Dictionary Length](#dictionary-length) + - [Accessing Dictionary Items](#accessing-dictionary-items) + - [Adding Items to a Dictionary](#adding-items-to-a-dictionary) + - [Modifying Items in a Dictionary](#modifying-items-in-a-dictionary) + - [Checking Keys in a Dictionary](#checking-keys-in-a-dictionary) + - [Removing Key and Value Pairs from a Dictionary](#removing-key-and-value-pairs-from-a-dictionary) + - [Changing Dictionary to a List of Items](#changing-dictionary-to-a-list-of-items) + - [Clearing a Dictionary](#clearing-a-dictionary) + - [Deleting a Dictionary](#deleting-a-dictionary) + - [Copy a Dictionary](#copy-a-dictionary) + - [Getting Dictionary Keys as a List](#getting-dictionary-keys-as-a-list) + - [Getting Dictionary Values as a List](#getting-dictionary-values-as-a-list) + - [πŸ’» Exercises: Day 8](#-exercises-day-8) + +# πŸ“˜ Day 8 + +## Dictionaries + +DictionaryλŠ” μˆœμ„œκ°€ μ—†λŠ” μˆ˜μ •(λ³€ν˜•) κ°€λŠ₯ν•œ 쌍(ν‚€: κ°’)의 μžλ£Œν˜•μ˜ μ»¬λ ‰μ…˜μž…λ‹ˆλ‹€. + +### Creating a Dictionary + +Dictionaryλ₯Ό λ§Œλ“€λ €λ©΄ μ€‘κ΄„ν˜Έ {} λ˜λŠ” *dict()* λ‚΄μž₯ ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€. + +```py +# syntax +empty_dict = {} +# Dictionary with data values +dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'} +``` + +**Example:** + +```py +person = { + 'first_name':'Asabeneh', + 'last_name':'Yetayeh', + 'age':250, + 'country':'Finland', + 'is_marred':True, + 'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'], + 'address':{ + 'street':'Space street', + 'zipcode':'02210' + } + } +``` + +μƒλ‹¨μ˜ DictionaryλŠ” 값이 μ–΄λ–€ μžλ£Œν˜•μΌ μˆ˜λ„ μžˆλ‹€λŠ” 것을 λ³΄μ—¬μ€λ‹ˆλ‹€:string, boolean, list, tuple, set λ˜λŠ” dictionary. + +### Dictionary Length + +dictionary λ‚΄ 'key: value' 쌍의 개수λ₯Ό ν™•μΈν•©λ‹ˆλ‹€. + +```py +# syntax +dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'} +print(len(dct)) # 4 +``` + +**Example:** + +```py +person = { + 'first_name':'Asabeneh', + 'last_name':'Yetayeh', + 'age':250, + 'country':'Finland', + 'is_marred':True, + 'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'], + 'address':{ + 'street':'Space street', + 'zipcode':'02210' + } + } +print(len(person)) # 7 + +``` + +### Accessing Dictionary Items + +ν‚€μ˜ 이름을 톡해 λ”•μ…”λ„ˆλ¦¬ μ•„μ΄ν…œμ— μ ‘κ·Όν•  수 μžˆμŠ΅λ‹ˆλ‹€. + +```py +# syntax +dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'} +print(dct['key1']) # value1 +print(dct['key4']) # value4 +``` + +**Example:** + +```py +person = { + 'first_name':'Asabeneh', + 'last_name':'Yetayeh', + 'age':250, + 'country':'Finland', + 'is_marred':True, + 'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'], + 'address':{ + 'street':'Space street', + 'zipcode':'02210' + } + } +print(person['first_name']) # Asabeneh +print(person['country']) # Finland +print(person['skills']) # ['JavaScript', 'React', 'Node', 'MongoDB', 'Python'] +print(person['skills'][0]) # JavaScript +print(person['address']['street']) # Space street +print(person['city']) # Error +``` + +μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” ν‚€μ˜ μ΄λ¦„μœΌλ‘œ μ•„μ΄ν…œμ— μ ‘κ·Όν•  경우 μ—λŸ¬κ°€ λ°œμƒν•  수 μžˆμŠ΅λ‹ˆλ‹€. 이 μ—λŸ¬λ₯Ό ν”Όν•˜κΈ°μœ„ν•΄ μš°λ¦¬λŠ” μš°μ„  ν‚€κ°€ μ‘΄μž¬ν•˜λŠ”μ§€ ν™•μΈν•΄μ•Όν•©λ‹ˆλ‹€. λ˜λŠ” _get_ λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν• μˆ˜ μžˆμŠ΅λ‹ˆλ‹€. get λ©”μ„œλ“œλŠ” ν‚€κ°€ μ‘΄μž¬ν•˜μ§€ μ•Šμ„ 경우, NoneType object μžλ£Œν˜•μΈ None을 λ°˜ν™˜ν•©λ‹ˆλ‹€. +```py +person = { + 'first_name':'Asabeneh', + 'last_name':'Yetayeh', + 'age':250, + 'country':'Finland', + 'is_marred':True, + 'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'], + 'address':{ + 'street':'Space street', + 'zipcode':'02210' + } + } +print(person.get('first_name')) # Asabeneh +print(person.get('country')) # Finland +print(person.get('skills')) #['HTML','CSS','JavaScript', 'React', 'Node', 'MongoDB', 'Python'] +print(person.get('city')) # None +``` + +### Adding Items to a Dictionary + +λ”•μ…”λ„ˆλ¦¬μ— μƒˆλ‘œμš΄ 킀와 κ°’μ˜ μŒμ„ μΆ”κ°€ν•  수 μžˆμŠ΅λ‹ˆλ‹€. + +```py +# syntax +dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'} +dct['key5'] = 'value5' +``` + +**Example:** + +```py +person = { + 'first_name':'Asabeneh', + 'last_name':'Yetayeh', + 'age':250, + 'country':'Finland', + 'is_marred':True, + 'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'], + 'address':{ + 'street':'Space street', + 'zipcode':'02210' + } +} +person['job_title'] = 'Instructor' +person['skills'].append('HTML') +print(person) +``` + +### Modifying Items in a Dictionary + +λ”•μ…”λ„ˆλ¦¬μ˜ μ•„μ΄ν…œμ„ μˆ˜μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€ + +```py +# syntax +dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'} +dct['key1'] = 'value-one' +``` + +**Example:** + +```py +person = { + 'first_name':'Asabeneh', + 'last_name':'Yetayeh', + 'age':250, + 'country':'Finland', + 'is_marred':True, + 'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'], + 'address':{ + 'street':'Space street', + 'zipcode':'02210' + } + } +person['first_name'] = 'Eyob' +person['age'] = 252 +``` + +### Checking Keys in a Dictionary + +λ”•μ…”λ„ˆλ¦¬μ— ν‚€κ°€ μ‘΄μž¬ν•˜λŠ” 지 ν™•μΈν•˜κΈ° μœ„ν•΄ _in_ μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€ + +```py +# syntax +dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'} +print('key2' in dct) # True +print('key5' in dct) # False +``` + +### Removing Key and Value Pairs from a Dictionary + +- _pop(key)_: νŠΉμ • ν‚€ 이름을 가진 μ•„μ΄ν…œμ„ μ‚­μ œν•©λ‹ˆλ‹€ +- _popitem()_: λ§ˆμ§€λ§‰ μ•„μ΄ν…œμ„ μ‚­μ œν•©λ‹ˆλ‹€ +- _del_: νŠΉμ • ν‚€ 이름을 가진 μ•„μ΄ν…œμ„ μ‚­μ œν•©λ‹ˆλ‹€ + +```py +# syntax +dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'} +dct.pop('key1') # removes key1 item +dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'} +dct.popitem() # removes the last item +del dct['key2'] # removes key2 item +``` + +**Example:** + +```py +person = { + 'first_name':'Asabeneh', + 'last_name':'Yetayeh', + 'age':250, + 'country':'Finland', + 'is_marred':True, + 'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'], + 'address':{ + 'street':'Space street', + 'zipcode':'02210' + } + } +person.pop('first_name') # Removes the firstname item +person.popitem() # Removes the address item +del person['is_married'] # Removes the is_married item +``` + +### Changing Dictionary to a List of Items + +_items()_ λ©”μ„œλ“œλŠ” λ”•μ…”λ„ˆλ¦¬λ₯Ό νŠœν”Œμ˜ 리슀트둜 λ³€ν™˜ν•©λ‹ˆλ‹€. + +```py +# syntax +dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'} +print(dct.items()) # dict_items([('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3'), ('key4', 'value4')]) +``` + +### Clearing a Dictionary + +λ”•μ…”λ„ˆλ¦¬ λ‚΄μ˜ μ•„μ΄ν…œμ„ μ›ν•˜μ§€ μ•ŠλŠ”λ‹€λ©΄ _clear()_ λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•΄ λΉ„μšΈ 수 μžˆμŠ΅λ‹ˆλ‹€ + +```py +# syntax +dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'} +print(dct.clear()) # None +``` + +### Deleting a Dictionary + +λ”•μ…”λ„ˆλ¦¬λ₯Ό μ‚¬μš©ν•˜μ§€μ•ŠλŠ”λ‹€λ©΄ μ™„μ „νžˆ μ§€μšΈ 수 μžˆμŠ΅λ‹ˆλ‹€ + +```py +# syntax +dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'} +del dct +``` + +### Copy a Dictionary + +_copy()_ λ©”μ„œλ“œλ₯Ό 톡해 λ”•μ…”λ„ˆλ¦¬λ₯Ό 볡사할 수 μžˆμŠ΅λ‹ˆλ‹€. copyλ₯Ό μ‚¬μš©ν•΄ μ›λž˜ λ”•μ…”λ„ˆλ¦¬μ˜ λ³€ν™”λ₯Ό 막을 수 μžˆμŠ΅λ‹ˆλ‹€. + +```py +# syntax +dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'} +dct_copy = dct.copy() # {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'} +``` + +### Getting Dictionary Keys as a List + +_keys()_ λ©”μ„œλ“œλŠ” ν•˜λ‚˜μ˜ λ”•μ…”λ„ˆλ¦¬μ˜ λͺ¨λ“  ν‚€λ₯Ό 리슀트둜 μ€λ‹ˆλ‹€. + +```py +# syntax +dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'} +keys = dct.keys() +print(keys) # dict_keys(['key1', 'key2', 'key3', 'key4']) +``` + +### Getting Dictionary Values as a List + +_values_ λ©”μ„œλ“œλŠ” ν•˜λ‚˜μ˜ λ”•μ…”λ„ˆλ¦¬μ˜ λͺ¨λ“  값을 리슀트둜 μ€λ‹ˆλ‹€. + +```py +# syntax +dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'} +values = dct.values() +print(values) # dict_values(['value1', 'value2', 'value3', 'value4']) +``` + +πŸŒ• 당신은 정말 λ†€λΌμ›Œμš”. 이제, μ—¬λŸ¬λΆ„μ€ μ‚¬μ „μ˜ 힘으둜 μ™„μ „νžˆ μΆ©μ „λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€. μ—¬λŸ¬λΆ„μ€ 이제 막 8일째의 도전을 마쳀고 μœ„λŒ€ν•¨μ„ ν–₯ν•΄ 8보 μ „μ§„ν–ˆμŠ΅λ‹ˆλ‹€. 이제 μ—¬λŸ¬λΆ„μ˜ λ‡Œμ™€ κ·Όμœ‘μ„ μœ„ν•œ μš΄λ™μ„ ν•˜μ„Έμš”. + +## πŸ’» Exercises: Day 8 + +1. dogλΌλŠ” μ΄λ¦„μ˜ 빈 λ”•μ…”λ„ˆλ¦¬λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€ +2. dog λ”•μ…”λ„ˆλ¦¬μ— name, color, breed, legs, age λ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€ +3. student λ”•μ…”λ„ˆλ¦¬λ₯Ό μƒμ„±ν•˜κ³  first_name, last_name, gender, age, marital status, skills, country, city 와 address λ₯Ό ν‚€λ‘œ μΆ”κ°€ν•©λ‹ˆλ‹€ +4. student λ”•μ…”λ„ˆλ¦¬μ˜ 길이λ₯Ό μ–»μŠ΅λ‹ˆλ‹€ +5. skills 의 값을 μ–»κ³  μžλ£Œν˜•μ„ ν™•μΈν•©λ‹ˆλ‹€, list μ—¬μ•Ό ν•©λ‹ˆλ‹€ +6. ν•œκ°œλ‚˜ λ‘κ°œλ₯Ό μΆ”κ°€ν•΄ skills의 값을 μˆ˜μ •ν•©λ‹ˆλ‹€ +7. λ”•μ…”λ„ˆλ¦¬μ˜ ν‚€λ₯Ό 리슀트둜 μ–»μŠ΅λ‹ˆλ‹€ +8. λ”•μ…”λ„ˆλ¦¬μ˜ 값을 리슀트둜 μ–»μŠ΅λ‹ˆλ‹€ +9. _items()_ λ©”μ„œλ“œλ₯Ό μ΄μš©ν•΄ νŠœν”Œμ˜ 리슀트둜 λ”•μ…”λ„ˆλ¦¬λ₯Ό λ°”κΏ‰λ‹ˆλ‹€ +10. λ”•μ…”λ„ˆλ¦¬μ˜ μ•„μ΄ν…œμ€‘ ν•˜λ‚˜λ₯Ό μ‚­μ œν•©λ‹ˆλ‹€ +11. λ”•μ…”λ„ˆλ¦¬ 쀑 ν•˜λ‚˜λ₯Ό μ‚­μ œν•©λ‹ˆλ‹€ + +πŸŽ‰ CONGRATULATIONS ! πŸŽ‰ + +[<< Day 7 ](../07_Day_Sets/07_sets.md) | [Day 9 >>](../09_Day_Conditionals/09_conditionals.md)