import 구문을 사용하며, 코드의 가장 위에 작성함import (모듈 이름)
from (모듈 이름) import (가져오고 싶은 변수 혹은 함수)
*기호를 사용하면 되나, 모든 것을 가져오면 식별자 이름에서 충돌이 일어날 수 있으므로 최대한 필요한 것만 가져와서 사용하는 것이 좋음import (모듈) as (사용하고 싶은 식별자)
import random as rd
print("# random 모듈")
# random(): 0.0 <= x < 1.0 사이의 float값을 리턴
print("- random():", random.rd())
# uniform(min, max): 지정한 범위 사이의 float를 리턴
print("- uniform(10, 20)", rd.uniform(10,20))
# randrange(): 지정한 범위의 int를 리던
# - randrange(max): 0부터 max 사이의 값을 리턴
# - randrange(min, max): min부터 max 사이의 값을 리턴
print("- randrange(10)", rd.randrange(10))
# choice(list): 리스트 내부에 있는 요소를 랜덤하게 선택
print("- choice([1, 2, 3, 4, 5])", rd.choice([1, 2, 3, 4, 5]))
# shuffle(list): 리스트의 요소를 랜덤하게 선택
print("- suffle([1, 2, 3, 4, 5]):", rd.suffle([1, 2, 3, 4, 5]))
# sample(list, k=(숫자)): 리스트의 요소 중에 k개를 뽑음
print("- sample([1, 2, 3, 4, 5], k=2):", rd.sample([1, 2, 3, 4, 5], k=2))
실행 결과
# random 모듈
- random(): 0.5671614057098718
- uniform(10, 20): 18.064138971352616
- randrange(10): 6
- choice([1, 2, 3, 4, 5]): 2
- suffle([1, 2, 3, 4, 5]): None
- sample([1, 2, 3, 4, 5], k=2): [5, 4]
import sys
# 명령 매개변수를 출력
print(sys.argv)
print("---")
# 컴퓨터 환경과 관련된 정보 출력
print("getwindowsversion():", sys.getwindowsversion())
print("---")
print("copyright:", sys.copyright)
print("---")
print("version:", sys.version)
sys.exit()
python module_sys.py 10 20 30을 입력한 후 코드를 실행하면, sys.argv에 ['module_sys.py', '10', '20', '30']이라는 리스트가 들어옴python module_sys.py filename.txt 등으로 입력하면 파일 경로 등을 외부에서 지정할 수 있음import os
# 기본 정보 출력
print("현재 운영체제:", os.name)
print("현재 폴더:", os.getcwd())
print("현재 폴더 내부의 요소:", os.listdir())
# 폴더를 만들고 제거(폴더가 비어있을 떄만 제거 가능)
os.mkdir("hello")
os.rmdir("hello")
# 파일을 생성하고 파일 이름을 변경
with open("original.txt", "w") as file:
file.write("hello")
os.rename("original.txt", "new.txt")
# 파일 제거
os.remove("new.txt")
#os.unlink("new.txt")
# 시스템 명령어 실행
os.system("dir)
rm -rf \와 같은 루트 권한이 있을 경우 무슨 명령어든 실행할 수 있으므로 주의해서 사용해야함time.sleep()
cmd (명령 프롬프트)창을 띄우고, 모듈 이름에 필요한 외부 모듈명을 작성하여 설치하면 됨pip install (모듈 이름)
import matplotlib.pyplot as plt
X = ['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun']
Y1 = [15.6, 14.2, 16.3, 18.2, 17.1, 20.2, 22.4]
Y2 = [20.1, 23.1, 23.8, 25.9, 23.4, 25.1, 26.3]
plt.plot(X, Y1, X, Y2)
plt.xlabel("day")
plt.ylabel("temperature")
plt.show()

import matplotlib.pyplot as plt
X = ['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun']
Y = [15.6, 14.2, 16.3, 18.2, 17.1, 20.2, 22.4]
plt.bar(X, Y)
plt.xlabel("day")
plt.ylabel("temperature")
plt.show()

pip numpy
>>> a = np.array([1, 2, 3, 4]) # 넘파이의 배열을 생성할 때, 반드시 대괄호를 사용하여 리스트 형식의 데이터를 인자로 사용해야함
>>> b = np.array([1, 'two', 3, 4])
>>> b # 자동 형 변환이 일어난 모습
array(['1', 'two', '3', '4'])