모듈은 특정 기능을 가지고 있는 파이썬 파일로 사용자는 쉽게 사용할 수 있다.
name에는 모듈 이름이 저장되거나 ‘main’이 저장된다.

import 키워드를 이용해서 모듈을 임포트 한다.

as 키워드를 이용해서 모듈 이름을 단축 시킬 수 있다.

from ~ as 키워드를 이용해서 모듈의 특정 기능만 사용할 수 있다.

패키지를 이용하면 관련 있는 모듈을 그룹으로 관리할 수 있다.

site-packages에 있는 모듈은 어디서나 사용할 수 있다.


합계 sum
listVar = [2, 5, 3.14, 58, 10, 2]
print(f'sum(listVar): {sum(listVar)}')
최댓값 max
listVar = [2, 5, 3.14, 58, 10, 2]
print(f'max(listVar): {max(listVar)}')
최솟값 min
listVar = [2, 5, 3.14, 58, 10, 2]
print(f'min(listVar): {min(listVar)}')
거듭제곱 pow
print(f'pow(13, 2): {pow(13, 2)}')
print(f'pow(13, 3): {pow(13, 3)}')
print(f'pow(13, 4): {pow(13, 4)}')
반올림 round
print(f'round(3.141592, 2): {round(3.141592, 2)}')
print(f'round(3.141592, 3): {round(3.141592, 3)}')
절댓값 abs/fabs
print(f'math.fabs(-10): {math.fabs(-10)}')
print(f'math.fabs(-0.12895): {math.fabs(-0.12895)}')
올림 ceil
print(f'math.ceil(5.21): {math.ceil(5.21)}')
print(f'math.ceil(-5.21): {math.ceil(-5.21)}')
내림 floor
print(f'math.floor(5.21): {math.floor(5.21)}')
print(f'math.floor(-5.21): {math.floor(-5.21)}')
버림 trunc
print(f'math.trunc(5.21): {math.trunc(5.21)}')
print(f'math.trunc(-5.21): {math.trunc(-5.21)}')
최대공약수 gcd
print(f'math.gcd(14, 21): {math.gcd(14, 21)}')
팩토리얼 factorial
print(f'math.factorial(10): {math.factorial(10)}')
제곱근 sqrt
print(f'math.sqrt(4): {math.sqrt(4)}')
print(f'math.sqrt(12): {math.sqrt(12)}')
random
0이상 1미만 난수
print(f'random.random(): {random.random()}')
randint
1이상 20이하 난수
print(f'random.randint(1, 20): {random.randint(1, 20)}')
randrange
1이상 20미만 난수
print(f'random.randrange(1, 20): {random.randrange(1, 20)}')
sample
1이상 21미만 난수 5개
print(f'random.sample(range(1, 21), 5): {random.sample(range(1, 21), 5)}')
choice
무작위 한개 추출
listVar = [0, 1, 2, 3, 4, 5, 6]
print(f'random.choice(listVar): {random.choice(listVar)}')
print(f'listVar: {listVar}')
shuffle
random.shuffle(listVar)
print(f'shuffle listVar: {listVar}')
시스템 시간
lt = time.localtime()
print(f'time.localtime(): {lt}')
년 print(f'lt.tm_year: {lt.tm_year}')
월 print(f'lt.tm_mon: {lt.tm_mon}')
일 print(f'lt.tm_mday: {lt.tm_mday}')
시 print(f'lt.tm_hour: {lt.tm_hour}')
분 print(f'lt.tm_min: {lt.tm_min}')
초 print(f'lt.tm_sec: {lt.tm_sec}')
요일 print(f'lt.tm_wday: {lt.tm_wday}')