파이썬 모듈 맛보기
모듈
- 같은 폴더의 다른 파일에 있는 함수를 불러올 수 있다.
- import [file] as [name] : file의 이름 대신으로 name을 사용할 수 있다.
- from [file] import [function] : file의 함수 중에서 특정 function 몇 개만을 가져올 수 있다.
def add(x,y):
return x + y
def subtract(x,y):
return x + y
def multiply(x,y):
return x * y
def divide(x,y):
return x / y
import calculator as calc
print(calc.add(2, 5))
print(calc.multiply(3, 4))
from calculator import add, multiply
from calculator import *
print(add(2, 5))
print(multiply(3, 4))
스탠다드 라이브러리 (Standard Library)
- 파이썬에 기본으로 내장되어 있는 라이브러리를 불러와서 사용할 수 있다.
- math, random, os 등이 있다.
import math
print(math.log10(100))
print(math.cos(0))
print(math.pi)
import random
print(random.random())
print(random.randint(1, 20))
print(random.uniform(0, 2))
import os
print(os.getlogin())
print(os.getcwd())
import datetime
pi_day = datetime.datetime(2020, 3, 14, 13, 6, 15)
print(pi_day)
print(type(pi_day))
today = datetime.datetime.now()
print(today)
print(today.year)
print(today.hour)
print(today.second)
print(today.strftime("%A, %B %dth %Y"))
print(today-pi_day)
print(type(today-pi_day))