Python
Python 개념 및 실습 - 모듈(1)
developers developing
2022. 9. 12. 09:00
모듈 : 함수나 변수 또는 클래스들을 모아 놓은 파일 . 패키지 정도
모듈 사용하기
- import 모듈명
- import 모듈명 as 별칭
- from 모듈명 import 모듈 함수(특정한 함수)
- from 모듈명 import 모듈 함수 as 별칭
- from 모듈명 import *== import 모듈명
# sys 모듈
import sys
print(sys.builtin_module_names)
파이썬이 기본으로 가지고 있는 모듈 확인
Ctrl + 마우스 오버 : 클래스를 확인 할 수 있음.
# math 모듈
import math # math 안에 있는 모든 것들이 import
print(dir(math))
print(math.__doc__) # ddefined by the C standard # C언어 기반
print(math.ceil(3.14))
print(math.sin(1))
print(math.cos(1))
print(math.floor(2.4))

# 필요한 모듈 함수만 불러오기.
from math import sin, cos, floor, ceil
print(sin(1))
print(cos(1))
print(floor(2.5))
print(ceil(2.5))

# as : 모듈명에 별칭 붙일 때 사용
import math as m
print(m.ceil(3.14))
print(m.sin(1))
print(m.cos(1))
print(m.floor(2.4))

# random 모듈
import random
# 0.0 <= x< 0 ~ 1 리턴
print(random.random())
# 지정한 범위 사이의 임의의 수 리턴
print(random.uniform(10, 20))
# randrange(max) : 0 ~ max 사이의 임의의 수 리턴
# randrange(min,max) : min~ max 사이의 임의의 수 리턴
print(random.randrange(10))
# choice(list) : 리스트 내부의 요소 중에서 임의의 수 리턴
print(random.choice([1, 2, 3, 4, 5, 6, 7]))
# shuffle(list) : 리스트 내부의 요소 섞기
list1 = [1, 2, 3, 4, 5, 6, 7]
print("shuffle 전 ", list1)
random.shuffle(list1)
print("shuffle 후 ", list1)
# sample(list, 숫자) : 리스트 요소 중에서 숫자 만큼 추출
print(random.sample([1, 2, 3, 4, 5, 6, 7], k=2))
# time 모듈
import time
print("지금부터 5초간 정지")
time.sleep(5)
print("프로그램 종료")