본문 바로가기

python

module - 표준모듈

 

Module

소스 코드의 재사용을 가능하게 할 수 있으며, 소스코드를 하나의 이름공간으로 구분하고 관리하게 된다
하나의 파일은 하나의 모듈이 된다.
표준모듈, 사용자 작성 모듈, 제3자(Third party) 모듈
모듈의 멤버는 전역변수, 실행문, 함수, 클래스, 모듈이 있다

 

 

표준모듈 (내장된 모듈)

 

math 모듈

import math
print(math.pi)
print(math.sin(math.radians(30)))

3.141592653589793
0.49999999999999994

 

 

calendar 모듈

setfirstweekday() 로 일요일부터 시작하도록 설정했다

import calendar
calendar.setfirstweekday(6)  # 0:월...일(6)
calendar.prmonth(2022, 10)

    October 2022
Su Mo Tu We Th Fr Sa
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

 

 

os 모듈

import os
print(os.getcwd())
#C:\work
print(os.listdir('/'))

C:\work\psou\pro1\pack2
['$MfeDeepRem', '$Recycle.Bin', '$WinREAgent', 'acorn202206', 'apache-maven-3.8.6', 'bootTel.dat', 'Documents and Settings', 'DumpStack.log.tmp', 'hiberfil.sys', 'Intel', 'MSOCache', 'oraclexe', 'pagefile.sys', 'PerfLogs', 'Program Files', 'Program Files (x86)', 'ProgramData', 'recovery', 'rhdsetup.log', 'Setup.log', 'swapfile.sys', 'System Volume Information', 'UkLog.dat', 'Users', 'User_manual', 'Windows', 'work']

 

 

random 모듈

print()
import random
# 실수
print(random.random())
# 정수
print(random.randint(1,20))

0.04402237631026218
8

 

 

from import 방식

# from 모듈명 import 멤버 - 추후에 해당 멤버는 모듈명 생략이 가능하다
from random import random
print(random())

# 나열해서 작성하는 것도 가능하
from random import randint, randrange
print(random.randint(1,10))

0.4624106462488524
4

'python' 카테고리의 다른 글

module - 라이브러리  (0) 2022.10.06
module - 사용자 작성 모듈  (0) 2022.10.06
재귀함수(Recursive function), factorial  (0) 2022.10.06
함수 장식자 decorator  (0) 2022.10.06
일급객체와 일급함수, 람다함수  (0) 2022.10.06