1. λͺ¨λ module
π μ¬λ¬ λ³μμ ν¨μλ₯Ό κ°μ§κ³ μλ μ§ν©μ²΄
π νμ΄μ¬μ κΈ°λ³Έμ μΌλ‘ λ΄μ₯λμ΄ μλ λͺ¨λμ 'νμ€ λͺ¨λ', λ€λ₯Έ μ¬λλ€μ΄ λ§λ€μ΄ 곡κ°ν λͺ¨λμ 'μΈλΆ λͺ¨λ'
1) λͺ¨λμ μ¬μ©
πΎ λ°λμ κ°μ λλ ν 리μ μμ΄μΌ ν¨
πΎ λͺ¨λμ μ μ₯λ ν¨μλ₯Ό μ¬μ©νλ λ°©λ²
A. λͺ¨λ μ 체λ₯Ό κ°μ Έμ€λ λ°©λ²
- λͺ¨λμ μ μ₯λ 'λͺ¨λ ' ν΄λμ€λ ν¨μλ₯Ό μ¬μ©νκ³ μ ν λ
ex. import λͺ¨λ
# import λͺ¨λμ΄λ¦μΌλ‘ λΆλ¬μ΄
import converter
miles = converter.kilometer_to_miles(150) # λͺ¨λ.ν¨μ() νμμΌλ‘ νΈμΆ
print(f'150km={miles}miles') # 150km=93.20565miles
pounds = converter.gram_to_pounds(1000)
print(f'1000g={pounds}pounds') # 1000g=2.20462pounds
B. λͺ¨λμ ν¬ν¨λ ν¨μ μ€μμ νΉμ ν¨μλ§ κ³¨λΌμ κ°μ Έμ€λ λ°©λ²
ex. from λͺ¨λ import ν¨μ
from λͺ¨λ import ν¨μ1, ν¨μ2
from λͺ¨λ import *
from converter import kilometer_to_miles # λͺ¨λμ κ°μ Έμ€μ§ μκ³ νΉμ ν¨μλ§ κ°μ Έμ΄
miles = kilometer_to_miles(150) # λͺ¨λλͺ
μ λͺ
μνμ§ μκ³ μ¬μ©
print(f'150km={miles}miles') # 150km=93.20565miles
print()
from converter import * # λͺ¨λμ κ°μ Έμ€μ§ μκ³ λͺ¨λμ λͺ¨λ ν¨μ κ°μ Έμ΄
miles = kilometer_to_miles(150) # λͺ¨λλͺ
μ λͺ
μνμ§ μκ³ μ¬μ©
print(f'150km={miles}miles')
pounds = gram_to_pounds(1000)
print(f'1000g={pounds}pounds') # λͺ¨λλͺ
μ λͺ
μνμ§ μκ³ μ¬μ©
2) λ³λͺ μ¬μ©
πΎ λͺ¨λμ΄λ ν¨μλ₯Ό import νλ κ²½μ°μ μλ μ΄λ¦ λμ λ³λͺ
aliasλ₯Ό μ§μ νκ³ μ¬μ©
πΎ λͺ¨λμ΄λ ν¨μμ μ΄λ¦μ΄ κΈ΄ κ²½μ°μ μ£Όλ‘ μ§§μ λ³λͺ
μ μ§μ νκ³ κΈ΄ λ³Έλ μ΄λ¦ λμ μ¬μ©
πΎ λ³λͺ
μ μ§μ ν λλ as ν€μλλ₯Ό μ¬μ©
import converter as cvt # converter λͺ¨λμ cvtλΌλ λ³λͺ
μ μ§μ
miles = cvt.kilometer_to_miles(150) # λ³λͺ
μ μ΄μ©ν΄μ ν¨μ μ¬μ©
print(f'150km={miles}miles')
pounds = cvt.gram_to_pounds(1000)
print(f'1000g={pounds}pounds')
print()
from converter import kilometer_to_miles as k_to_m # ν¨μμλ λ³λͺ
μ μ§μ κ°λ₯
miles = k_to_m(150) # ν¨μ μ΄λ¦ λμ λ³λͺ
μ μ¬μ©
print(f'150km={miles}miles')
2. math λͺ¨λ
π λ§ κ·Έλλ‘ μνκ³Ό κ΄λ ¨λ κΈ°λ₯μ κ°μ§κ³ μλ€
λ³μ λλ ν¨μ | μ€λͺ |
μμ£Όμ¨ pi | λ μ νν νμ΄ κ°μ μ¬μ©νκΈ° μν΄ |
ceil() & floor() | μ λ¬λ κ°μ μ μλ‘ μ¬λ¦Ό μ²λ¦¬νκ±°λ λ΄λ¦Ό μ²λ¦¬ |
trunc() | μ λ¬λ κ°μ μ μλ‘ μ μ¬, μμλ₯Ό μ²λ¦¬ν λλ μ°¨μ΄κ° μμ§λ§ μμλ₯Ό μ²λ¦¬ν λλ κ²°κ³Όμ μ°¨μ΄κ° μλ€ |
sqrt() | μ κ³±κ·Ό κ΅¬ν¨ |
pow() | μ κ³±μ κ΅¬ν¨ |
import math # import ν μ¬μ©
# 1) μμ£Όμ¨ pi
print(math.pi) # 3.141592653589793
# 2) ceil() ν¨μμ floor() ν¨μ
print(math.ceil(1.1)) # 2 / μ μλ‘ μ¬λ¦Ό μ²λ¦¬
print(math.floor(1.9)) # 1 / μ μλ‘ λ΄λ¦Ό μ²λ¦¬
# 3) trunc() ν¨μ
print(math.trunc(-1.9)) # -1 / μ μ¬μ΄λ―λ‘ μμμ μ΄νλ₯Ό μλΌλ²λ¦Ό
print(math.floor(-1.9)) # -2 / λ΄λ¦Όμ΄λ―λ‘ -1.9 λ³΄λ€ μμ μ μλ‘ λ΄λ¦Ό
# 4) sqrt() ν¨μ
print(math.sqrt(25)) # 5.0 / λ£¨νΈ 25λ₯Ό μλ―Έ
3. random λͺ¨λ
π λμ random number λ₯Ό μμ±νλ λͺ¨λ
π λμλ₯Ό ν΅ν΄μ κ°λ¨ν κ²μμ μ μν μ μκ³ νλ₯ μ²λ¦¬λ ν μ μλ€
λ³μ λλ ν¨μ | μ€λͺ |
randint() | μ λ¬νλ λ μΈμ μ¬μ΄μ μ μλ₯Ό μμλ‘ μμ± |
randrange() | range() ν¨μλ νΉμ λ²μμ μ μ«κ°λ€μ λͺ¨λ μμ±ν μ μμ§λ§, randrange() ν¨μλ κ·Έ νΉμ λ²μμ μν μ μμ€ νλλ§ μμλ‘ μμ± |
random() | 0μ΄μ 1λ―Έλ§ λ²μμμ μμμ μ€μλ₯Ό μμ± 0μ΄μ 1λ―Έλ§ λ²μλ₯Ό λ°±λΆμ¨λ‘ νμ°νλ©΄ 0%μ΄μ 100%λ―Έλ§μ΄κΈ° λλ¬Έμ νλ₯ μ μ²λ¦¬ν λλ μ¬μ© |
choice(list) | μ λ¬λ μνμ€ μλ£νμ μν μμ μ€μμ νλλ₯Ό μμλ‘ λ°ν |
sample(list, k=<μ«μ>) | μ λ¬λ μνμ€ μλ£νμ μν μμ μ€ μ§μ λ κ°μμ μμλ₯Ό μμλ‘ λ°ν λ°ν κ²°κ³Όλ 리μ€νΈ list μλ£νμ΄λ©° μ€λ³΅μμ΄ μμμ μμκ° μ ν |
shuffle(list) | μ λ¬λ μνμ€ μλ£νμ μν μμμ μμλ₯Ό μμλ‘ μ‘°μ νμ¬ λ€μ μ¬λ°°μΉνλ ν¨μ νΈμΆνλ©΄ μ€μ μ λ¬λ μνμ€ μλ£νμ μμκ° μ¬λ°°μΉ |
import random
# 1) randint() ν¨μ
print(random.randint(1,10)) # 1μ΄μ 10μ΄νμ μ μ
# 2) randrange() ν¨μ
print(random.randrange(10)) # 0μ΄μ 10λ―Έλ§μ μ μ
print(random.randrange(1,10)) # 1μ΄μ 10λ―Έλ§μ μ μ
print(random.randrange(1,10,2)) # 1μ΄μ 10λ―Έλ§μ νμ
# 3) random() ν¨μ
print(random.random()) # 0.1859815803780659
# 50% νλ₯ λ‘ 'μλ
νμΈμ' μΆλ ₯
if random.random() < 0.5:
print('μλ
νμΈμ')
rand = random.randint(1,2)
print(rand)
if rand == 1:
print('μλ
νμΈμ')
# 4) choice() ν¨μ
seasons = ['spring', 'summer', 'fall', 'winter']
print(random.choice(seasons)) # fall
idx = random.randint(0, len(seasons) - 1)
print(seasons[idx])
# 5) sample() ν¨μ
print(random.sample(range(1, 46), 6)) # [36, 4, 14, 35, 12, 16]
seasons = ['spring', 'summer', 'fall', 'winter']
print(random.sample(seasons, 3))
seasons = ['summer', 'summer', 'summer', 'winter']
print(random.sample(seasons, 3))
# 6) shuffle() ν¨μ
my_list = [1, 2, 3, 4, 5]
random.shuffle(my_list)
print(my_list) # [4, 2, 3, 1, 5]
4. sys λͺ¨λ
π μμ€ν κ³Ό κ΄λ ¨λ μ 보λ₯Ό κ°μ§κ³ μλ λͺ¨λ
π λͺ λ Ή 맀κ°λ³μλ₯Ό λ°μ λ λ§μ΄ μ¬μ©
import sys
# λͺ
λ Ή 맀κ°λ³μ μΆλ ₯
print(sys.argv) # ['/Users/Desktop/pycharm/pythonProject/book/1.py']
print("----")
# μ»΄ν¨ν° νκ²½κ³Ό κ΄λ ¨λ μ 보 μΆλ ₯
print("copyright:", sys.copyright)
print("----")
print("version:", sys.version)
'''
copyright: Copyright (c) 2001-2023 Python Software Foundation.
All Rights Reserved.
... μλ΅ ...
----
version: 3.10.13 (main, Sep 11 2023, 08:16:02) [Clang 14.0.6 ]
'''
# νλ‘κ·Έλ¨ κ°μ μ’
λ£
sys.exit()
5. os λͺ¨λ
π μ΄μ체μ μ κ΄λ ¨λ κΈ°λ₯μ κ°μ§ λͺ¨λ
import os
# κΈ°λ³Έ μ 보 μΆλ ₯
print("νμ¬ μ΄μ체μ :", os.name)
print("νμ¬ ν΄λ:", os.getcwd())
print("νμ¬ ν΄λ λ΄λΆμ μμ:", os.listdir())
'''
νμ¬ μ΄μ체μ : posix
νμ¬ ν΄λ: /Users/Desktop/pycharm/pythonProject/book
νμ¬ ν΄λ λ΄λΆμ μμ: ['1.py']
'''
# ν΄λ λ§λ€κ³ μ κ±°(ν΄λκ° λΉμ΄μμ λλ§ κ°λ₯)
os.mkdir("hello")
os.rmdir("hello")
# νμΌ μμ±νκ³ νμΌ μ΄λ¦ λ³κ²½
with open("original.txt", "w") as file:
file.write("hello")
os.rename("original.txt", "new.txt")
# νμΌ μ κ±°
os.remove("new.txt")
# μμ€ν
λͺ
λ Ήμ΄ μ€ν
os.system("dir")
6. datetime λͺ¨λ
π λ μ§μ μκ° λ°μ΄ν°λ₯Ό μ²λ¦¬ν λ μ¬μ©
λ³μ λλ ν¨μ | μ€λͺ |
now() | μμ€ν μ νμ¬ λ μ§μ μκ°μ λ°ν |
date() | νΉμ λ μ§λ₯Ό λ°ν |
time() | νΉμ μκ°μ λ°ν |
λ μ§/μκ° κ΄λ ¨ νλκ° | νΉμ λ μ§μ μνλ λ°μ΄ν°λ§ μΆμΆνκ³ μ ν λ μ΄μ© |
timedelta() | λ μ§ / μκ° λ°μ΄ν°μ μ°μ°μ μνμ¬ μ¬μ© |
total_seconds() | μ΄λ€ κΈ°κ°μ ν¬ν¨λ μ΄ μκ°μ μ΄ seconds λ‘ λ°ν |
import datetime
# 1) now() λ©μλ
present = datetime.datetime.now()
print(present)
# 2) date() ν¨μ
birthday = datetime.date(2014, 8, 25)
print(birthday) # 2014-08-25
# 3) time() ν¨μ
wake = datetime.time(10, 48, 0)
print(wake) # 10:48:00
# 4) λ μ§ / μκ° κ΄λ ¨ νλκ°
today = datetime.datetime.now()
print(today.year) # λ
λ
print(today.month) # μ
print(today.day) # μΌ
print(today.hour) # μ
print(today.minute) # λΆ
print(today.second) # μ΄
# 5) timedelta() ν¨μ
# 1μ£Ό : timedelta(weeks=1)
# 1μΌ : timedelta(days=1)
# 1μκ° : timedelta(hours=1)
# 1λΆ : timedelta(minutes=1)
# 1μ΄ : timedelta(seconds=1)
today = datetime.datetime.now()
yesterday = today - datetime.timedelta(days=1) # μ΄μ ꡬν¨
tomorrow = today + datetime.timedelta(days=1) # λ΄μΌ ꡬν¨
print(yesterday)
print(tomorrow)
# 6) total_seconds() λ©μλ
date1 = datetime.date(2020, 8, 25)
date2 = datetime.date(2020, 8, 26)
print(date2 - date1) # 1 day, 0:00:00
print((date2 - date1).total_seconds()) # 86400.0 = 60μ΄ * 60λΆ * 24μκ°
7. time λͺ¨λ
π μκ°κ³Ό κ΄λ ¨λ κΈ°λ₯ μ²λ¦¬
λ³μ λλ ν¨μ | μ€λͺ |
time() | 1970λ
1μ 1μΌ 0μ 0λΆ 0μ΄ λΆν° νμ¬κΉμ§ κ²½κ³Όλ μκ° timestampμ λ°ν μμμ μ΄νλ λ§μ΄ν¬λ‘ μ΄λ₯Ό μλ―Έ |
ctime() | μΈμλ‘ μ λ¬λ μκ° timestampμ νμμ κ°μΆ° λ°ν |
strftime() | μΈμλ‘ μ λ¬λ λ μ§μ μ§μμλ₯Ό μ΄μ©νμ¬ νμμ κ°μΆ λ μ§ λ°μ΄ν°κ° λ¬Έμμ΄λ‘ λ°ν |
sleep() | μΈμλ‘ μ λ¬λ μ΄ second λ§νΌ μμ€ν μ μΌμ μ μ§ |
import time
# 1) time() ν¨μ
print(time.time()) # 1645240996.2733545
# 2) ctime() ν¨μ
print(time.ctime(time.time())) # Sat Feb 19 12:23:16 2022
# 3) strftime() ν¨μ
# λ
: %y : 2μ리 λ μ§λ‘ νμ
# λ
: %Y : 4μ리 λ μ§λ‘ νμ
# μ : %m : 2μ리 μ«μλ‘ νμ (01 ~ 12)
# μ : %b : 3μ리 μλ¬ΈμΌλ‘ νμ (Jan ~ Dec)
# μ : %B : μ 체 μλ¬ΈμΌλ‘ νμ (January ~ December)
# μΌ : %d : 2μ리 μ«μλ‘ νμ (01 ~ 31)
# μμΌ : %a : 3μ리 μλ¬ΈμΌλ‘ νμ (Sun ~ Sat)
# μμΌ : %A : μ 체 μλ¬ΈμΌλ‘ νμ (Sunday ~ Saturdy)
# μ : %l : 12μκ°μ λ‘ νμ (01 ~ 12)
# μ : %H : 24μκ°μ λ‘ νμ (00 ~ 23)
# λΆ : %M : 2μκ°μ λ‘ νμ (00 ~ 59)
# μ΄ : %S : 2μκ°μ λ‘ νμ (00 ~ 59)
# μ€μ / μ€ν : %p : AM λλ PM
print(time.strftime('%Y-%m-%d %H:%M:%S'))
# 4) sleep() ν¨μ
time.sleep(1)
8. urllib λͺ¨λ
π URLμ λ€λ£¨λ λΌμ΄λΈλ¬λ¦¬
π URL : Uniform Rssource Locator, λ€νΈμν¬μ μμμ΄ μ΄λμ μμΉνλμ§ νμΈν λ μ¬μ©νλ κ²
βΆοΈ μΉ λΈλΌμ°μ μ μ£Όμμ°½μ μ λ ₯νλ μ£Όμ
from urllib import request
# urlopen() ν¨μλ‘ κ΅¬κΈ λ©μΈνμ΄μ§λ₯Ό μ½μ΅λλ€
target = request.urlopen("https://google.com")
output = target.read()
# μΆλ ₯ν©λλ€
print(output)
'''
b'<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage"
lang="ko"><head><meta content="text/html; charset=UTF-8"
... μλ΅ ...
'''
[ λ΄μ© μ°Έκ³ : IT νμ κ°μ λ° μ± 'νΌμ 곡λΆνλ νμ΄μ¬' ]