파이썬 중급

조지원·2023년 5월 11일
0

python

목록 보기
7/15


💻 keep going

갈수록 너무 어렵고 잘 할 수 있을지, 끝까지 할 수 있을지 모르겠지만 그냥 해보자.


💡 클래스 상속

ex)

class CalculatorSuper:

    def add(self, n1, n2):
        return n1 + n2

    def sub(self, n1, n2):
        return  n1 - n2


class CalculatorChild(CalculatorSuper):

    def mul(self, n1, n2):
        return n1 * n2


    def div(self, n1, n2):
        return n1 / n2


cal = CalculatorChild()
print(f"cal.add(10,10) : {cal.add(10, 20)}")
print(f"cal.sub(10,10) : {cal.sub(10, 20)}")
print(f"cal.mul(10,10) : {cal.mul(10, 20)}")
print(f"cal.div(10,10) : {cal.div(10, 20)}")


💡 생성자

기능 같은 경우에는 상속만 하면 사용할 수 있지만 속성은 __init__ 메소드를 호출해야한다.

실습 :



💡 다중 상속

필요한 경우에만 사용

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 NewCalculator(BasicCalculator, DeveloperCalculator):

    def __init__(self):
        pass

cal = NewCalculator()


💡 오버라이딩

재정의



💡 추상클래스

어떠한 공통적인 기능을 쓰는데 각자 알아서 구현해서 사용해야 할때.

@ abstractmethod 적용된 함수는 반드시 구현을 해줘야함 상속받은 클래스에서.



💡 예외


💡 예외 종류


💡 예외 처리

💡 try ~ except


💡 ~ else

예외가 발생하지 않았을 경우


💡 finally



💡 exception class

💡 raise



💡 사용자 예외 클래스



텍스트 파일 쓰기

💡 기본함수

💡 파일쓰기

import time

lt = time.localtime()

dateStr = "[" + str(lt.tm_year) + "년" + str(lt.tm_mon) + "월" + \
    str(lt.tm_mday) + "일]"

todaySchedule = input("오늘 일정 : ")

file = open("C:/pythonTxt/new.txt", "w")  
file.write(dateStr + todaySchedule)
file.close()                    
  • 마지막에 꼭 닫아주기 👉 .close()
  • 내용은 계속 덮어 씌워진다.
  • 방향 /로 꼭 바꿔주기 👉 C:/pythonTxt/


💡 텍스트 파일 읽기

  • file = open("C:/pythonTxt/test.txt", "r", encoding="UTF8")
    👉 encoding="UTF8" : 인코등 오류 날경우 사용 (cp949 Error)


💡 텍스트 파일 열기



💡 with ~ as 문



💡 writelines()



💡 readline(), readlines()



⚡ Check

✔ str.replace("Python", "파이썬", 2)
👉 Python 을 파이썬으로 2번째까지 변경

✔ print(f"공과금 : {utilily.getUtilityBill():,.0f}원")
👉f"" 함수에서 :,.0f = 1000단위씩 , 적용

profile
keep going

0개의 댓글