{"id":963825,"date":"2024-12-27T04:22:17","date_gmt":"2024-12-26T20:22:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/963825.html"},"modified":"2024-12-27T04:22:20","modified_gmt":"2024-12-26T20:22:20","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e6%9c%88%e4%bb%bd","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/963825.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5224\u65ad\u6708\u4efd"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180931\/1489afe3-f856-427b-9def-cb77bf7e514b.webp\" alt=\"python\u4e2d\u5982\u4f55\u5224\u65ad\u6708\u4efd\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5224\u65ad\u6708\u4efd\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c<strong>\u5305\u62ec\u4f7f\u7528datetime\u6a21\u5757\u3001calendar\u6a21\u5757\u548c\u624b\u52a8\u521b\u5efa\u6708\u4efd\u5217\u8868<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528datetime\u6a21\u5757\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65e5\u671f\u548c\u65f6\u95f4\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u83b7\u53d6\u5f53\u524d\u6708\u4efd\u6216\u5904\u7406\u65e5\u671f\u76f8\u5173\u7684\u64cd\u4f5c\u3002\u4e0b\u9762\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5224\u65ad\u6708\u4efd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528DATETIME\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u6a21\u5757\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u5f53\u524d\u6708\u4efd\u6216\u4ece\u65e5\u671f\u5bf9\u8c61\u4e2d\u63d0\u53d6\u6708\u4efd\u3002<\/p>\n<\/p>\n<p><h4>1. \u83b7\u53d6\u5f53\u524d\u6708\u4efd<\/h4>\n<\/p>\n<p><p>\u8981\u83b7\u53d6\u5f53\u524d\u6708\u4efd\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime.now()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u7136\u540e\u4f7f\u7528<code>.month<\/code>\u5c5e\u6027\u63d0\u53d6\u6708\u4efd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.now()<\/p>\n<h2><strong>\u63d0\u53d6\u6708\u4efd<\/strong><\/h2>\n<p>current_month = now.month<\/p>\n<p>print(f&quot;\u5f53\u524d\u6708\u4efd\u662f\uff1a{current_month}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>datetime.now()<\/code>\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u7136\u540e\u901a\u8fc7<code>.month<\/code>\u5c5e\u6027\u63d0\u53d6\u51fa\u5f53\u524d\u6708\u4efd\u3002<\/p>\n<\/p>\n<p><h4>2. \u4ece\u65e5\u671f\u5bf9\u8c61\u4e2d\u63d0\u53d6\u6708\u4efd<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u7279\u5b9a\u7684\u65e5\u671f\u5bf9\u8c61\uff0c\u5e76\u60f3\u4ece\u4e2d\u63d0\u53d6\u6708\u4efd\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>date_str = &#39;2023-10-15&#39;<\/p>\n<p>date_obj = datetime.strptime(date_str, &#39;%Y-%m-%d&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u6708\u4efd<\/strong><\/h2>\n<p>month = date_obj.month<\/p>\n<p>print(f&quot;\u6307\u5b9a\u65e5\u671f\u7684\u6708\u4efd\u662f\uff1a{month}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>datetime.strptime()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>.month<\/code>\u5c5e\u6027\u63d0\u53d6\u6708\u4efd\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528CALENDAR\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>calendar<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5b9e\u7528\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u5904\u7406\u65e5\u5386\u76f8\u5173\u7684\u4efb\u52a1\u3002\u867d\u7136\u5b83\u4e0d\u76f4\u63a5\u63d0\u4f9b\u83b7\u53d6\u6708\u4efd\u7684\u529f\u80fd\uff0c\u4f46\u53ef\u4ee5\u7528\u4e8e\u68c0\u67e5\u6708\u4efd\u7684\u5929\u6570\u548c\u5176\u4ed6\u65e5\u5386\u76f8\u5173\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1. \u83b7\u53d6\u6708\u4efd\u540d\u79f0<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u60f3\u83b7\u53d6\u6708\u4efd\u7684\u540d\u79f0\uff0c\u53ef\u4ee5\u4f7f\u7528<code>calendar.month_name<\/code>\u5c5e\u6027\uff0c\u5b83\u662f\u4e00\u4e2a\u5305\u542b\u6708\u4efd\u540d\u79f0\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import calendar<\/p>\n<h2><strong>\u83b7\u53d6\u6708\u4efd\u540d\u79f0<\/strong><\/h2>\n<p>month_number = 10<\/p>\n<p>month_name = calendar.month_name[month_number]<\/p>\n<p>print(f&quot;\u6708\u4efd {month_number} \u7684\u540d\u79f0\u662f\uff1a{month_name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee<code>calendar.month_name<\/code>\u5217\u8868\u6765\u83b7\u53d6\u6708\u4efd\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><h4>2. \u68c0\u67e5\u6708\u4efd\u7684\u5929\u6570<\/h4>\n<\/p>\n<p><p><code>calendar<\/code>\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e2a\u51fd\u6570<code>monthrange()<\/code>\uff0c\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u67d0\u5e74\u67d0\u6708\u7684\u5929\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import calendar<\/p>\n<h2><strong>\u83b7\u53d6\u67d0\u5e74\u67d0\u6708\u7684\u5929\u6570<\/strong><\/h2>\n<p>year = 2023<\/p>\n<p>month = 10<\/p>\n<p>_, num_days = calendar.monthrange(year, month)<\/p>\n<p>print(f&quot;{year} \u5e74 {month} \u6708\u6709 {num_days} \u5929&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>calendar.monthrange()<\/code>\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\uff0c\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u8be5\u6708\u7b2c\u4e00\u5929\u7684\u661f\u671f\u51e0\uff080-6\uff09\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u662f\u8be5\u6708\u7684\u5929\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u624b\u52a8\u521b\u5efa\u6708\u4efd\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u5904\u7406\u81ea\u5b9a\u4e49\u7684\u6708\u4efd\u6570\u636e\uff0c\u53ef\u4ee5\u624b\u52a8\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6708\u4efd\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u6708\u4efd\u5217\u8868<\/p>\n<p>months = [<\/p>\n<p>    &quot;January&quot;, &quot;February&quot;, &quot;March&quot;, &quot;April&quot;, &quot;May&quot;, &quot;June&quot;,<\/p>\n<p>    &quot;July&quot;, &quot;August&quot;, &quot;September&quot;, &quot;October&quot;, &quot;November&quot;, &quot;December&quot;<\/p>\n<p>]<\/p>\n<h2><strong>\u83b7\u53d6\u6708\u4efd\u540d\u79f0<\/strong><\/h2>\n<p>month_number = 10<\/p>\n<p>month_name = months[month_number - 1]<\/p>\n<p>print(f&quot;\u6708\u4efd {month_number} \u7684\u540d\u79f0\u662f\uff1a{month_name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u624b\u52a8\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u6708\u4efd\u540d\u79f0\u7684\u5217\u8868\uff0c\u5e76\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u6765\u83b7\u53d6\u7279\u5b9a\u6708\u4efd\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u4f7f\u7528\u5176\u4ed6\u6a21\u5757\u548c\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u4f7f\u7528\u591a\u4e2a\u6a21\u5757\u548c\u65b9\u6cd5\u6765\u5b9e\u73b0\u590d\u6742\u7684\u65e5\u671f\u548c\u65f6\u95f4\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u7ed3\u5408<code>datetime<\/code>\u548c<code>calendar<\/code>\u6a21\u5757\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u9ad8\u7ea7\u7684\u65e5\u671f\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h4>1. \u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u6708\u4efd\u5dee<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u65e5\u671f<\/strong><\/h2>\n<p>date1 = datetime(2022, 5, 15)<\/p>\n<p>date2 = datetime(2023, 10, 15)<\/p>\n<h2><strong>\u8ba1\u7b97\u6708\u4efd\u5dee<\/strong><\/h2>\n<p>month_diff = (date2.year - date1.year) * 12 + date2.month - date1.month<\/p>\n<p>print(f&quot;\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u6708\u4efd\u5dee\u662f\uff1a{month_diff} \u4e2a\u6708&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8ba1\u7b97\u5e74\u4efd\u5dee\u548c\u6708\u4efd\u5dee\u6765\u83b7\u53d6\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u603b\u6708\u4efd\u5dee\u3002<\/p>\n<\/p>\n<p><h4>2. \u68c0\u67e5\u65e5\u671f\u662f\u5426\u5728\u7279\u5b9a\u6708\u4efd\u5185<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u68c0\u67e5\u4e00\u4e2a\u65e5\u671f\u662f\u5426\u5728\u7279\u5b9a\u7684\u6708\u4efd\u5185\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u5b9a\u4e49\u65e5\u671f\u548c\u76ee\u6807\u6708\u4efd<\/strong><\/h2>\n<p>date_to_check = datetime(2023, 10, 15)<\/p>\n<p>target_month = 10<\/p>\n<h2><strong>\u68c0\u67e5\u65e5\u671f\u662f\u5426\u5728\u76ee\u6807\u6708\u4efd\u5185<\/strong><\/h2>\n<p>if date_to_check.month == target_month:<\/p>\n<p>    print(&quot;\u65e5\u671f\u5728\u76ee\u6807\u6708\u4efd\u5185&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u65e5\u671f\u4e0d\u5728\u76ee\u6807\u6708\u4efd\u5185&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u6bd4\u8f83\u65e5\u671f\u5bf9\u8c61\u7684\u6708\u4efd\u5c5e\u6027\u548c\u76ee\u6807\u6708\u4efd\u6765\u786e\u5b9a\u65e5\u671f\u662f\u5426\u5728\u6307\u5b9a\u6708\u4efd\u5185\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u4e0d\u540c\u683c\u5f0f\u7684\u65e5\u671f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u65e5\u671f\u6570\u636e\u53ef\u80fd\u4f1a\u4ee5\u4e0d\u540c\u7684\u683c\u5f0f\u51fa\u73b0\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u7684<code>strptime()<\/code>\u65b9\u6cd5\u6765\u89e3\u6790\u4e0d\u540c\u683c\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>1. \u89e3\u6790\u4e0d\u540c\u683c\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u4e0d\u540c\u683c\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_str1 = &#39;15\/10\/2023&#39;<\/p>\n<p>date_str2 = &#39;October 15, 2023&#39;<\/p>\n<h2><strong>\u89e3\u6790\u65e5\u671f\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_obj1 = datetime.strptime(date_str1, &#39;%d\/%m\/%Y&#39;)<\/p>\n<p>date_obj2 = datetime.strptime(date_str2, &#39;%B %d, %Y&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u6708\u4efd<\/strong><\/h2>\n<p>month1 = date_obj1.month<\/p>\n<p>month2 = date_obj2.month<\/p>\n<p>print(f&quot;\u65e5\u671f1\u7684\u6708\u4efd\u662f\uff1a{month1}&quot;)<\/p>\n<p>print(f&quot;\u65e5\u671f2\u7684\u6708\u4efd\u662f\uff1a{month2}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>strptime()<\/code>\u65b9\u6cd5\u89e3\u6790\u4e86\u4e24\u79cd\u4e0d\u540c\u683c\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32\uff0c\u5e76\u63d0\u53d6\u51fa\u6708\u4efd\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5224\u65ad\u6708\u4efd\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c<strong>\u4f7f\u7528datetime\u6a21\u5757\u662f\u6700\u5e38\u7528\u548c\u65b9\u4fbf\u7684\u65b9\u6cd5<\/strong>\u3002\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u4e0d\u540c\u7684\u6a21\u5757\u548c\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5904\u7406\u5404\u79cd\u65e5\u671f\u548c\u65f6\u95f4\u76f8\u5173\u7684\u4efb\u52a1\uff0c\u5305\u62ec\u83b7\u53d6\u5f53\u524d\u6708\u4efd\u3001\u89e3\u6790\u65e5\u671f\u5b57\u7b26\u4e32\u3001\u8ba1\u7b97\u65e5\u671f\u5dee\u5f02\u7b49\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u65e5\u671f\u64cd\u4f5c\uff0c\u8fd8\u662f\u590d\u6742\u7684\u65e5\u671f\u8ba1\u7b97\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u6765\u5e2e\u52a9\u5f00\u53d1\u8005\u9ad8\u6548\u5730\u5b8c\u6210\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u5f53\u524d\u6708\u4efd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>datetime<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u5f53\u524d\u6708\u4efd\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>datetime.datetime.now()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u7136\u540e\u901a\u8fc7<code>.month<\/code>\u5c5e\u6027\u63d0\u53d6\u6708\u4efd\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import datetime\ncurrent_month = datetime.datetime.now().month\nprint(current_month)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\u5f53\u524d\u7684\u6708\u4efd\u6570\u5b57\uff081\u523012\uff09\u3002<\/p>\n<p><strong>\u5982\u4f55\u5224\u65ad\u7ed9\u5b9a\u7684\u6708\u4efd\u662f\u5426\u6709\u6548\uff1f<\/strong><br \/>\u8981\u5224\u65ad\u4e00\u4e2a\u7ed9\u5b9a\u7684\u6708\u4efd\u662f\u5426\u6709\u6548\uff0c\u53ef\u4ee5\u7b80\u5355\u5730\u68c0\u67e5\u5b83\u662f\u5426\u57281\u523012\u7684\u8303\u56f4\u5185\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">def is_valid_month(month):\n    return 1 &lt;= month &lt;= 12\n\nprint(is_valid_month(5))  # \u8f93\u51fa: True\nprint(is_valid_month(13)) # \u8f93\u51fa: False\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6574\u6570\u4f5c\u4e3a\u8f93\u5165\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u8868\u793a\u8be5\u6708\u4efd\u662f\u5426\u6709\u6548\u3002<\/p>\n<p><strong>\u5982\u4f55\u6839\u636e\u6708\u4efd\u8fd4\u56de\u5b63\u8282\u4fe1\u606f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u8f93\u5165\u7684\u6708\u4efd\u8fd4\u56de\u76f8\u5e94\u7684\u5b63\u8282\u4fe1\u606f\u3002\u5b63\u8282\u7684\u5212\u5206\u901a\u5e38\u4e3a\uff1a\u6625\u5b63\uff083-5\u6708\uff09\u3001\u590f\u5b63\uff086-8\u6708\uff09\u3001\u79cb\u5b63\uff089-11\u6708\uff09\u3001\u51ac\u5b63\uff0812-2\u6708\uff09\u3002\u4e0b\u9762\u662f\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">def get_season(month):\n    if month in [3, 4, 5]:\n        return &quot;\u6625\u5b63&quot;\n    elif month in [6, 7, 8]:\n        return &quot;\u590f\u5b63&quot;\n    elif month in [9, 10, 11]:\n        return &quot;\u79cb\u5b63&quot;\n    elif month in [12, 1, 2]:\n        return &quot;\u51ac\u5b63&quot;\n    else:\n        return &quot;\u65e0\u6548\u7684\u6708\u4efd&quot;\n\nprint(get_season(4))  # \u8f93\u51fa: \u6625\u5b63\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u63a5\u6536\u6708\u4efd\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u5b63\u8282\u540d\u79f0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5224\u65ad\u6708\u4efd\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528datetime\u6a21\u5757\u3001calendar\u6a21\u5757\u548c\u624b\u52a8\u521b\u5efa\u6708\u4efd [&hellip;]","protected":false},"author":3,"featured_media":963830,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/963825"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=963825"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/963825\/revisions"}],"predecessor-version":[{"id":963835,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/963825\/revisions\/963835"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/963830"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=963825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=963825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=963825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}