Test/Python(20220101~)

Day9_Dictionaries, Nesting

kiostory 2022. 1. 11. 13:59

딕셔너리: key, value

중첩

 

 

* Dictionary

    { key : value }

    { key1:value1, key2:value2, ... }

    키는 "문자", 숫자 123, 등 datatype을 구별한다

 

# Retrieving items for dictionary

programming_dictionary = {
    "Bug": "An error in a program that prevents the program from running as expected.",
    "Function": "A piece of code that you can easily call over and over again.",
    "Loop": "The action of doing something over and over again.",
}
print(programming_dictionary["Function"])
--------------------------------------------------------------------------------------------------
A piece of code that you can easily call over and over again.
 
# Adding new items to dictionary
programming_dictionary["kio"] = "Hansome guy in south Korea."
print(programming_dictionary)
--------------------------------------------------------------------------------------------------
{'Bug': 'An error in a program that prevents the program from running as expected.', 'Function': 'A piece of code that you can easily call over and over again.', 'Loop': 'The action of doing something over and over again.', 'kio': 'Hansome guy in south Korea.'}

 

# Create an empty dictionary

empty_dictionary = {}

 

# Wipe an existing dictionary

programming_dictionary = {}

 

# Edit an item in a dictionary

programming_dictionary["kio"] = "Really hansome" 

 

# Loop through a dictionary

for key in programming_dictionary :
print(key)
----------------------------------------------------------------------------------------------------------
Bug
Function
Loop
kio

for key in programming_dictionary :
    print(key)
    print(programming_dictionary[key]
----------------------------------------------------------------------------------------------------------
Bug
An error in a program that prevents the program from running as expected.
Function
A piece of code that you can easily call over and over again.
Loop
The action of doing something over and over again.
kio
Hansome guy in south Korea.