본문 바로가기

Python

Python [import]

 

1) import 방법

import math

 

print(math.ceil(1.2))

print(math.fsum([1, 2, 3]))

 

위 처럼 import 작성 후 import 함수를 사용하면 된다.

 

하지만, 위 처럼 외부 api 전체를 불러올 필요가 없다. (효율측면)

 

2) 특정 함수만 import

from math import ceil, fsum

 

print(ceil(1.2))

print(fsum([1, 2, 3]))

 

이렇게 하면 1, 2는 같은 결과 값을 갖는다.

 

3) import 한 함수의 명칭을 변경 할 수 있다.

 

from math import ceil, fsum as sum

 

print(ceil(1.2))

print(sum([1, 2, 3]))

 

- 위의 fsum 함수를 sum 으로 명칭 변경하여 사용 할 수 있다.

 

위 방식으로 다른 파일도 import 하여 함수를 사용 가능 * .py 를 붙이지 않아도 된다.

 

https://www.youtube.com/watch?time_continue=469&v=H995Ldft-s4&feature=emb_logo

 

 

 

 

'Python' 카테고리의 다른 글

Python [for in]  (0) 2019.11.24
Python [논리 연산자 and, or, not]  (0) 2019.11.24
Python if - elif - else  (0) 2019.11.24
Python [format] 스트링 안에 변수 값 넣고 출력  (0) 2019.11.24
Python [function] arg 위치 지정 호출  (0) 2019.11.24