
본 블로그 글은 박동민·강영민 저자님의 으뜸파이썬 교재를 참고하여 만들어진 글임을 밝힙니다.
imort + 모듈 이름을 취함import [모듈 이름 1], [모듈 이름 2]
예시
import datetime as dt
import random as rd
import math as m
import turtle as t
예시
from datetime import datetime # 클래스만 가져오기
from math import sqrt # 메소드만 가져오기
from math import pi # 변수만 가져오기
dir() 함수
- 모듈이 가진 클래스, 속성, 메소드 반환
- 객체가 가진 속성과 메소드 반환
datetime.datetime.now()datetime.date.today()[datetime.datetime object].replace()datetime.timedelta()import datetime
# 현재 시간 출력 (년, 월, 일, 시, 분, 초, 밀리초 단위까지)
print(datetime.datetime.now()) # datetime.datetime(2024, 12, 5, 6, 57, 27, 904565)
# 오늘 날짜 출력 (년, 얼, 일)
today = datetime.date.today()
print(today) # 2024-12-05
print(today.year) # 2024
print(today.month) # 1
print(today.day) # 2
# 날짜 및 시간 변경
cur_time = datetime.datetime.now()
cur_time = cur_time.replace(month = 12, day = 25) # 크리스마스 날짜로 변경
print(cur_time)
# 남은 시간 구하기
xMas = datetime.datetime(2026, 12, 25)
gap = xMas - datetime.datetime.now()
print(f'크리스마스까지는 {gap.days}일 {gap.seconds // 3600}시간 남았습니다.')
# 100일 뒤 날짜 구하기
hundred = datetime.timedelta(days = 100)
plus100day = datetime.datetime.now() + hundred
print('100일 후 = ', plus100day)
time.time()struct_time)로 반환 time.localtime()time.strftime([출력 포맷], [struct_time 구조체])time.sleep()import time
# 에폭 이후의 시간 출력
seconds = time.time()
print('에폭 이후의 시간 = ', seconds)
# 현재 시간 출력
local_time = time.localtime()
print(time.strftime('%Y-%m-%d %H:%M:%S', local_time)) # 2024-12-05 18:51:49
# 3초동안 일시 중지
time.sleep(3)
# 경과 시간 출력
start_time = time.time() # 시작 시간 기록
print(1 * 2 * 3 * 4 * 5 * 6 * 7 * 8)
print(f'1에서 8까지의 곱을 구하고 출력하는데 걸린 시간 : {time.time() - start_time:7.4f}초')
| 함수 | 설명 |
|---|---|
math.sin(x) | x(라디안 단위)의 사인값을 반환 |
math.cos(x) | x(라디안 단위)의 코사인값을 반환 |
math.tan(x) | x(라디안 단위)의 탄젠트값을 반환 |
math.log(x, base) | x의 로그값을 계산. 기본 base는 e(자연로그), base 지정 가능 |
math.pow(x, y) | x의 y제곱값을 계산 (x^y). 연산자 ``와 동일하지만 명시적임** |
math.ceil(x) | x 이상의 가장 작은 정수 반환 (올림) |
math.floor(x) | x 이하의 가장 큰 정수 반환 (내림) |
math.trunc(x) | x의 소수점 이하를 잘라내 정수 부분만 반환 |
math.fabs(x) | x의 절댓값을 반환 |
math.copysign(x, y) | x의 절댓값에 y의 부호를 복사해 반환 |
import math
print(math.pow(3, 3)) # 27.0
print(math.fabs(-99)) # 99.0
print(math.ceil(2.1)) # 3
print(math.floor(2.1)) # 2
print(math.log(math.e)) # 1.0
print(math.log(100, 10)) # 2.0
print(math.sin(math.pi/2)) # 1.0
print(math.e) # 2.718281828459045
print(math.pi) # 3.141592653589793
print(math.radians(180)) # 3.141592653589793
| 함수 | 설명 |
|---|---|
random.random() | 0 이상 1 미만의 부동소수점 난수를 반환 |
random.randrange(start, stop, step) | 주어진 범위 내에서 임의의 정수를 반환. stop은 포함되지 않음, step으로 간격 지정 가능 |
random.randint(a, b) | a 이상 b 이하의 정수 난수를 반환. (a와 b 포함) |
random.shuffle(seq) | 주어진 시퀀스(seq)의 요소들을 무작위로 섞음 (원본 데이터 변경, 반환값 없음) |
random.choice(seq) | 주어진 시퀀스(seq)에서 임의의 요소를 하나 선택 |
random.sample(seq, k) | 주어진 시퀀스(seq)에서 중복 없이 k개의 요소를 랜덤 선택 (원본은 변경되지 않음) |
import random
print(random.random()) # 0 이상 1 미만의 실수 반환
print(random.randrange(1, 7)) # 1 이상 7 미만의 정수를 반환함
print(random.randrange(0, 10, 2)) # 0 이상 10 미만 정수 중 2의 배수를 반환함
print(random.randint(1, 10)) # 1 이상 10이하의 임의의 정수를 반환함
# 리스트 원소 섞기
num_list = [10, 20, 30, 40, 50]
random.shuffle(num_list)
print(num_list)
# 리스트 원소 중 랜덤하게 고르기
print(random.choice(num_list)) # 1개만 고르기
print(random.choice(num_list, 3)) # 3개 고르기
sys.prefixsys.versionsys.copyrightsys.pathimport sys
# 파이썬 설치 경로 출력
print(sys.prefix)
# 파이썬 버전 출력
print(sys.version)
# 파이썬 저작권 출력
print(sys.copyright)
# 모듈 검색 경로 출력
print(sys.path)