2주차 강의 주요 내용
- 함수
- 모듈
- 객체
- 클래스 상속
- 예외
- 텍스트 파일




인수와 매개변수는 짝을 이루어야하기 때문에, 갯수가 항상 일치해야 한다.
매개변수 개수가 정해지지 않은 경우 '*'를 사용

전역 변수: 함수 밖에서 선언된 변수로 어디서나 사용은 가능하지만 함수 안에서 수정 불가능

지역 변수: 함수 안에서 선언된 변수로, 내부에서만 사용 가능

lambda 키워드를 이용하면 함수 선언을 보다 간단하게 할 수 있다
기존 방법
def calculator(n1, n2): result = n1 + n2 return result returnValue = calculator(10, 20) print(f'returnValue: {returnValue}')
lambda 사용
calculator = lambda n1, n2: n1 + n2 # 함수 생성 및 calculator에 할당 returnValue = calculator(10, 20) print(f'returnValue: {returnValue}')
import calculator as cal
from calculator import add



객체 속성은 변경할 수 있다
class NewGenerationPC: def __init__(self, name, cpu, memory, ssd): self.name = name self.cpu = cpu self.memory = memory self.ssd = ssd def printPCInfo(self): print(f'self.name: {self.name}') print(f'self.cpu: {self.cpu}') print(f'self.memory: {self.memory}') print(f'self.ssd: {self.ssd}') myPC = NewGenerationPC('myPC', 'i5', '16G','256G') myPC.printPCInfo() myPC.cpu = 'i9' # 속성 변경 myPC.memory = '64G' myPC.ssd = '1T' myPC.printPCInfo()






다중 상속: 2개 이상의 클래스를 상속하는 것
class BasicCalculator: def add(self, n1, n2): return n1 + n2 def sub(self, n1, n2): return n1 - n2 def mul(self, n1, n2): return n1 * n2 def div(self, n1, n2): return n1 / n2 class DeveloperCalculator: def mod(self, n1, n2): return n1 % n2 def flo(self, n1, n2): return n1 // n2 def exp(self, n1, n2): return n1 ** n2 class Calculator(BasicCalculator, DeveloperCalculator): def __init__(self): pass cal = Calculator() print(f'cal.add(10,20) : {cal.add(10,20)}') print(f'cal.sub(10,20) : {cal.sub(10,20)}') print(f'cal.mul(10,20) : {cal.mul(10,20)}') print(f'cal.div(10,20) : {cal.div(10,20)}') print(f'cal.mod(10,20) : {cal.mod(10,20)}') print(f'cal.flo(10,20) : {cal.flo(10,20)}') print(f'cal.exp(2,5): {cal.exp(2,5)}')






except Exception as e:


class NotUseZeroException(Exception): def __init__(self, n): super().__init__(f'{n}은(는) 사용할 수 없습니다.') # 시스템에서 뜨는 exception창에 띄울 수 있음 def divCalculator(n1, n2): if n2 == 0: raise NotUseZeroException(n2) else: print(f'{n1} / {n2} = {n1/n2}') num1 = int(input('Input Number1 : ')) num2 = int(input('Input Number2 : ')) # 실제 처리 방법 try: divCalculator(num1, num2) except NotUseZeroException as e: print(f'NotUseZeroException : {e}')


r: 파일이 없으면 에러 발생
- w: 파일이 있으면 덮어씌움
- a: 파일이 있으면 덧붙임
- x: 파일이 있으면 에러 발생
with~as문 사용 시, 파일 닫기 생략 가능
writelines()는 리스트(List) 또는 튜플 데이터를 파일에 쓰기 위한 함수이다
languages = ['c/c++', 'java', 'c#', 'python', 'javascript'] uri = 'C:/pythonTxt/' #기존 방법 with open(uri + 'languages.txt', 'a') as f: f.write('item') f.write('\n') f.write(str(languages)) writelines() with open(uri + 'languages.txt', 'a') as f: f.writelines(item + '\n' for item in languages) #languages 에서 하나의 item을 넣어주는데, #그 아이템을 그대로 쓰는 것이 아닌 개행을 넣어서 사용 with open(uri + 'languages.txt', 'r') as f: str = f.read() print(f'str : {str}')

uri = 'C:/pythonTxt/' with open(uri + 'lans.txt', 'r') as f: lanLists = f.readlines() print(f'Lists : {lanLists}') print(f'Lists type : {type(lanLists)}') with open(uri + 'lans.txt', 'r') as f: lan = f.readline() while lan != '': print(f'Lan: {lan}', end='') lan = f.readline()

자료 출처: 제로베이스 데이터 스쿨 강의