1. 모듈 다루기
1import theater_module # 모듈 가져오기23theater_module.price(3) # 3명이 영화를 보러 갔을 때 가격4theater_module.price_morning(4) # 4명이 조조 영화를 보러 갔을 때 가격5theater_module.price_soldier(5) # 군인 5명이 영화를 보러 갔을 때 가격67import theater_module as mv # theater_module을 별명인 mv로 사용한다는 의미89mv.price(3)10mv.price_morning(4)11mv.price_soldier(5)1213from theater_module import * # theater_module에서 모든 기능을 가져와 사용함1415price(3) # theater_module.을 작성할 필요 없음16price_morning(4)17price_soldier(5)1819from theater_module import price, price_morning # 모듈에서 일부 함수만 가져와 사용함2021price(5) # 5명22price_morning(6) # 6명23price_soldier(7) # import하지 않아서 사용 불가2425# price_soldier를 별명인 price로 대체 사용26from theater_module import price_soldier as price2728price(5) # price_soldier() 함수 호출
2. 패키지 다루기
1import travel.thailand # travel 패키지의 thailand 모듈 가져오기23trip_to = travel.thailand.ThailandPackage()4trip_to.detail()56"""7import travel.thailand.ThailandPackage # 클래스 import 불가89trip_to = travel.thailand.ThailandPackage()10trip_to.detail()11"""1213# travel.thailand 모듈에서 ThailandPackage 클래스 가져오기14from travel.thailand import ThailandPackage1516trip_to = ThailandPackage() # from~import 문에서는 travel.thailand. 제외17trip_to.detail()
3. 모듈
3.1 모듈 공개 설정 -__all___
1from travel import *23trip_to = vietnam.VietnamPackage() # 베트남4# trip_to = thailand.ThailandPackage() # 태국5trip_to.detail()
3.2 모듈 직접 실행
1from travel import *23trip_to = thailand.ThailandPackage()4trip_to.detail()
4. 패키지, 모듈 위치 확인
1import inspect # 현재 모듈의 위치 확인하는 모듈2import random34print(inspect.getfile(random)) # random 모듈 위치(경로)56import inspect7from travel import *89print(inspect.getfile(thailand)) # thailand 모듈 위치
4.2 패키지 설치
1from bs4 import BeautifulSoup23soup = BeautifulSoup("<p>Some<b>bad<i>HTML")4print(soup.prettify())
5. 내장함수 사용
1language = input("어떤 언어를 좋아하세요? ")2print("{0}은 아주 좋은 언어입니다!".format(language))34print(dir())5import random # random 모듈 가져다 쓰기67print(dir())8import pickle # pickle 모듈 가져다 쓰기910print(dir())1112import random1314print(dir(random))1516lst = [1, 2, 3]17print(dir(lst))1819name = "Jim"20print(dir(name))
6. 외장함수 사용
6.1 glob
1# 파이썬 외장함수 리스트 https://docs.python.org/3/py-modindex.html2# glob : 경로 내의 폴더 / 파일 목록 조회 (윈도우 dir)3import glob45print(glob.glob("*.py")) # 확장자가 py인 모든 파일 출력
6.2 os
1# os : 운영체제에서 제공하는 기본 기능2import os34print(os.getcwd()) # 현재 작업 폴더 위치(경로)56folder = "sample_dir"78if os.path.exists(folder): # 같은 이름의 폴더가 존재한다면9print("이미 존재하는 폴더입니다.")10os.rmdir(folder) # 폴더 삭제11print(folder, "폴더를 삭제했습니다.") # 삭제 문구 출력12else: # 폴더가 존재하지 않으면13os.makedirs(folder) # 폴더 생성14print(folder, "폴더를 생성했습니다.")1516print(os.listdir()) # 현재 작업 폴더 안의 폴더와 파일 목록 출력
6.3 time
1import time23print(time.localtime())4print(time.strftime("%Y-%m-%d %H:%M:%S")) # 연-월-일 시:분:초56import datetime78print("오늘 날짜는", datetime.date.today())9today = datetime.date.today() # 오늘 날짜 저장10td = datetime.timedelta(days=100) # 100일째 날짜 저장11print("우리가 만난 지 100일은", today + td) # 오늘부터 100일 후 날짜
7. 퀴즈. 나만의 모듈 만들기
1"""2Quiz) 프로젝트 내에 나만의 시그니처를 남기는 모듈을 만드시오34조건 : 모듈 파일명은 byme.py로 작성56(모듈 사용 예제)7import byme8byme.sign()910(출력 예제)11이 프로그램은 나에 의해 만들어졌습니다.12"""13import byme1415byme.sign()
1def sign():2print("이 프로그램은 내가 만들었습니다.")3print("유승우")