[Python] 중급 3-1. 자주 사용하는 모듈

HyunJin·2024년 5월 19일

✍ math 모듈

  • 수학 관련 모듈

1. 절대값

import math
print(f'math.fabs(-10):{math.fabs(-10)}')
print(f'math.fabs(-0.12895):{math.fabs(-0.12895)}')

# math.fabs(-10):10.0
# math.fabs(-0.12895):0.12895

2. 올림

import math
print(f'math.ceil(5.21):{math.ceil(5.21)}')
print(f'math.ceil(-5.21):{math.ceil(-5.21)}')

# math.ceil(5.21):6
# math.ceil(-5.21):-5

3. 내림

import math
print(f'math.floor(5.21):{math.floor(5.21)}')
print(f'math.floor(-5.21):{math.floor(-5.21)}')

# math.floor(5.21):5
# math.floor(-5.21):-6

4. 버림

import math
print(f'math.trunc(5.21):{math.trunc(5.21)}')
print(f'math.trunc(-5.21):{math.trunc(-5.21)}')

# math.trunc(5.21):5
# math.trunc(-5.21):-5

5. 최대공약수

import math
print(f'math.gcd(14,21):{math.gcd(14,21)}')

# math.gcd(14,21):7

6. 팩토리얼

import math
print(f'math.factorial(10):{math.factorial(10)}')

# math.factorial(10):3628800

7. 제곱근

import math
print(f'math.sqrt(4):{math.sqrt(4)}')
print(f'math.sqrt(12):{math.sqrt(12)}')

# math.sqrt(4):2.0
# math.sqrt(12):3.4641016151377544

✍ time 모듈

  • 시간 관련 모듈

1. 현재 시간

import time
lt=time.localtime()
print(f'time.localtime(): {lt}')

# time.localtime(): time.struct_time(tm_year=2024, tm_mon=5, tm_mday=19, tm_hour=17, tm_min=30, tm_sec=20, tm_wday=6, tm_yday=140, tm_isdst=0)

2. 현재의 날짜(Year)

import time
print(f'lt.tm_year: {lt.tm_year}')

# lt.tm_year: 2024

3. 현재의 날짜(Month)

import time
print(f'lt.tm_mon: {lt.tm_mon}')

# lt.tm_mon: 5

4. 현재의 날짜(Day)

import time
print(f'lt.tm_mday: {lt.tm_mday}')

# lt.tm_mday: 19

5. 현재의 날짜(요일)

import time
print(f'lt.tm_wday: {lt.tm_wday}')

# lt.tm_wday: 6

6. 현재의 시간(시)

import time
print(f'lt.tm_hour: {lt.tm_hour}')

# lt.tm_hour: 17

7. 현재의 시간(분)

import time
print(f'lt.tm_min: {lt.tm_min}')

# lt.tm_min: 30

8. 현재의 시간(초)

import time
print(f'lt.tm_sec: {lt.tm_sec}')

# lt.tm_sec: 20
profile
데이터 분석가 준비생

0개의 댓글