7단원 쪽지시험: 모듈 & 패키지¶
이름: ____________
출력 예측: 소스 코드를 보고 실행 결과를 예측하세요.
문제 1. math 모듈 (출력 예측)¶
소스 코드
import math
print(math.floor(-2.3))
print(math.ceil(-2.3))
print(math.sqrt(144))
print(math.factorial(0))
print(round(math.pi, 2))
실행 결과
①
②
③
④
⑤
문제 2. __name__ (출력 예측)¶
utils.py
def double(x):
return x * 2
print(f"utils loaded: __name__ = {__name__}")
if __name__ == "__main__":
print("직접 실행:", double(5))
app.py
from utils import double
print(double(7))
app.py를 실행했을 때 출력:
①
②
문제 3. json 변환 (출력 예측)¶
소스 코드
import json
original = {"x": 1, "y": [2, 3], "z": True}
text = json.dumps(original)
back = json.loads(text)
print(type(text))
print(back["x"] + back["y"][0])
print(back["z"] is True)
print(len(back["y"]))
실행 결과
①
②
③
④
문제 4. random + sorted (출력 예측)¶
소스 코드
import random
random.seed(0)
items = ["a", "b", "c", "d", "e"]
chosen = random.sample(items, 3)
print(chosen)
print(sorted(chosen))
print(len(random.sample(range(10), 5)))
print(type(random.random()))
실행 결과
①
②
③
④
Q: random.sample과 random.choices의 차이점은?
Answer: ______
문제 5. os.path (출력 예측)¶
소스 코드
import os
p = os.path.join("home", "user", "docs", "file.txt")
print(p)
print(os.path.basename(p))
print(os.path.dirname(p))
print(os.path.splitext(p))
print(os.path.exists(p))
실행 결과 (Linux 기준, 파일은 실제로 없음)
①
②
③
④
⑤