카테고리 없음

python - 8

chacharang 2021. 4. 27. 18:52

모듈 

 

코드의 길이가 길어지는 상황

이떄 모든 함수, 변수를 구현한느것은 불가능

-> 누군가 만들어 놓은 함수, 변수 등을 활용

 

 

모듈 특정 목적을 가진 함수, 자료의 모임

 

 

모듈 사용방법

 

모듈 불러오기

 

import(불러오다) 키워드를 이용해서 모듈 사용

import random #random 모듈불러오기

 

모듈 사용법

모듈 속 사용하려는 함수/변수의 사용법 확인

 

 

 

random.randrange(start.stop) 

 

range중 한가지 원소를 가지고 온다. 

print(random.randrange(0,2)

 

.(dot)을 쓴 후에 모듈 속 함수/변수 사용 

 

모듈 만들기

우리가 원하는 내용이 담긴 모듈 제작 가능

.py(파이썬)로 만들수 있다.

 

import my_module

 

#cal.py

def plus(a,b):

    c= a+b

    return c 

 

#main.py

import cal

print(cal.plus(3,4))

 

모듈 사용하기

import math

#math 모듈 불러오기

 

math.pi 

math.e 

 

import random#랜덤한 숫자를 선택해 주는 함수 들이 담긴 모듈

random.randrange(a,b)

a이상 b미만 하나를 반환

 

 

패키지

모듈을 폴터(directory)로 구분하여 관리하는 것

import user.cal    # dot 중요!

 

print(cal.plus(3,4))

 

from user.cal import plus  # user 폴더의 cal.py 파일에서 plus 함수만 가져오겠다.

 

함수, 변수 사용시 .를 써주지 않아도 된다.=

print (plus(3,4))