실습시간 예제

In [1]:
scores = [85, 70, 99]
subject_scores = ["±¹¾î",85,"¼öÇÐ",70,"¿µ¾î",99]
print(scores)
print(subject_scores)
[85, 70, 99]
['±¹¾î', 85, '¼öÇÐ', 70, '¿µ¾î', 99]
In [6]:
subject_scores_dic = {"±¹¾î":85,"¼öÇÐ":70,"¿µ¾î":99}
print(subject_scores_dic["±¹¾î"])
subject_scores_dic["±¹¾î"] = 66
print(subject_scores_dic)
85
{'±¹¾î': 66, '¼öÇÐ': 70, '¿µ¾î': 99}
In [7]:
print(subject_scores_dic.keys())
dict_keys(['±¹¾î', '¼öÇÐ', '¿µ¾î'])
In [8]:
print(subject_scores_dic.values())
dict_values([66, 70, 99])
In [9]:
print(subject_scores_dic.items())
dict_items([('±¹¾î', 66), ('¼öÇÐ', 70), ('¿µ¾î', 99)])
In [10]:
for item in subject_scores_dic.items() :
    print(item)
('±¹¾î', 66)
('¼öÇÐ', 70)
('¿µ¾î', 99)
In [11]:
for subject, score in subject_scores_dic.items() :
    print(subject,"°ú¸ñÀÇ Á¡¼ö´Â",score,"ÀÔ´Ï´Ù.")
±¹¾î °ú¸ñÀÇ Á¡¼ö´Â 66 ÀÔ´Ï´Ù.
¼öÇÐ °ú¸ñÀÇ Á¡¼ö´Â 70 ÀÔ´Ï´Ù.
¿µ¾î °ú¸ñÀÇ Á¡¼ö´Â 99 ÀÔ´Ï´Ù.
In [12]:
sum = 0
for subject, score in subject_scores_dic.items() :
    sum += score
average = sum / len(subject_scores_dic.items())
print("3°ú¸ñÀÇ ÇÕÀº",sum,"ÀÌ°í Æò±ÕÀº",average,"ÀÔ´Ï´Ù.")
3°ú¸ñÀÇ ÇÕÀº 235 ÀÌ°í Æò±ÕÀº 78.33333333333333 ÀÔ´Ï´Ù.
In [13]:
print(subject_scores_dic)
{'±¹¾î': 66, '¼öÇÐ': 70, '¿µ¾î': 99}
In [15]:
subject_scores_dic["°úÇÐ"]=88
print(subject_scores_dic)
print("Á¤·Ä ÈÄ:",sorted(subject_scores_dic))
{'±¹¾î': 66, '¼öÇÐ': 70, '¿µ¾î': 99, '°úÇÐ': 88}
Á¤·Ä ÈÄ: ['°úÇÐ', '±¹¾î', '¼öÇÐ', '¿µ¾î']
In [17]:
print(sorted(subject_scores_dic.items(),key=lambda x:x[0]))
[('°úÇÐ', 88), ('±¹¾î', 66), ('¼öÇÐ', 70), ('¿µ¾î', 99)]
In [18]:
print(sorted(subject_scores_dic.items(),key=lambda x:x[1]))
[('±¹¾î', 66), ('¼öÇÐ', 70), ('°úÇÐ', 88), ('¿µ¾î', 99)]
In [19]:
print(subject_scores_dic.get("°úÇÐ"))
88
In [20]:
print(subject_scores_dic.items())
dict_items([('±¹¾î', 66), ('¼öÇÐ', 70), ('¿µ¾î', 99), ('°úÇÐ', 88)])
In [21]:
print(subject_scores_dic.pop("°úÇÐ"))
print(subject_scores_dic.items())
88
dict_items([('±¹¾î', 66), ('¼öÇÐ', 70), ('¿µ¾î', 99)])
In [24]:
f = open("first_making_file.txt","w")
f.write("³»°¡ pythonÀ¸·Î ¸¸µç ù¹ø° ÆÄÀÏÀÔ´Ï´Ù.")
f.close()

f = open("first_making_file.txt","r")
s = f.read()
f.close()
print("ÆÄÀϳ»¿ë:",s)
ÆÄÀϳ»¿ë: ³»°¡ pythonÀ¸·Î ¸¸µç ù¹ø° ÆÄÀÏÀÔ´Ï´Ù.