Repository navigation
Expand file tree
/
Copy pathModulePackage.py
More file actions
103 lines (79 loc) · 2.49 KB
/
Copy pathModulePackage.py
File metadata and controls
103 lines (79 loc) · 2.49 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# =============================================
# 모듈과 패키지 예제
# =============================================
# ==================== random 모듈 ====================
print("=== random 모듈 ===")
import random
# seed 설정 (결과 고정)
random.seed(42)
# random() - 0.0 ~ 1.0 난수
print(random.random())
# randint(a, b) - a ~ b 정수 난수
print(random.randint(1, 10))
# shuffle - 리스트 섞기
data = [1, 2, 3, 4, 5]
random.shuffle(data)
print(data)
# choice - 하나 선택
print(random.choice(['a', 'b', 'c']))
# 로또 번호 생성
lotto = list(range(1, 46))
random.shuffle(lotto)
print(sorted(lotto[:6]))
# ==================== math 모듈 ====================
print("\n=== math 모듈 ===")
import math
print(f"PI: {math.pi}")
print(f"e: {math.e}")
print(f"ceil(3.14): {math.ceil(3.14)}") # 4
print(f"floor(3.14): {math.floor(3.14)}") # 3
print(f"sqrt(16): {math.sqrt(16)}") # 4.0
print(f"radians(180): {math.radians(180)}") # π
# ==================== sys 모듈 ====================
print("\n=== sys 모듈 ===")
import sys
print(f"Python 버전: {sys.version}")
print(f"최대 정수값: {sys.maxsize}")
# print(f"모듈 검색 경로: {sys.path}")
# ==================== os 모듈 ====================
print("\n=== os 모듈 ===")
import os
print(f"현재 디렉토리: {os.getcwd()}")
# print(f"디렉토리 내용: {os.listdir('.')}")
# 경로 결합
path = os.path.join("folder", "subfolder", "file.txt")
print(f"경로 결합: {path}")
# 경로 존재 확인
print(f"현재 디렉토리 존재: {os.path.exists('.')}")
# ==================== datetime 모듈 ====================
print("\n=== datetime 모듈 ===")
from datetime import datetime, timedelta
now = datetime.now()
print(f"현재: {now}")
print(f"연도: {now.year}")
print(f"월: {now.month}")
print(f"일: {now.day}")
# 날짜 연산
tomorrow = now + timedelta(days=1)
print(f"내일: {tomorrow}")
# 포맷팅
print(now.strftime("%Y-%m-%d %H:%M:%S"))
# ==================== collections 모듈 ====================
print("\n=== collections 모듈 ===")
from collections import Counter, defaultdict
# Counter
text = "abracadabra"
counter = Counter(text)
print(f"Counter: {counter}")
print(f"가장 많은 2개: {counter.most_common(2)}")
# defaultdict
dd = defaultdict(int)
dd['a'] += 1
dd['b'] += 2
print(f"defaultdict: {dict(dd)}")
# ==================== 별칭 사용 ====================
print("\n=== 별칭 사용 ===")
import math as m
print(f"m.pi: {m.pi}")
from math import sqrt as s
print(f"s(25): {s(25)}")