Function or Method

python 有很多不同 function,因此我想在這篇寫所有 funtion 我會用到。我指的 function 不是傳 function,而是一些 libary function

Math

Datetime 時間

time

程式跑的時間

如果計算程式袍多就可以用這方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import time

# starting time
start = time.time()
# your code
for i in range(1,60):
print(i)

#endtime
end = time.time()
runtime=end - start
print(" Runtime of the program is {runtime:%.8f}")
print(f"Runtime of the program is {runtime:.5f}")
#or
print(f"Runtime of the program is {(end - start):.5f}")

datetime

今天日期

  • date.today 取今天日期
1
2
3
from datetime import date
print(date.today())
#2021-05-12
  • datetime.datetime.today 取今天日期和時間
1
2
3
4
from datetime import date
now=datetime.datetime.today()
print(now)
#2021-01-21 13:29:47.648666
  • date time datetime 取今天日期和時間
1
2
3
4
5
6
7
from datetime import date
from datetime import time
from datetime import datetime
print(datetime.now())
#2021-05-12 16:38:03.287772
print (datetime.time(datetime.now()))
#16:40:27.363532

設定時間

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import datetime
lanunch_date=datetime.date(2021, 1,1)
lanunch_time=datetime.time(22, 27,0)
lanunch_datetime=datetime.datetime(2017,3,30, 22,27, 0)
print(lanunch_date)
#2017-03-30
print(lanunch_time)
#22:27:00
print(lanunch_datetime)
#2017-03-30 22:27:00
print(lanunch_datetime.hour)
#22
print(lanunch_datetime.minute)
#27
print(lanunch_datetime.second)
#0

不同格式例印

  • 印出不同格式 print format

    %c- indicates the local date and time
    %x- indicates the local date
    %X- indicates the local time

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from datetime import date
from datetime import time
from datetime import datetime
now = datetime.now()
print(now.strftime("%c"))
# Wed Nov 23 17:19:57 2022
print(now.strftime("%x"))
# 11/23/22
print(now.strftime("%X"))
# 17:19:57
print(now.strftime("%I:%M:%S %p"))
# 05:19:57 PM
print(now.strftime("%H:%M"))
# 17:19
print(now.strftime("%A, %B, %d, %Y"))
# Wednesday, November, 23, 2022

也可以用:

1
2
3
message = "XXX was born on {:%A, %B, %d, %Y}."
print(message.format(now))
# XXX was born on Wednesday, November, 23, 2022.

convert String into datetime.strptime

1
2
3
4
from datetime import datetime
date_string= "7/20/1969"
print(datetime.strptime(date_string, "%m/%d/%Y"))
#1969-07-20 00:00:00

或是

1
2
3
4
5
from datetime import datetime

date_string = "2012-12-12 10:10:10"
print (datetime.fromisoformat(date_string))
2012-12-12 10:10:10

reference: https://stackoverflow.com/questions/466345/convert-string-jun-1-2005-133pm-into-datetime

filename after datetime

1
2
from datetime import datetime
filename=f"result-{datetime.now():%Y-%m-%d %H-%m-%d}.txt"

enumerate

可以印出 index 索引

Data Type Example

Ex1: String

1
2
for i, char in enumerate ('hello'):
print(i, char)

output:

1
2
3
4
5
6
0 h
1 e
2 l
3 l
4 o
list

Ex2: List

1
2
for j, char in enumerate ([1,2,3]):
print(j, char)

output:

1
2
3
0 1
1 2
2 3

Ex3 tuple

1
2
for k, char in enumerate ((1,2,3)):
print(k, char)

output:

1
2
3
0 1
1 2
2 3

Example print index of 50

1
2
3
for i, char in enumerate (list(range(100))):
if char == 50:
print (f"index of 50 is {i}")

output:

index of 50 is 50