日 - 循環
寫一個計算年齡的程式:
from datetime import datetime dob = input("Enter your Date of Birth (yyyy-mm-dd): ") dob_date = datetime.strptime(dob, "%Y-%m-%d") print(dob_date) current_date = datetime.now() age = current_date.year - dob_date.year print(f"Your age is {age}")
datetime.now() -datetime.now() 是 Python 的 datetime 模組中的函數,它以 datetime 物件的形式傳回目前本機日期和時間,包括微秒。
strptime() - Python 中的 strptime() 方法用於將表示日期和/或時間的字串解析(轉換)為日期時間物件。它是日期時間模組的一部分。
Enter your Date of Birth (yyyy-mm-dd): 1993-03-26 1993-03-26 00:00:00 Your age is 31
另一個方法:
如果結果為負數,請使用此方法
from datetime import date birth_year = 1993 birth_month = 3 birth_day = 26 today = date.today() year = today.year - birth_year month = today.month - birth_month days = today.day - birth_day if month<0: year = year - 1 month = 12+month if days<0: month=month-1 days = 30 + days print (f"You are {year} Years {month} Months {days} Days Old")
You are 31 Years 7 Months 29 Days Old
使用relativedelta的替代方法:
from datetime import datetime from dateutil.relativedelta import relativedelta dob = input("Enter date of birth in yyyy-mm-dd format: ") dob_date = datetime.strptime(dob, "%Y-%m-%d") today = datetime.now() difference = relativedelta(today, dob_date) print(difference.years, " Years ", difference.months, " Months ", difference.days, " days")
relativedelta 是 Python 中 dateutil 模組的一部分,它提供了比標準函式庫的 timedelta 更強大的日期和時間操作操作。它允許您執行諸如添加或減去月份和年份之類的操作,而 timedelta 無法直接處理這些操作。
Enter date of birth in yyyy-mm-dd format: 1993-03-26 31 Years 7 Months 30 days
while 循環的一些例子:
no = 1 while no<=5: print(no, end=' ') no+=1
1 1 1 1 1
no = 1 while no<=10: print(no, end=' ') no+=1
1 2 3 4 5 6 7 8 9 10
no = 10 while no>=1: print(no, end=' ') no-=1
10 9 8 7 6 5 4 3 2 1
no = 1 while no<=10: print(no, end=' ') no+=2
1 3 5 7 9
no=2 while no<=10: print(no, end=' ') no+=2
2 4 6 8 10
no = 3 while no<=10: print(no, end=' ') no+=3
3 6 9
no = 1 total = 0 while no<=5: total = total + no no+=1 print(total)
15
no = 1 while no<=5: print(no*3, end=' ') no+=1
3 6 9 12 15
no = 1 while no<=10: print(no,"*5=",no*5, end='\n') no+=1
1 *5= 5 2 *5= 10 3 *5= 15 4 *5= 20 5 *5= 25 6 *5= 30 7 *5= 35 8 *5= 40 9 *5= 45 10 *5= 50
no = 1 while no<=10: print(no, end = ' ') if no==9: no = 0 no+=2
1 3 5 7 9 2 4 6 8 10
以上是日 - 循環的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Python更易學且易用,C 則更強大但複雜。 1.Python語法簡潔,適合初學者,動態類型和自動內存管理使其易用,但可能導致運行時錯誤。 2.C 提供低級控制和高級特性,適合高性能應用,但學習門檻高,需手動管理內存和類型安全。

每天學習Python兩個小時是否足夠?這取決於你的目標和學習方法。 1)制定清晰的學習計劃,2)選擇合適的學習資源和方法,3)動手實踐和復習鞏固,可以在這段時間內逐步掌握Python的基本知識和高級功能。

Python在開發效率上優於C ,但C 在執行性能上更高。 1.Python的簡潔語法和豐富庫提高開發效率。 2.C 的編譯型特性和硬件控制提升執行性能。選擇時需根據項目需求權衡開發速度與執行效率。

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

Python在自動化、腳本編寫和任務管理中表現出色。 1)自動化:通過標準庫如os、shutil實現文件備份。 2)腳本編寫:使用psutil庫監控系統資源。 3)任務管理:利用schedule庫調度任務。 Python的易用性和豐富庫支持使其在這些領域中成為首選工具。

Python在科學計算中的應用包括數據分析、機器學習、數值模擬和可視化。 1.Numpy提供高效的多維數組和數學函數。 2.SciPy擴展Numpy功能,提供優化和線性代數工具。 3.Pandas用於數據處理和分析。 4.Matplotlib用於生成各種圖表和可視化結果。

Python在Web開發中的關鍵應用包括使用Django和Flask框架、API開發、數據分析與可視化、機器學習與AI、以及性能優化。 1.Django和Flask框架:Django適合快速開發複雜應用,Flask適用於小型或高度自定義項目。 2.API開發:使用Flask或DjangoRESTFramework構建RESTfulAPI。 3.數據分析與可視化:利用Python處理數據並通過Web界面展示。 4.機器學習與AI:Python用於構建智能Web應用。 5.性能優化:通過異步編程、緩存和代碼優
