๐Ÿ“˜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