C-log

Welcome to Python : 표준 라이브러리 본문

📘Python

Welcome to Python : 표준 라이브러리

4:Bee 2023. 7. 27. 18:13
728x90

Title Code Console
datetime.date

import
datetime
day1 = datetime.date(2021,12,14)
day2 = datetime.date(2023,4,5)
print(day1, day2)
2021-12-14 2023-04-05
time.time
 
import
time
print(time.time())
 
1690873216.8322353
time.localtime
 
import
time
print(time.localtime(time.time()))
 
time.struct_time(tm_year=2023, tm_mon=8, tm_mday=1, tm_hour=16, tm_min=0, tm_sec=43, tm_wday=1, tm_yday=213, tm_isdst=0)
time.asctime
 
import
time
print(time.asctime(time.localtime(time.time())))
 
Tue Aug  1 16:01:25 202
time.ctime

import
time
print(time.ctime())
Tue Aug  1 16:02:02 2023
time.strftime

import
time
print(time.strftime("%x", time.localtime(time.time())))
print(time.strftime("%c", time.localtime(time.time())))

08/01/23
Tue Aug  1 16:03:21 2023
time.sleep
 
import
time
for i in range(10):
    print(i)
    time.sleep(1)
 
0
1
2
3
...
math.gcd
 
#gcd : 최대 공약수
import math
print(math.gcd(60, 100, 80))
 
20
math.lcm

#lcm : 최소 공배수
import math
print(math.lcm(15, 25)) 
 
75
random

import random
print(random.random())
0.13769631608517663

#randint()
import random
def random_pop(data) :
    number = random.randint(0, len(data) - 1)
    return data.pop(number)

if __name__ == "__main__":
    data = [1,2,3,4,5]
    while data :
        print(random_pop(data))
 
4
5
2
3
1

#rchoice()
import random
def random_pop(data) :
    number = random.choice(data)
    data.remove(number)
    return number

if __name__ == "__main__":
    data = [1,2,3,4,5]
    while data :
        print(random_pop(data))
 
1
2
3
5
4
 
#sample
import random
data = [1,2,3,4,5]
data = random.sample(data, len(data))
print(data)
 
[5, 3, 4, 2, 1]
itertools.zip_longest

students = ['한민서', '황지민', '이영철', '이광수', '김승민']
snacks = ['사탕', '초콜릿', '젤리']

result = zip(students, snacks)
print(list(result))

[('한민서', '사탕'), ('황지민', '초콜릿'), ('이영철', '젤리')]

import
itertools

students = ['한민서', '황지민', '이영철', '이광수', '김승민']
snacks = ['사탕', '초콜릿', '젤리']

result = itertools.zip_longest(students, snacks, fillvalue='새우깡')
print(list(result))
 
[('한민서', '사탕'), ('황지민', '초콜릿'), ('이영철', '젤리'), ('이광수', '새우깡'), ('김승민', '새우깡')]
itertools.permutation

import
itertools

data = list(itertools.permutations(['1','2','3'], 2))
print(data)

[('1', '2'), ('1', '3'), ('2', '1'), 
('2', '3'), ('3', '1'), ('3', '2')]
itertools.combination
 
import
itertools

it = itertools.combinations(range(1,46),6)
print(it)
 
<itertools.combinations object at 0x00000280441BA9D0>
functools.reduce

import
functools

data = [1,2,3,4,5]
result = functools.reduce(lambda x, y: x + y, data)
print(result)

15
operator.itemgetter

#나이 순서
from operator import itemgetter

students = [
    ("jane", 22, 'A'),
    ("dave", 32, 'B'),
    ("sally", 17, 'B'),
]

result = sorted(students, key=itemgetter(1))
print(result)

[('sally', 17, 'B'), ('jane', 22, 'A'), ('dave', 32, 'B')]

#age 순서
from operator import itemgetter

students = [
    {"name" : "jane", "age" : 22, "grade" : 'A'},
    {"name" : "dave", "age" : 32, "grade" : 'B'},
    {"name" : "sally", "age" : 17, "grade" : 'B'},
]

result = sorted(students, key=itemgetter('grade'))
print(result)

[{'name': 'jane', 'age': 22, 'grade': 'A'}, {'name': 'dave', 'age': 32, 'grade': 'B'}, {'name': 'sally', 'age': 17, 'grade': 'B'}]
shutil

#복사
import shutil
shutil.copy("c:/doit/a.txt", "c:/doit/a.txt.bak")

 
 
 
#이동 / 삭제
import shutil
shutil.move("c:/doit/a.txt", "c:/temp/a.txt")
 
 
glob
 
 
pickle    
os.environ    
os.chdir    
os.getcwd    
os.system    
os.popen    
zipfile    
threading    
tempfile    
traceback    
json    
urllib    
webbrowser    
728x90

'📘Python' 카테고리의 다른 글

Welcome to Python : 내장 함수  (0) 2023.07.25
Welcome to Python : 예외처리  (0) 2023.07.25
Welcome to Python : 패키지  (0) 2023.07.24
Welcome to Python : 모듈  (0) 2023.07.24
Welcome to Python : 클래스  (0) 2023.07.21
Comments