๋ฐ˜์‘ํ˜•

๐ŸŽˆ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ (Standard Libraries)

Python์€ ๋‹ค์–‘ํ•œ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ, ์ถ”๊ฐ€ ์„ค์น˜ ์—†์ด๋„ ๋งŽ์€ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ํŒŒ์ผ ์ž…์ถœ๋ ฅ, ๋„คํŠธ์›Œํ‚น, ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ, ๋‚ ์งœ์™€ ์‹œ๊ฐ„ ๊ด€๋ฆฌ ๋“ฑ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋ฉฐ, import ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ„ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. Python์˜ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๋ณต์žกํ•œ ์ž‘์—…์„ ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค€๋‹ค. ๋Œ€ํ‘œ์ ์ธ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

01. math: ์ˆ˜ํ•™ ๊ด€๋ จ ํ•จ์ˆ˜์™€ ์ƒ์ˆ˜๋ฅผ ์ œ๊ณต.

import math

# ์ œ๊ณฑ๊ทผ ๊ณ„์‚ฐ
print(math.sqrt(16))  # Output: 4.0

# ์›์ฃผ์œจ (pi) ์ƒ์ˆ˜ ์‚ฌ์šฉ
print(math.pi)  # Output: 3.141592653589793

# ์‚ผ๊ฐ ํ•จ์ˆ˜ ๊ณ„์‚ฐ
angle = math.radians(90)  # ๊ฐ๋„๋ฅผ ๋ผ๋””์•ˆ์œผ๋กœ ๋ณ€ํ™˜
print(math.sin(angle))  # Output: 1.0

 

02. datetime: ๋‚ ์งœ์™€ ์‹œ๊ฐ„ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๋ชจ๋“ˆ.

import datetime

# ํ˜„์žฌ ๋‚ ์งœ์™€ ์‹œ๊ฐ„ ์ถœ๋ ฅ
now = datetime.datetime.now()
print(now)  # ์˜ˆ: 2024-11-02 22:12:01.519150

# ํŠน์ • ๋‚ ์งœ ์ƒ์„ฑ
birthday = datetime.datetime(2000, 1, 1)
print(birthday)  # Output: 2000-01-01 00:00:00

# ๋‚ ์งœ ์ฐจ์ด ๊ณ„์‚ฐ
today = datetime.date.today()
new_year = datetime.date(today.year + 1, 1, 1)
days_until_new_year = (new_year - today).days
print(f"New Year๊นŒ์ง€ ๋‚จ์€ ์ผ์ˆ˜: {days_until_new_year}์ผ")

03. random: ๋‚œ์ˆ˜ ์ƒ์„ฑ ๋ฐ ๋ฌด์ž‘์œ„ ์„ ํƒ ๊ธฐ๋Šฅ ์ œ๊ณต.

import random

# 1์—์„œ 10 ์‚ฌ์ด์˜ ์ž„์˜์˜ ์ •์ˆ˜ ์ƒ์„ฑ
print(random.randint(1, 10))  # Output: 1์—์„œ 10 ์‚ฌ์ด์˜ ๋ฌด์ž‘์œ„ ์ •์ˆ˜

# 0๊ณผ 1 ์‚ฌ์ด์˜ ์ž„์˜์˜ ๋ถ€๋™ ์†Œ์ˆ˜์  ์ƒ์„ฑ
print(random.random())  # Output: 0.0 <= N < 1.0

# ๋ฆฌ์ŠคํŠธ์—์„œ ๋ฌด์ž‘์œ„๋กœ ํ•˜๋‚˜ ์„ ํƒ
choices = ["apple", "banana", "cherry"]
print(random.choice(choices))  # Output: choices ์ค‘ ํ•˜๋‚˜

# ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฌด์ž‘์œ„๋กœ ์„ž๊ธฐ
deck = [1, 2, 3, 4, 5]
random.shuffle(deck)
print(deck)  # Output: ์˜ˆ: [3, 1, 5, 2, 4]

# 1์—์„œ 100 ์‚ฌ์ด์˜ ์ž„์˜์˜ ๋ถ€๋™ ์†Œ์ˆ˜์  ์ƒ์„ฑ
print(random.uniform(1, 100))  # Output: 1.0 <= N <= 100.0
  • random.randint(a, b): a์™€ b ์‚ฌ์ด์˜ ์ •์ˆ˜ ๋‚œ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜.
  • random.random(): 0๊ณผ 1 ์‚ฌ์ด์˜ ๋ถ€๋™ ์†Œ์ˆ˜์  ๋‚œ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜.
  • random.choice(seq): ์‹œํ€€์Šค์—์„œ ๋ฌด์ž‘์œ„ ์š”์†Œ๋ฅผ ์„ ํƒ.
  • random.shuffle(seq): ์‹œํ€€์Šค์˜ ์š”์†Œ๋ฅผ ๋ฌด์ž‘์œ„๋กœ ์„ž์Œ.
  • random.uniform(a, b): a์™€ b ์‚ฌ์ด์˜ ๋ถ€๋™ ์†Œ์ˆ˜์  ๋‚œ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜.

์ด์™€ ๊ฐ™์ด random ๋ชจ๋“ˆ์„ ํ†ตํ•ด ๋‹ค์–‘ํ•œ ๋ฌด์ž‘์œ„ ์ž‘์—…์„ ์‰ฝ๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

