구조체(클래스)를 선언하고 사용하는 방법에 대해 배우게 됩니다.
__init__
: 클래스의 형태를 정의하는 생성자(constructor) 함수kor, eng, math
: 클래스가 선언될 때마다 값이 대입되는 멤버 변수class Student:
def __init__(self, kor, eng, math):
self.kor = kor
self.eng = eng
self.math = math
객체이름.멤버변수이름
형태로 값을 조회k, e, m
으로 정의했다면, 객체 생성 후 lee.k, lee.e, lee.m
으로 값을 조회lee = Student(90, 100, 80) # 객체(instance) 생성
print(lee.kor) # 90
print(lee.eng) # 100
print(lee.math) # 80
def num():
return 1, 2
a, b = num() # a, b = (1, 2) 와 같다
print(a, b, a + b) # 1 2 3
lee = (90, 100, 80)
k, e, m = lee
print(k, e, m) # 90 100 80
nums = 1, 2, 3, 4, 5
print(nums) # (1, 2, 3, 4, 5)
Runtime: 72ms, Memory: 29MB
# 클래스 선언
class meet:
def __init__(self, code, location, time):
self.code = code
self.location = location
self.time = time
# 변수 선언 및 입력
c, l, t = tuple(input().split())
# 객체 생성
ans = meet(c, l, t)
print(f"secret code : {ans.code}")
print(f"meeting point : {ans.location}")
print(f"time : {ans.time}")
''' Runtime: 102ms, Memory: 29MB
print("secret code :", ans.code)
print("meeting point :", ans.location)
print("time :", ans.time)
'''
class Student:
def __init__(self, kor=0, eng=0, math=0):
self.kor = kor
self.eng = eng
self.math = math
객체이름.매개변수이름 = 바꿀 값
으로 객체에 저장된 값 변경 가능kim = Student()
print(kim.kor) # 0
print(kim.eng) # 0
print(kim.math) # 0
kim.kor = 90
kim.eng = 80
kim.math = 100
print(kim.kor) # 90
print(kim.eng) # 80
print(kim.math) # 100
kim = (0, 0, 0)
k, e, m = kim
print(k, e, m) # 0 0 0
kim = (0, 0, 0)
k, e, m = kim
print(k, e, m) # 0 0 0
kim = (k, e, 100)
k, e, m = kim
print(k, e, m) # 0 0 100
kim = (90, 80, 100)
_, _, m = kim
print(m) # 100
class User:
def __init__(self, id = "codetree", level = "10"):
self.id = id
self.lv = level
input_id, input_lv = tuple(input().split())
user1 = User()
user2 = User(input_id, input_lv)
print(f'user {user1.id} lv {user1.lv}')
print(f'user {user2.id} lv {user2.lv}')
class Student:
def __init__(self, kor=0, eng=0, math=0):
self.kor = kor
self.eng = eng
self.math = math
students = []
for _ in range(5):
students.append(Student())
students = [Student() for _ in range(5)]
students = []
for _ in range(5):
kor, eng, math = tuple(map(int, input().split()))
students.append(Student(kor, eng, math))
student2 = students[1]
print(student2.kor, student2.eng, student2.math)
students = [tuple(map(int, input().split())) for _ in range(5)]
student2 = students[1]
kor, eng, math = student2
print(kor, eng, math)
class Agent:
def __init__(self, codename, score):
self.codename = codename
self.score = score
agents = []
for _ in range(5):
codename, score = input().split()
score = int(score)
agents.append(Agent(codename, score))
min_value = 10e9
for agent in agents:
if min_value > agent.score:
min_value = agent.score
ans = agent
print(ans.codename, ans.score)