728x90

모듈

  • 코드를 분리하고 공유하는 기능

  • 표준 모듈 : 파이썬에 기본적으로 내장된 모듈

  • 외부 모듈 : 사람들이 만들어 공개한 모듈

import 모듈이름
 

The Python Standard Library — Python 3.8.1 documentation

The Python Standard Library While The Python Language Reference describes the exact syntax and semantics of the Python language, this library reference manual describes the standard library that is distributed with Python. It also describes some of the opt

docs.python.org

 

  • 모듈 사용하기 

import math
print(math.sin(1))
print(math.cos(1))
print(math.tan(1))
print(math.factorial(10))
... 

실행결과 : 
0.8414709848078965
0.5403023058681398
1.5574077246549023
3628800
  • from 구문 : module 이름 없이 변수나 함수 사용 가능

from math import sin, cos, tan, factorial
print(sin(1))
print(cos(1))
print(tan(1))
print(factorial(10))
... 

실행결과 :
0.8414709848078965
0.5403023058681398
1.5574077246549023
3628800
  • as 구문 : module의 이름을 줄여서 사용 가능

import math as m
print(m.sin(1))
print(m.cos(1))
print(m.tan(1))
print(m.factorial(10))
... 

실행결과 : 
0.8414709848078965
0.5403023058681398
1.5574077246549023
3628800
  • random 모듈 : 랜덤한 수를 생성하는 모듈

import random

print("random module")

print("random() : ", random.random())                               # 0 <= x < 1의 float 값
print("uniform(10,20) :", random.uniform(10,20))                    # 지정한 범위 내의 float 값
print("randrange(10) :", random.randrange(10))                      # 0 ~ 10까지 int 값
print("randrange(10,20) :", random.randrange(10,20))                # 10 ~ 20까지  int 값
print("choice([1,2,3,4,5]) :", random.choice([1,2,3,4,5]))          # 리스트 내의 요소 랜덤 선택
a = [1,2,3,4,5]
print("shuffle([1,2,3,4,5]) :", random.shuffle(a))                  # 리스트 요소를 랜덤으로 섞기
print(a)
print("sample([1,2,3,4,5]), k=2", random.sample([1,2,3,4,5],k=2))   # 리스트 요소 중 k 개를 뽑음

실행결과 : 
random module
random() :  0.10331388844593303
uniform(10,20) : 13.293293367032614
randrange(10) : 7
randrange(10,20) : 16
choice([1,2,3,4,5]) : 4
shuffle([1,2,3,4,5]) : None
[5, 3, 4, 1, 2]
sample([1,2,3,4,5]), k=2 [5, 1]
  • datetime 모듈 : 날짜 및 시간과 관련된 모듈

import datetime

now = datetime.datetime.now()
print("현재시간 : ",now)
print("년 :",now.year)
print("월 :",now.month)
print("일 :",now.day)
print("시 :",now.hour)
print("분 :",now.minute)
print("초 :",now.second)

# 포맷으로 시간 출력하기
formatA = now.strftime("%Y년 %m월 %d일 %H:%M:%S")     			       # Y = 2020 y = 20
formatB = "{}년 {}월 {}일 {}시 {}분 {}초".format(now.year,now.month,now.day,now.hour,now.minute,now.second)
formatC = now.strftime("%Y{} %m{} %d{} %H{} %M{} %S{}").format(*"년월일시분초") # 중괄호 자리에 한글자 씩 들어감


print(formatA)
print(formatB)
print(formatC)

실행결과 :
현재시간 :  2020-01-10 15:56:10.482231
년 : 2020
월 : 1
일 : 10
시 : 15
분 : 56
초 : 10
2020년 01월 10일 15:56:10
2020년 1월 10일 15시 56분 10초
2020년 01월 10일 15시 56분 10초
  • time 모듈  : 시간과 관련된 기능

import time

print("5초 후 끝")
time.sleep(5)
print("끝")

실행결과 : 
5초 후 끝
(5초 지나고)
끝
728x90

'Programming > Python' 카테고리의 다른 글

Python TCP 소켓 프로그래밍하기  (0) 2020.01.13
Python 예외 처리하기  (0) 2020.01.10
Python class 사용하기  (0) 2020.01.10
Python 터틀 그래픽 사용하기  (0) 2020.01.09
Python 파일 입출력하기  (0) 2020.01.09

+ Recent posts