๐Python
Welcome to Python : ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ
4:Bee
2023. 7. 27. 18:13
728x90
- ํด๋์ค : https://hi-code.tistory.com/123
- ๋ชจ๋ : https://hi-code.tistory.com/124
- ํจํค์ง : https://hi-code.tistory.com/125
- ์์ธ ์ฒ๋ฆฌ : https://hi-code.tistory.com/126
- ๋ด์ฅ ํจ์ : https://hi-code.tistory.com/127
- ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ : https://hi-code.tistory.com/128
- ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ :https://hi-code.tistory.com/129
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 |
#๋์ด ์์
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