04. glob: ํŒŒ์ผ ๊ฒฝ๋กœ ํŒจํ„ด ๋งค์นญ์„ ํ†ตํ•ด ํŠน์ • ํŒŒ์ผ ๋ชฉ๋ก์„ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ฐ ์œ ์šฉํ•œ ๋ชจ๋“ˆ.

import glob

files = glob.glob('./*.py')  # ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ ๋ชจ๋“  .py ํŒŒ์ผ ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ
print(files) # ํ„ฐ๋ฏธ๋„์— ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ์˜ ๋ชจ๋“  .py ํŒŒ์ผ์ด ๋ฆฌ์ŠคํŠธ๋กœ ์ถœ๋ ฅ๋จ

 

05. json: JSON ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ JSON ํŒŒ์ผ์„ ์ฝ๊ณ  ์“ฐ๊ธฐ ์œ„ํ•œ ๋ชจ๋“ˆ. Python์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ JSON ํŒŒ์ผ๋กœ ์ €์žฅ

import json

aespa = {'karina': 24, 'winter': 23, 'ningning': 21, 'giselle': 23}

with open("./aespa.json", "wt") as f:
    json.dump(aespa, f)

 

  1. ํŒŒ์ผ ์—ด๊ธฐ: open("./aespa.json", "wt")๋กœ aespa.json ํŒŒ์ผ์„ ์“ฐ๊ธฐ ๋ชจ๋“œ๋กœ ์—ฝ๋‹ˆ๋‹ค.
  2. ํŒŒ์ผ ๊ฐ์ฒด ์ฐธ์กฐ: f ๋˜๋Š” aespa_file ๋ณ€์ˆ˜์— ํŒŒ์ผ ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.
  3. JSON ๋ฐ์ดํ„ฐ ์ €์žฅ: json.dump(aespa, f)๋ฅผ ํ†ตํ•ด aespa ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ JSON ํ˜•์‹์œผ๋กœ ํŒŒ์ผ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

    * wt ๋ชจ๋“œ : wt๋Š” ํŒŒ์ผ์„ ์—ด ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“œ. wt๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด JSON ํ˜•์‹์˜ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ผ์— ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
    w:์“ฐ๊ธฐ ๋ชจ๋“œ(write)๋กœ, ๊ธฐ์กด ํŒŒ์ผ์ด ์žˆ๋‹ค๋ฉด ๋‚ด์šฉ์„ ๋ฎ์–ด์“ฐ๊ณ , ํŒŒ์ผ์ด ์—†์œผ๋ฉด ์ƒˆ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
    t:ํ…์ŠคํŠธ ๋ชจ๋“œ(text). ์ด ๋ชจ๋“œ๋Š” ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ๋ณดํ†ต w๋งŒ ์จ๋„ ๋™์ผํ•˜๊ฒŒ ์ž‘๋™ํ•œ๋‹ค. 

    * f ํŒŒ์ผ ๊ฐ์ฒด : f๋Š” ํŒŒ์ผ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ณ€์ˆ˜๋ช…์œผ๋กœ, open() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฐ˜ํ™˜๋œ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•œ๋‹ค. ํŒŒ์ผ ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๋ฅผ ์“ฐ๊ฑฐ๋‚˜ ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค.๊ด€๋ก€์ ์œผ๋กœ ํŒŒ์ผ ์ž‘์—…์—์„œ๋Š” f๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ์ƒํ™ฉ์— ๋งž๋Š” ๋ณ€์ˆ˜๋ช…(aespa_file ๋“ฑ)์„ ์‚ฌ์šฉํ•˜๋ฉด ๋” ๋ช…ํ™•ํ•  ์ˆ˜ ์žˆ๋‹ค.

    * : with ๊ตฌ๋ฌธ ๋•๋ถ„์— ๋ธ”๋ก์ด ๋๋‚˜๋ฉด ํŒŒ์ผ์ด ์ž๋™์œผ๋กœ ๋‹ซํžŒ๋‹ค.

06. os: ๋ชจ๋“ˆ์€ ํŒŒ์ผ ์‹œ์Šคํ…œ ์ž‘์—…, ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์ ‘๊ทผ ๋“ฑ ์šด์˜์ฒด์ œ์™€ ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณต .

import os

# ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ ํ™•์ธ
print(os.getcwd())  # ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ ์ถœ๋ ฅ

# ์ƒˆ๋กœ์šด ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ
os.mkdir("test_directory")

# ํŒŒ์ผ ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ
print(os.path.exists("test_directory"))  # Output: True

# ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์ ‘๊ทผ
home_directory = os.environ.get("HOME")
print(home_directory)

 

07. sys: Python ์ธํ„ฐํ”„๋ฆฌํ„ฐ์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š” ๊ธฐ๋Šฅ ์ œ๊ณต. ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ ๋“ฑ์˜ ๊ธฐ๋Šฅ๋„ ํฌํ•จ

import sys

# Python ๋ฒ„์ „ ํ™•์ธ
print(sys.version)

# ๋ช…๋ น์ค„ ์ธ์ž ํ™•์ธ
print(sys.argv)  # ์˜ˆ: ['script_name.py', 'arg1', 'arg2']

# ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ
try:
    sys.exit("ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.")  # ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ์™€ ํ•จ๊ป˜ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ
except SystemExit as e:
    print(e)

 

 

 

๋ฐ˜์‘ํ˜•

+ Recent posts