| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- Operating System
- Data Science
- Humble
- cs231n
- BFS
- System Call
- Process
- DFS
- CNN
- Machine Learning
- Baekjoon
- Seoul National University
- do it! 알고리즘 코딩테스트: c++편
- ROS2
- Python
- C++
- Linux
- Optimization
- Gentoo2
- SQLD
- RNN
- CPP
- Robocup@Home 2026
- deep learning
- file system
- computer vision
- paper review
- 밑바닥부터 시작하는 딥러닝2
- Multimedia
- On-memory file system
Archives
- Today
- Total
newhaneul
[Advanced Python Programming] Lecture 8. Module and Package 본문
4. University Study/Advanced Python Programming
[Advanced Python Programming] Lecture 8. Module and Package
뉴하늘 2026. 4. 16. 23:28728x90
포스팅은 인하대학교 허혜선 교수님의 [202601-EEC3408-001] 고급파이썬프로그래밍을 수강하고 공부한 내용을 정리하기 위한 포스팅입니다.
1. datetime (날짜와 시간 모듈)
- 특정 시점 (datetime.datetime): 2026년 4월 16일, 오늘 오후 2시 등 달력이나 시계에 콕 찍을 수 있는 특정한 점을 의미한다.
- 기간 (datetime.timedelta): 100일 동안, 3시간 동안 등 시간의 양(길이, 간격)을 의미한다. (days, seconds ... 보유)
- datetime.datetime.now(): 현재의 년, 월, 일, 시, 분, 초, 밀리초 단위의 시간까지 나타낸다.
import datetime
print(datetime.datetime.now())
2026-04-16 21:54:33.020608
- datetime.date.today(): 현재 날짜를 today에 반환
import datetime
today = datetime.date.today()
print(today)
print(today.year)
print(today.month)
print(today.day)
2026-04-16
2026
4
16
- replace(year=?, month=?, day=?): datetime에서 날짜나 시간 값을 변경하고 싶을 때 사용
import datetime
start_time = datetime.datetime.now()
print(start_time)
start_time = start_time.replace(month=12, day=25)
print(start_time)
2026-04-16 22:00:01.285783
2026-12-25 22:00:01.285783
- datetime.timedelta(...): 현재의 년, 월, 일, 시, 분, 초, 밀리초 단위의 시간까지 나타낸다.
- weeks, days, hours ... s로 끝남!
import datetime
today = datetime.datetime.now()
hundred = datetime.timedelta(days=100)
hundred_days_from_today = today + hundred
print(hundred_days_from_today)
2026-07-25 22:03:42.786090

- 예제

import datetime
now = datetime.datetime.now()
print(now)
print(f"오늘의 날짜: {now.year}년 {now.month}월 {now.day}일")
ampm = "오전"
hour = now.hour
if hour >= 12:
ampm = "오후"
if hour > 12:
hour -= 12
elif hour == 0:
hour = 12
print(f"현재시간: {ampm} {hour}시 {now.minute}분 {now.second}초")
오늘의 날짜: 2026년 4월 16일
현재시간: 오후 10시 13분 24초

import datetime
now = datetime.datetime.now()
print(f"오늘은 {now.year}년 {now.month}월 {now.day}일입니다.")
event = datetime.datetime(year=2027, month=12, day=25)
gap = event - now
print(f"2027년 크리스마스 까지는 {gap.days}일 {gap.seconds // 3600}시간 남았습니다.")
오늘은 2026년 4월 16일입니다.
2027년 크리스마스 까지는 617일 1시간 남았습니다.

import datetime
now = datetime.datetime.now()
plus_1000 = now + datetime.timedelta(days=1000)
print(plus_1000)
2029-01-10 22:31:26.004040
import datetime
y, m, d = map(int, input("처음으로 사귄 연도와 월, 일을 입력하시오: ").split())
event = datetime.datetime(year=y, month=m, day=d) + datetime.timedelta(days=99)
print(f"100일 기념일은: {event.year}년 {event.month}월 {event.day}일입니다.")
처음으로 사귄 연도와 월, 일을 입력하시오: 2019 3 30
100일 기념일은: 2019년 7월 7일입니다.
2. time (시간 모듈)
- time.time(): 시간 기록
import time
start_time = time.time()
for i in range(1000000):
i += 1
end_time = time.time()
print(f"{(end_time - start_time):.4f}")
0.0557
3. math (수학 모듈)
- math.pow(a, b): a의 b 제곱
- math.fabs(a): a의 실수 절대값
- math.abs(a): a의 정수 절대값
- math.ceil(a): a의 올림값
- math.floor(a): a의 내림값
- math.log(a), math.log(a, b): a가 진수인 지수 로그, a가 진수이고 b가 밑인 로그

