자료 참조는 제로베이스
#random 모듈 - 1~100까지 정수중 난수 10개발생
rNums = random.sample(range(1, 101), 10) # 값이 여러개인 'List' type 으로 반환
print(f'random number : {rNums}')
#출력
random number : [95, 68, 55, 67, 93, 13, 6, 30, 71, 50]
import random
def getLottoNum():
result6 = random.sample(range(1, 46), 6)
return result6
#로또머신모듈 lottoMachine 임포트해서 쓰기
import lottoMachine
num = lottoMachine.getLottoNum()
print(f'lotto numbers : {num}')
#출력
lotto numbers : [29, 33, 2, 44, 36, 10]
import : 모듈을 가져올 떄 쓰는 키워드
as : 모듈 이름을 간략하게 단축 시킬 수 있다.
from ~ import : 그 모듈안에 있는 특정한 기능만 쓸 수 있다.
#calculator.py
def add(a, b):
print(f'a + b = {a + b}')
def sub(a, b):
print(f'a - b = {a - b}')
def mul(a, b):
print(f'a * b = {a * b}')
def div(a, b):
print(f'a / b = {a / b}')
→'calculator' 모듈 생성.
import calculator as cal # import / as 사용
cal.add(10, 20)
cal.sub(10, 20)
cal.mul(10, 20)
cal.div(10, 20)
→calculator 모듈을 가져오고, 'cal'로 모듈 이름을 간략하게 쓸 수 있다.
from calculator import add
add(10, 20)
→calculator 모듈 속 add 기능만 가져온다.
모듈을 그룹으로 묶는 단위이다.
site-packages에 있는 모듈은 어디서나 사용할 수 있다.
site-packages > calculator > cal
'cal' 모듈은 site-packages에 있으니 어디서든 쓸 수 있다. (다수의 프로젝트 간에도 사용 가능)
import math
#math 절대값
print(f'\'-10의 절대값\' : {math.fabs(-10)}')
#math 올림
print(f'\'5.21\'의 올림 : {math.ceil(5.21)}')
#math 내림
print(f'\'5.21\'의 올림 : {math.floor(5.21)}')
#math 버림
print(f'\'5.21\'의 올림 : {math.trunc(5.21)}')
#math 최대공약수
print(f'\'14, 21\'의 최대공약수 : {math.gcd(14, 21)}')
#math 팩토리얼
print(f'\'5\'의 팩토리얼 : {math.factorial(5)}')
#math 제곱근
print(f'\'4\'의 제곱근 : {math.sqrt(4)}')
import time
lt = time.localtime()
print(f'localtime : {lt}')
print(f'localtime_mon : {lt.tm_mon}')
print(f'localtime_year : {lt.tm_year}')
print(f'localtime_mday : {lt.tm_mday}')
print(f'localtime_hour : {lt.tm_hour}')
3.141592 : 3.1
3.141592 : 3.14
3.141592 : 3.14159
'-10의 절대값' : 10.0
'5.21'의 올림 : 6
'5.21'의 올림 : 5
'5.21'의 올림 : 5
'14, 21'의 최대공약수 : 7
'5'의 팩토리얼 : 120
'4'의 제곱근 : 2.0
localtime : time.struct_time(tm_year=2023, tm_mon=8, tm_mday=8, tm_hour=12, tm_min=3, tm_sec=44, tm_wday=1, tm_yday=220, tm_isdst=0)
localtime_mon : 8
localtime_year : 2023
localtime_mday : 8
localtime_hour : 12