본문 바로가기
프로그래밍/Python

[Python]파이썬 from import

by wyatti 2023. 5. 12.

 

파이썬 from import 파이썬에서 from과 import는 모듈을 불러오는 데 사용되는 예약어입니다. from은 모듈 내에서 특정 객체(함수, 변수 등)를 가져올 때 사용됩니다.

import math는 math 모듈 전체를 가져오는 것을 의미합니다. 이를 통해 파이썬 프로그래밍에서 필요한 다양한 모듈을 불러와 사용할 수 있습니다.
파이썬 from import

파이썬에서 from과 import는 모듈을 불러오는 데 사용되는 예약어입니다. from은 모듈 내에서 특정 객체(함수, 변수 등)를 가져올 때 사용됩니다.

import math는 math 모듈 전체를 가져오는 것을 의미합니다. 이를 통해 파이썬 프로그래밍에서 필요한 다양한 모듈을 불러와 사용할 수 있습니다.

 

 

파이썬 import as
파이썬 import as

 

파이썬 from import 예제 3가지

 

random 모듈에서 randint 함수만 가져와서 사용하는 예제

from random import randint

# 1부터 10까지 랜덤한 정수 출력
print(randint(1, 10))

 

결과

random 모듈에서 randint 함수만 가져와서 사용 랜덤 숫자 출력

 

 

 

math 모듈에서 sqrt 함수만 가져와서 사용하는 예제

from math import sqrt

# 25의 제곱근 출력
print(sqrt(25))

 

결과

math 모듈에서 sqrt 함수만 가져와서 사용 25 제곱근 출력

 

 

 

datetime 모듈에서 date 클래스와 timedelta 클래스를 가져와서 사용하는 예제

from datetime import date, timedelta

# 오늘 날짜 출력
today = date.today()
print(today)

# 1일 후의 날짜 출력
one_day = timedelta(days=1)
tomorrow = today + one_day
print(tomorrow)

 

결과

파이썬 datetime 모듈에서 date 클래스와 timedelta 클래스 오늘 날짜 내일 날짜 출력

댓글