Programming/Python

모듈 사용하기

쌩우 2019. 7. 21. 20:39

import 'module'

모듈을 사용할 땐, import 모듈명과 같은 방식으로 가능하다.

math

이름 그대로 math, 수학과 관련된 모듈이다.

  • math.pi => 원주율 값을 나타낸다
  • math.ceil(number) => number를 올림시킨다
  • math.floor(number) => number를 내림시킨다

random

무작위와 관련된 기능을 쓸 수 있다.

  • random.choice(list) => list 내에서 임의의 값을 선택해준다

urllib.request

웹 사이트의 내용을 가져오는 기능을 쓸 수 있다.

def get_web(url):
    """URL을 넣으면 페이지의 내용을 돌려주는 함수"""
    import urllib.request
    response = urllib.request.urlopen(url)
    data = response.read()
    decoded = data.decode('urf-8')
    return decoded

url = input('원하는 사이트의 주소를 입력해주세요')  
content = get_web(url)
print(content)