存取 datetime 的屬性
假設你正在分析 2000 年的網路泡沫崩盤。為了撰寫報告,你需要把年份、月份與日期的數值指定給變數。
請使用 datetime 物件的屬性,將正確的數值指定到這些變數。
本練習屬於課程
Intermediate Python for Finance
練習說明
- 將崩盤發生的年份指定給變數
yr。 - 將崩盤發生的月份指定給變數
mth。 - 將崩盤發生的日期指定給變數
day。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# March 10, 2000 Tech Bubble Crash
tech_bubble = datetime(2000, 3, 10)
# Access the year
yr = tech_bubble.____
# Access the month
mth = tech_bubble.____
# Access the day
day = tech_bubble.____
print(f"Year: {yr}, Month: {mth}, Day: {day}")