在這個簡單的 python 程序中,我們必須顯示一個月的日曆。這是一個初級 python 程序。
要理解這個例子,您應該了解以下 Python 編程主題:
- Python 語法
- Python 數據類型
- Python 模塊
用 python 解釋日曆模塊?
為了顯示日曆,我們在 python 中有一個預定義的模塊,叫做 calendar,它顯示日曆。calendar
模塊的語法類似( calendar.month(yy,mm))where’
- yy 是年份
- mm 為月。
我們用 python 語言對這個日曆模塊進行了許多預定義的操作。例如,
- firstweekday() :返回一周的第一天數字。
- isleap(年份):這是檢查給定的年份是不是閏年。
- 閏日(第 1 年、第 2 年):這將返回兩年限制內的閏日數。
- 月(年、月):本程序使用,打印特定年份的特定月份。
日曆模塊還有很多方法可以使用,這在日曆模塊教程中有所描述。
如何用 Python 語言打印日曆?
在這個簡單的 python 程序中,我們從用戶那裡接受年和月的值,並導入日曆模塊。最後我們調用打印功能,調用calendar
模塊打印年、月**print(calendar.month(yy,mm))**
。讓我們破譯密碼
算法
STEP 1: 導入名為 calendar 的 python 內置模塊,它有很多我們在上面的描述中討論過的公認的操作方法。
STEP 2: 接受用戶使用輸入法輸入的年和月作為字符串,並使用 python 語言中的 int 將其轉換為整數。
STEP 3: 只需使用月(yy,mm) 操作,日曆模塊如同日曆。月(yy,mm) 打印特定年份的特定月份。
Python 源代碼
import calendar
yy = int(input("Enter year: "))
mm = int(input("Enter month: "))
print(calendar.month(yy,mm))
輸出
Enter year: 2020
Enter month: 5
May 2020
Mo Tu We Th Fr Sa Su
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
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/259759.html