import math
for i in range(2, 10):
print(f"4 ** {i} = {math.pow(4, i)}")
4 ** 2 = 16.0
4 ** 3 = 64.0
4 ** 4 = 256.0
4 ** 5 = 1024.0
4 ** 6 = 4096.0
4 ** 7 = 16384.0
4 ** 8 = 65536.0
4 ** 9 = 262144.0
import math
for i in range(0, 181, 10):
print(f"{i} degree = {math.radians(i):.4} radian")
0 degree = 0.0 radian
10 degree = 0.1745 radian
20 degree = 0.3491 radian
30 degree = 0.5236 radian
40 degree = 0.6981 radian
50 degree = 0.8727 radian
60 degree = 1.047 radian
70 degree = 1.222 radian
80 degree = 1.396 radian
90 degree = 1.571 radian
100 degree = 1.745 radian
110 degree = 1.92 radian
120 degree = 2.094 radian
130 degree = 2.269 radian
140 degree = 2.443 radian
150 degree = 2.618 radian
160 degree = 2.793 radian
170 degree = 2.967 radian
180 degree = 3.142 radian
import math
for i in range(0, 181, 10):
print(f"sin({i}) = {math.sin(math.radians(i)):.2f}")
sin(0) = 0.00
sin(10) = 0.17
sin(20) = 0.34
sin(30) = 0.50
sin(40) = 0.64
sin(50) = 0.77
sin(60) = 0.87
sin(70) = 0.94
sin(80) = 0.98
sin(90) = 1.00
sin(100) = 0.98
sin(110) = 0.94
sin(120) = 0.87
sin(130) = 0.77
sin(140) = 0.64
sin(150) = 0.50
sin(160) = 0.34
sin(170) = 0.17
sin(180) = 0.00
4. random (난수 모듈)
- random.random(): 0 이상 1 미만 사이의 임의의 실수를 반환한다.
import random
print(random.random())
0.017483372668855246
- random.randrange(n, m, [step]): n 이상 m 미만의 지정된 범위 내 임의의 정수를 반환하며, 간격을 지정할 수도 있다.
import random
print(random.randrange(1, 10))
2
- random.randint(a, b): a 이상 b 이하 사이의 임의의 정수를 반환한다.
import random
print(random.randint(1, 10))
9
- random.shuffle(sequence): 인자로 주어진 시퀀스의 요소를 매번 다른 순서로 랜덤하게 섞는다.
import random
numlist = [1, 2, 3, 4, 5]
random.shuffle(numlist)
print(numlist)
- random.choice(sequence): 인자로 들어온 시퀀스로부터 임의의 요소 하나를 선택하여 반환한다.
import random
numlist = [1, 2, 3, 4, 5]
print(random.choice(numlist))
1
- random.sample(sequence, a): 시퀀스의 원소들 중에서 임의로 a 개를 고른다.
- 예제

import random
array = []
for i in range(3):
array.append(random.randrange(0, 101, 5))
print(f"0에서 100 이하의 정수 중에서 5의 배수 {array}")
[55, 5, 70]
import random
array = list(range(1, 11))
result = random.sample(array, 3)
print(f"1에서 10 사이의 임의의 정수: {result}")
1에서 10 사이의 임의의 정수: [1, 9, 4]
728x90
'4. University Study > Advanced Python Programming' 카테고리의 다른 글
| [Advanced Python Programming] Lecture 10. Exception Handling (1) | 2026.04.17 |
|---|---|
| [Advanced Python Programming] Lecture 9. File Input/Output (0) | 2026.04.17 |
| [Advanced Python Programming] Lecture 7. Special Methods (1) | 2026.04.12 |
| [Advanced Python Programming] Lecture 6. Object (1) | 2026.04.12 |
| [Advanced Python Programming] Lecture 5. Function (2) | 2026.04.12 |