








모듈파일
def reverseStr(str):
reversedString=''
for c in str:
reversedString= c + reversedString
return reversedString
실행파일
import reverseStr
userInputStr= input('문자열 입력: ')
reversedString= reverseStr.reverseStr(userInputStr)
print(f'reversedString:{reversedString}')
결과
문자열 입력: 파이썬 공부를 하고 있습니다.
reversedString:.다니습있 고하 를부공 썬이파

from 키워드를 이용해서 모듈의 특정 기능만 사용


import 뒤에 *을 사용하면 해당 모듈의 모든 함수 호츌

모듈파일
scores= []
def addScore(s):
scores.append(s) #append는 괄호( ) 안에 값을 입력하면 새로운 요소를 array 맨 끝에 객체로 추가
def getScore():
return scores
def getTotalScore():
total = 0
for s in scores:
total += s
return total
def getAvgScore():
avg= (getTotalScore() / len(scores))
return avg
실행파일
import scores as sc
korScore = int(input('국어점수: '))
engScore = int(input('영어점수: '))
matScore = int(input('수학점수: '))
sc.addScore(korScore)
sc.addScore(engScore)
sc.addScore(matScore)
print(sc.getScore())
print(sc.getTotalScore())
print(sc.getAvgScore())
결과
국어점수: 75
영어점수: 90
수학점수: 86
[75, 90, 86]
251
83.66666666666667



모듈파일
def cmToMm (n):
return round (n*10, 3)
def cmToInch (n):
return round (n*0.393, 3)
def cmToM (n):
return round (n*0.01, 3)
def cmToFt (n):
return round (n*0.032, 3)
실행파일
import unitConversion as uc
if __name__ == '__main__':
inputNum= int(input('길이(cm)입력: '))
returnValue= uc.cmToMm(inputNum)
print(f'returnValue: {returnValue}mm')
returnValue=uc.cmToInch(inputNum)
print(f'returnValue: {returnValue}inch')
returnValue=uc.cmToM(inputNum)
print(f'returnValue: {returnValue}m')
returnValue=uc.cmToFt(inputNum)
print(f'returnValue: {returnValue}ft')
결과값1
길이(cm)입력: 10
returnValue: 100mm
returnValue: 3.93inch
returnValue: 0.1m
returnValue: 0.32ft
결과값2
길이(cm)입력: 35
returnValue: 350mm
returnValue: 13.755inch
returnValue: 0.35m
returnValue: 1.12ft