이미 만들어진 훌륭한 기능으로 사용자는 쉽게 사용할 수 있으며, 특정 기능을 가지고 있는 Python 파일을 말한다.
내부 모듈
python 설치 시, 기본적으로 사용할 수 있는 모듈
외부 모듈
별도 설치 후, 사용할 수 있는 모듈
사용자 모듈
사용자가 직접 만든 모듈
관련 있는 모듈을 그룹으로 관리할 수 있다.
사용 방법은 다음과 같다.from 패키지명 import 모듈명
site-packages에 있는 모듈은 어디서나 사용할 수 있다.
- math module
- 수학 관련 모듈이다.
import math
sum() # 합
max() # 최댓값
min() # 최솟값
pow() # 거듭제곱
round() # 반올림
math.fabs() # 절댓값
math.ceil() # 올림
math.trunc() # 버림
math.gcd() # 최대공약수
math.factorial # 팩토리얼
math. sqrt() # 제곱근
- random module
- 난수 관련 모듈이다.
import random
# 1부터 10까지의 정수 중 난수 1개 발생시키기
random.randint(1, 10)
# 0부터 100사이의 난수 10개 발생시키기
random.sample(range(1, 101), 10)
- time module
- 시간 관련 모듈이다.
import time
lt = time.localtime()
lt.tm_year
lt.tm_mon
lt.tm_mday
lt.tm_hour
lt.tm_min
lt.tm_sec
lt.tm_wday
- datetime module
- 날짜와 시간을 처리하기 위한 모듈이다.
import datetime
today = datetime.datetime.today() # 현재 날짜와 시간
day = today.day # 날짜만
* 이 글은 제로베이스 데이터 스쿨의 강의 자료 일부를 발췌하여 작성되었습니다.