2주차(2) : Python 기본 자료형 다루기 (Dictionary, Tuple, Set)
딕셔너리변수 = {자료이름1: 자료값1, 자료이름2: 자료값2, 자료이름3: 자료값3}
b = {"math": 88, "english": 90, "history": 100}
print(b)
print(b["math"])
print(b["english"])
b = {1: 10, 2: 20}
print(b[1])
print(b[2])
scores = {"수학" : [50, 45, 65, 70, 80],
"영어" : [90, 20, 55, 60, 75],
"과학" : [20, 15, 65, 90, 95]}
print(scores)
print(scores["수학"])
english_average = sum(scores["영어"])/len(scores["영어"])
print("영어 평균점수는 " + str(english_average) + "점 입니다.")
scores = {"수학" : [50, 45, 65, 70, 80],
"영어" : [90, 20, 55, 60, 75],
"과학" : [20, 15, 65, 90, 95]}
print("key 값을 읽어와 불러옴.")
for key in scores:
print(key)
print(scores[key])
for key in scores.keys():
print(key)
print(scores[key])
print("\nvalue 값을 읽어와 불러옴.")
for score in scores.values():
print(score)
grade_per_lecture = {"현대디자인론" : 3,
"통합디자인연구" : 3,
"석사세미나" : 1,
"박사세미나" : 1,
"연구방법론" : 3,
"사용자인터페이스디자인연구" : 3}
grades = {"A+" : 4.3, "A0" : 4.0, "A-" : 3.7,
"B+" : 3.3, "B0" : 3.0, "B-" : 2.7,
"C+" : 2.3, "C0" : 2.0, "C-" : 1.7,
"D+" : 1.3, "D0" : 1.0, "D-" : 0.7, "F" : 0.0}
my_grades = {"현대디자인론" : "A-", "석사세미나" : "C+", "연구방법론" : "B0", "사용자인터페이스디자인연구" : "B+"}
튜플변수 = (자료값1, 자료값2)
a = (3, 5)
b = ("현대디자인론", "A+", 3)
c = (2, 5, (3, 9))
print(a[0])
print(b[1])
print(c[2][1])
grade_per_lecture = [("현대디자인론", 3),
("통합디자인연구", 3),
("석사세미나", 1),
("박사세미나", 1),
("연구방법론", 3),
("사용자인터페이스디자인연구", 3)]
for name, grade in grade_per_lecture:
print(name + " " + str(grade))
def two_largest_number(arr):
first = max(arr)
arr.remove(first)
second = max(arr)
return first, second
scores = [80, 90, 65, 82, 93, 79]
print(two_largest_number(scores))
a = 50
b = 30
print(a, b)
a, b = b, a
print(a, b)
집합변수명 = {집합자료1, 집합자료2, 집합자료 3}
a = {5, 3, 2}
b = {3, 5, 10}
print(a)
print(b)
print(a&b)
print(a|b)
print(a-b)
l = [5, 6, 7, 8, 8, 9, 9]
d = {"a" : 5, "b" : 6, "c" : 7}
t = (5, 6)
s = {8, 9, 3}
k = ["a", "b", "c", "d", "e", "f", "g"]
print(list(d), list(t), list(s))
print(dict(keys = k, values = l))
print(tuple(l), tuple(d), tuple(s))
print(set(l), set(d), set(t))