Skip to content

模組和工具

使用模組和工具進行操作

指定日期與設定格式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 指定日期與設定格式

import datetime
adate = datetime.datetime(2024, 6, 21)
print('{:%m-%d-%y}'.format(adate)) # 月份2碼、日期2碼、年份2碼
print('{:%m-%d-%Y}'.format(adate)) # 月份2碼、日期2碼、年份4碼
print('{:%b-%d-%Y}'.format(adate)) # 英文月份簡寫、日期2碼、年份4碼
print('{:%B-%d-%Y}'.format(adate)) # 英文月份全寫、日期2碼、年份4碼
print('{:%b-%d-%y}'.format(adate)) # 英文月份簡寫、日期2碼、年份2碼
print('{:%B-%d-%y}'.format(adate)) # 英文月份全寫、日期2碼、年份2碼

"""
[輸出]
06-21-24
06-21-2024
Jun-21-2024
June-21-2024
Jun-21-24
June-21-24
"""

現在日期

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 現在日期

# 從 datetime 模組匯入 date 類別
from datetime import date

# 將今天日期存入 today 變數
today = date.today()
# 輸出今天日期
print(today)
# 輸出年、月、日
print(today.year)
print(today.month)
print(today.day)

print(today.strftime('%m-%d-%y')) # 月份2碼、日期2碼、年份2碼
print(today.strftime('%m-%d-%Y')) # 月份2碼、日期2碼、年份4碼
print(today.strftime('%b-%d-%Y')) # 英文月份簡寫、日期2碼、年份4碼
print(today.strftime('%B-%d-%Y')) # 英文月份全寫、日期2碼、年份4碼
print(today.strftime('%b-%d-%y')) # 英文月份簡寫、日期2碼、年份2碼
print(today.strftime('%B-%d-%y')) # 英文月份全寫、日期2碼、年份2碼
print(today.strftime('%a')) # 英文星期簡寫
print(today.strftime('%A')) # 英文星期全寫
print(today.strftime('%w')) # 星期代碼 0 ~ 6 (0 是星期日)
print(today.weekday())      # 星期代碼 0 ~ 6 (0 是星期一)
print(today.strftime('%W')) # 年度週序代碼

"""
[輸出範例]
2024-06-21
2024
6
21
06-21-24
06-21-2024
Jun-21-2024
June-21-2024
Jun-21-24
June-21-24
Fri
Friday
5
4
25
"""

現在日期時間

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 現在日期時間

# 從 datetime 模組匯入 datetime 類別
from datetime import datetime

# 將現在日期時間存入 today 變數
today = datetime.today()
now = datetime.now()
# 輸出現在日期時間
print(today)
print(now)
# 輸出年、月、日
print(now.year)
print(now.month)
print(now.day)

print(now.strftime('%m-%d-%y')) # 月份2碼、日期2碼、年份2碼
print(now.strftime('%m-%d-%Y')) # 月份2碼、日期2碼、年份4碼
print(now.strftime('%b-%d-%Y')) # 英文月份簡寫、日期2碼、年份4碼
print(now.strftime('%B-%d-%Y')) # 英文月份全寫、日期2碼、年份4碼
print(now.strftime('%b-%d-%y')) # 英文月份簡寫、日期2碼、年份2碼
print(now.strftime('%B-%d-%y')) # 英文月份全寫、日期2碼、年份2碼
print(now.strftime('%a')) # 英文星期簡寫
print(now.strftime('%A')) # 英文星期全寫
print(now.strftime('%w')) # 星期代碼 0 ~ 6 (0 是星期日)
print(now.weekday())      # 星期代碼 0 ~ 6 (0 是星期一)
print(now.strftime('%W')) # 年度週序代碼

"""
[輸出範例]
2024-06-21 23:08:02.745124
2024-06-21 23:08:02.745124
2024
6
21
06-21-24
06-21-2024
Jun-21-2024
June-21-2024
Jun-21-24
June-21-24
Fri
Friday
5
4
25
"""

指定亂數範圍(1)

1
2
3
4
5
6
7
# 指定亂數範圍(1)

import random

print(random.randrange(11))
print(random.randrange(11, 31))
print(random.randrange(11, 31, 1))

指定亂數範圍(2)

1
2
3
4
5
6
# 指定亂數範圍(2)

import random

print(random.randint(1, 10))
print(random.randint(1, 10)*5)

浮點數的絕對值

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 浮點數的絕對值

import math

def float_to_abs(x):
    print('(1) x:', x)
    # 取浮點數的絕對值
    # x = abs(x)
    x = math.fabs(x)
    print('(2) x:', x)
    # 無條件進位到整數
    x = math.ceil(x)
    print('(3) x:', x)

float_to_abs(-12.48)