모듈

yejichoi·2023년 4월 18일
0

Python

목록 보기
4/4
post-thumbnail

math 모듈

import math

math.cos(1)
# 0.54030230586...
math.tan(1)
# 1.55740772..
math.floor(2.5)
#2
math.ceil(2.5)
# 3
변수 또는 함수설명
sqrt(x)제곱근
pow(x,y)x의 y제곱
radians(x)각도를 라디안으로 변환
degrees(x)라디안을 각도로 변환
factorial(x)팩토리얼
gcd(x,y)최대공약수
sin(x)사인값
cos(x)코사인값
tan(x)탄젠트값
log(x[,base])로그값
ceil(x)올림(실수를 정수로 바꿀 때)
floor(x)내림(실수를 정수로 바꿀 때)
#최소공배수
import math

def lcm(a, b):
    return abs(a * b) // math.gcd(a, b)

# 예시 사용법
result = lcm(12, 18)
print(result)  # 출력: 36

from 구문

from 모듈 이름 import 가져오고 싶은 변수 또는 함수

⚡️ 모두 가져오기

from math import *

⚡️ as 구문 (별명)

import 모듈 as 사용하고 싶은 식별자

from math import sin, cos, tan, floor, ceil 
math.cos(1)
# 0.54030230586...
math.tan(1)
# 1.55740772..
math.floor(2.5)
#2
math.ceil(2.5)
# 3

random 모듈

import random
랜덤한 값을 생성할 때 사용하는 모듈

import random

# random() : 0.0 <= x < 1.0 사이의 float를 리턴
random.random()

#uniform(min, max) : 지정한 범위 사이의 float를 리턴 
random.uniform(10, 20)

#randrange(): 지정한 범위의 int를 리턴
# - randrange(max) : 0부터 max 사이의 값을 리턴 
# - randrange(min, max) : min부터 max 사이의 값을 리턴
random.randrange(10)

#choice(list) : 리스트 내부에 있는 요소를 랜덤하게 선택
random.choice([1,2,3,4,5])

#shuffle(list) : 리스트의 요소들을 랜덤하게 섞음
random.shuffle([1,2,3,4,5])

#sample(list, k=<숫자>) : 리스트의 요소 중에 k개를 뽑음
random.sample([1,2,3,4,5], k=2)

sys 모듈

시스템과 관련된 정보를 가지고 있는 모듈

#컴퓨터 환경과 관련된 정보를 출력
sys.getwindowsversion()
sys.copyright
sys.version

#프로그램 강제 종료
sys.exit()

os 모듈

운영체제와 관련된 정보를 가지고 있는 모듈

# 기본 정보를 몇 개 출력
os.name
os.getcwd()
os.listdir()

# 폴더를 만들고 제거(폴더가 비어있을 때만 제거 가능)
os.mkdir("hello")
os.rmdir("hello")

# 파일 제거
os.remove("new.txt")

# 시스템 명령어 실행
os.system("dir")

datetime 모듈

date, time 과 관련된 날짜 형식 모듈
import datetime

time 모듈

시간과 관련된 기능을 다룰 때

import time

print("지금부터 5초 동안 정지합니다.")
time.sleep(5)
print("프로그램을 종료합니다.")

urllib 모듈

인터넷 주소를 활용할 때 사용하는 라이브러리

from urllib import request

# 구글 메인 페이지를 읽음
target = request.urlopen("https://google.com")
output = target.read()

#출력 
print(output)

0개의 댓글