Programming
-
[Python] 27. 모듈(module)과 패키지(package)Programming/Python 2021. 7. 26. 16:20
[1] 모듈이란? # theater_module.py # 영화표 가격 모듈 def price(people) : print(f"{people}명 가격은 {people * 10000}원 입니다.") def price_morning(people) : print(f"{people}명 조조할인 가격은 {people * 6000}원 입니다.") def price_soldier(people) : print(f"{people}명 군인할인 가격은 {people * 4000}원 입니다.") - 모듈이란, 함수나 클래스 등을 모아 파일로 만들어 놓은 것이라 생각 가능하다. - 모듈파일은 사용하고자하는 py파일과 같은 폴더 내에 있거나, 모듈이 모아져있는 폴더에 있어야한다. [2] 모듈의 사용 1) import # prace..
-
[Python] 26. 예외 처리Programming/Python 2021. 7. 26. 14:36
[1] 예외 처리란? # 나누기 전용 계산기 만들기 a = int(input("Please enter the number : ")) b = int(input("Please enter the number : ")) print(f"{a} ÷ {b} = {a/b}") Please enter the number : 10 Please enter the number : 가 Traceback (most recent call last): File "c:/Users/82102/Desktop/sw/PythonWorkspace/test.py", line 3, in b = int(input("Please enter the number : ")) # 가 입력 ValueError: invalid literal for int() ..
-
[Python] 25. pass와 super, isinstance()Programming/Python 2021. 7. 25. 16:21
[1] pass class human : def __init__(self, name, age) : pass khumsfcr = human("john", 24) - pass를 사용하면, 코드를 완성하지 않아도 오류가 발생하지 않는다. - 클래스의 생성자와 메소드 뿐만 아니라, 일반 함수에서도 사용 가능하다. [2] super class human : def __init__(self, name, age) : self.name = name self.age = age def show(self) : print(f"저의 이름은 {self.name}, 나이는 {self.age}") class khumsfcr(human) : def __init__(self, name, age, sex) : super().__init_..
-
[Python] 24. 상속Programming/Python 2021. 7. 25. 15:52
[1] 상속이란? class human : def __init__(self, name, age, blood_type) : self.name = name self.age = age self.blood_type = blood_type def show(self) : print(f"저의 이름은 {self.name}, 나이는 {self.age}, 혈액형은 {self.blood_type}") class khumsfcr(human) : def __init__(self, name, age, blood_type, sex, glass) : human.__init__(self, name, age, blood_type) self.sex = sex self.glass = glass khumsfcr = khumsfcr("john..
-
[Python] 23. 클래스(class)Programming/Python 2021. 7. 25. 15:05
[1] 클래스 사용 이유 puppy_name = "happy" puppy_age = 3 puppy_leg = 4 puppy_color = "흰색" cat_name = "beauty" cat_age = 6 cat_leg = 4 cat_color = "검은색" print(f"저의 반려동물 이름은 {puppy_name}, 나이는 {puppy_age}, 다리 수는 {puppy_leg}, 털 색은 {puppy_color}입니다.") print(f"저의 반려동물 이름은 {cat_name}, 나이는 {cat_age}, 다리 수는 {cat_leg}, 털 색은 {cat_color}입니다.") 저의 반려동물 이름은 happy, 나이는 3, 다리 수는 4, 털 색은 흰색입니다. 저의 반려동물 이름은 beauty, 나이는 6..
-
[Python] 22. withProgramming/Python 2021. 7. 20. 17:10
[1] 일반적인 파일 입출력에서의 사용 # 입력 with open("profile.txt", "w", encoding = "utf8") as profile_file: profile_file.write("im khumsfcr, im 24 years old.") # 출력 with open("profile.txt", "r", encoding = "utf8") as profile_file: print(profile_file.read()) im khumsfcr, im 24 years old. - with를 통해 일반적인 파일 입출력을 간단하게 할 수 있다. - close가 필요없다. [2] pickle에서의 사용 import pickle # 입력 with open("profile.pickle", "wb") as ..
-
[Python] 21. 피클(pickle)Programming/Python 2021. 7. 20. 17:01
import pickle #데이터를 파일에 쓰기 profile_file = open("profile.pickle", "wb") profile = {"name" : "john", "age" : 17, "hobby" : ["soccer", "coding", "golf"]} pickle.dump(profile, profile_file) profile_file.close() #파일을 데이터로 가져오기 profile_file = open("profile.pickle", "rb") profile2 = pickle.load(profile_file) profile_file.close() print(profile2) {'name': 'john', 'age': 17, 'hobby': ['soccer', 'coding', ..
-
[Python] 20. 파일 입출력Programming/Python 2021. 7. 20. 16:49
[1] 기본적인 파일 입출력 # 입력 score_file = open("score.txt", "w", encoding = "utf8") print("math : 80", file = score_file) print("coding : 100", file = score_file) score_file.close() # 파일을 open한 후 꼭 닫아주어야 한다. # 출력(읽기) score_file = open("score.txt", "r", encoding = "utf8") print(score_file.read()) # math : 80 # coding : 100 score_file.close() - open을 통해 score.txt를 쓰기전용으로(w) 연다. 이때, encoding은 한글을 읽기 위함이다. ..