Python

[Python]모듈(Module)

sagesse2021 2021. 11. 20. 14:40
반응형

math : 값을 올림해서 반환

import math   #import를 사용해서 module가져오기

print(math.ceil(1.2))

2

 

fabs :절대값 반환

import math

print(math.fabs(-1.2))

1.2

  • math를 import하면 기능을 다 가져오게 됨, math에 있는 기능 전부 import할 필요는 x

//특정 함수만 가져오려면 from.. import사용

from math import ceil, fsum

print(ceil(1.2))
print(fsum([1,2,3,4,5,6,7]))

2

28.0

  • 항상 사용할 것만 import 한다, module전부를 가져올 필요x
from math import fsum as fancy_sum  #as를 사용해서 이름 지정 가능
print(fancy_sum([1,2,3,4,5,6,7]))

28.0

# claculator.py
def plus(a,b):
  return a + b
# main.py
from calculator import plus  //파일명만 써주면됨
print(plus(1,2))
  • calculaor파일에서 정의된 기능을 import해서 사용할 수 있다
  • calculator.py에서 main. py로 기능을 가져와서 쓸 수 있다
# claculator.py

def plus(a,b):
  return a + b

def minus (a, b):
  return a - b
# main.py

from calculator import plus, minus
print(plus(1,2), minus(1,2))

3 -1

반응형

'Python' 카테고리의 다른 글

[Python]온라인에 있는 라이브러리 사용해서 url 가져오기  (0) 2021.11.20
[Python]for in 반복문  (0) 2021.11.19
[Python]if...else, elif..or/and  (0) 2021.11.19
[Python]Code Challenge  (0) 2021.11.19
[Python]Keyworded Arguments  (0) 2021.11.19