{"id":1134518,"date":"2025-01-08T21:16:23","date_gmt":"2025-01-08T13:16:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1134518.html"},"modified":"2025-01-08T21:16:26","modified_gmt":"2025-01-08T13:16:26","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%8f%96%e6%95%b0%e5%ad%97%e7%9a%84%e6%9c%80%e5%90%8e%e4%b8%80%e4%bd%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1134518.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u53d6\u6570\u5b57\u7684\u6700\u540e\u4e00\u4f4d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103421\/954319fa-2180-4108-b137-7707f3ec71ff.webp\" alt=\"python\u4e2d\u5982\u4f55\u53d6\u6570\u5b57\u7684\u6700\u540e\u4e00\u4f4d\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53d6\u6570\u5b57\u7684\u6700\u540e\u4e00\u4f4d\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u6a21\u8fd0\u7b97\u3001\u5b57\u7b26\u4e32\u5904\u7406\u3001\u5217\u8868\u5904\u7406\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u6a21\u8fd0\u7b97\u662f\u6700\u5e38\u89c1\u4e14\u6700\u7b80\u4fbf\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u53d6\u6a21\u8fd0\u7b97\u7b26 % \u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u6570\u5b57\u7684\u6700\u540e\u4e00\u4f4d\u3002<strong>\u4f8b\u5982\uff1a<code>last_digit = number % 10<\/code>\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\uff1a<strong>\u6a21\u8fd0\u7b97\uff08%\uff09\u662f\u83b7\u53d6\u6570\u5b57\u6700\u540e\u4e00\u4f4d\u6700\u5e38\u89c1\u7684\u65b9\u6cd5<\/strong>\uff0c\u8fd9\u662f\u56e0\u4e3a\u6a21\u8fd0\u7b97\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u88ab\u9664\u6570\u9664\u4ee5\u9664\u6570\u7684\u4f59\u6570\u3002\u5bf9\u4e8e\u4efb\u610f\u6574\u6570 <code>number<\/code>\uff0c\u5c06\u5176\u5bf910\u53d6\u6a21\uff08\u5373 <code>number % 10<\/code>\uff09\u53ef\u4ee5\u76f4\u63a5\u5f97\u5230\u8fd9\u4e2a\u6570\u5b57\u7684\u6700\u540e\u4e00\u4f4d\u3002\u8fd9\u4e2a\u65b9\u6cd5\u65e2\u9ad8\u6548\u53c8\u7b80\u6d01\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u8bb2\u89e3\u5404\u79cd\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u6a21\u8fd0\u7b97<\/strong><\/h2>\n<p><p>\u6a21\u8fd0\u7b97\u662f\u83b7\u53d6\u6570\u5b57\u6700\u540e\u4e00\u4f4d\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u5bf9\u4e8e\u4e00\u4e2a\u6574\u6570 <code>number<\/code>\uff0c\u5c06\u5176\u5bf910\u53d6\u6a21\uff0c\u5373 <code>number % 10<\/code>\uff0c\u53ef\u4ee5\u76f4\u63a5\u5f97\u5230\u8fd9\u4e2a\u6570\u5b57\u7684\u6700\u540e\u4e00\u4f4d\u3002<\/p>\n<\/p>\n<p><h2>1.1 \u57fa\u672c\u539f\u7406<\/h2>\n<\/p>\n<p><p>\u6a21\u8fd0\u7b97 <code>%<\/code> \u662f\u6570\u5b66\u4e2d\u7684\u4e00\u79cd\u8fd0\u7b97\uff0c\u8868\u793a\u4e24\u4e2a\u6570\u76f8\u9664\u7684\u4f59\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 12345<\/p>\n<p>last_digit = number % 10<\/p>\n<p>print(last_digit)  # \u8f93\u51fa\uff1a5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>12345 % 10<\/code> \u7684\u7ed3\u679c\u662f5\uff0c\u8fd9\u6b63\u662f <code>12345<\/code> \u7684\u6700\u540e\u4e00\u4f4d\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h2>1.2 \u5e94\u7528\u573a\u666f<\/h2>\n<\/p>\n<p><p>\u6a21\u8fd0\u7b97\u65b9\u6cd5\u9002\u7528\u4e8e\u5404\u79cd\u6574\u6570\uff0c\u65e0\u8bba\u6570\u5b57\u7684\u957f\u5ea6\u5982\u4f55\uff0c\u90fd\u80fd\u5feb\u901f\u83b7\u53d6\u6700\u540e\u4e00\u4f4d\u6570\u5b57\u3002\u5b83\u7684\u8ba1\u7b97\u590d\u6742\u5ea6\u4e3aO(1)\uff0c\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h2>1.3 \u4f18\u7f3a\u70b9<\/h2>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u7b80\u5355\u6613\u61c2\uff0c\u8ba1\u7b97\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a\u53ea\u9002\u7528\u4e8e\u6574\u6570\uff0c\u4e0d\u9002\u7528\u4e8e\u6d6e\u70b9\u6570\u6216\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u5b57\u7b26\u4e32\u5904\u7406<\/strong><\/h2>\n<p><p>\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u901a\u8fc7\u7d22\u5f15\u83b7\u53d6\u6700\u540e\u4e00\u4f4d\u5b57\u7b26\uff0c\u7136\u540e\u518d\u8f6c\u6362\u56de\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h2>2.1 \u57fa\u672c\u539f\u7406<\/h2>\n<\/p>\n<p><p>\u5c06\u6570\u5b57\u901a\u8fc7 <code>str()<\/code> \u51fd\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528\u7d22\u5f15 <code>[-1]<\/code> \u83b7\u53d6\u6700\u540e\u4e00\u4f4d\u5b57\u7b26\uff0c\u6700\u540e\u901a\u8fc7 <code>int()<\/code> \u51fd\u6570\u5c06\u5b57\u7b26\u8f6c\u6362\u56de\u6570\u5b57\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 12345<\/p>\n<p>last_digit = int(str(number)[-1])<\/p>\n<p>print(last_digit)  # \u8f93\u51fa\uff1a5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2.2 \u5e94\u7528\u573a\u666f<\/h2>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u9002\u7528\u4e8e\u4efb\u4f55\u5f62\u5f0f\u7684\u6570\u5b57\uff0c\u5305\u62ec\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3002\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u6216\u7279\u6b8a\u683c\u5f0f\u7684\u6570\u5b57\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u540c\u6837\u9002\u7528\u3002<\/p>\n<\/p>\n<p><h2>2.3 \u4f18\u7f3a\u70b9<\/h2>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u9002\u7528\u8303\u56f4\u5e7f\uff0c\u80fd\u5904\u7406\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a\u76f8\u6bd4\u6a21\u8fd0\u7b97\u65b9\u6cd5\uff0c\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u7a0d\u663e\u590d\u6742\uff0c\u4e14\u6548\u7387\u7565\u4f4e\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u5217\u8868\u5904\u7406<\/strong><\/h2>\n<p><p>\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u901a\u8fc7\u7d22\u5f15\u83b7\u53d6\u6700\u540e\u4e00\u4f4d\u5143\u7d20\uff0c\u7136\u540e\u518d\u8f6c\u6362\u56de\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h2>3.1 \u57fa\u672c\u539f\u7406<\/h2>\n<\/p>\n<p><p>\u5c06\u6570\u5b57\u901a\u8fc7 <code>str()<\/code> \u51fd\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u518d\u901a\u8fc7 <code>list()<\/code> \u51fd\u6570\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u4f7f\u7528\u7d22\u5f15 <code>[-1]<\/code> \u83b7\u53d6\u6700\u540e\u4e00\u4f4d\u5143\u7d20\uff0c\u6700\u540e\u901a\u8fc7 <code>int()<\/code> \u51fd\u6570\u5c06\u5b57\u7b26\u8f6c\u6362\u56de\u6570\u5b57\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 12345<\/p>\n<p>last_digit = int(list(str(number))[-1])<\/p>\n<p>print(last_digit)  # \u8f93\u51fa\uff1a5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3.2 \u5e94\u7528\u573a\u666f<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u5904\u7406\u65b9\u6cd5\u548c\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u9002\u7528\u4e8e\u4efb\u4f55\u5f62\u5f0f\u7684\u6570\u5b57\uff0c\u5305\u62ec\u6574\u6570\u548c\u6d6e\u70b9\u6570\uff0c\u4f46\u4e00\u822c\u4e0d\u5e38\u7528\uff0c\u56e0\u4e3a\u6b65\u9aa4\u8f83\u4e3a\u7e41\u7410\u3002<\/p>\n<\/p>\n<p><h2>3.3 \u4f18\u7f3a\u70b9<\/h2>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u80fd\u5904\u7406\u5404\u79cd\u5f62\u5f0f\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a\u6b65\u9aa4\u7e41\u7410\uff0c\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u6570\u5b66\u65b9\u6cd5<\/strong><\/h2>\n<p><p>\u901a\u8fc7\u6570\u5b66\u8fd0\u7b97\u53bb\u6389\u6570\u5b57\u7684\u5176\u4ed6\u4f4d\u6570\uff0c\u76f4\u63a5\u83b7\u53d6\u6700\u540e\u4e00\u4f4d\u3002<\/p>\n<\/p>\n<p><h2>4.1 \u57fa\u672c\u539f\u7406<\/h2>\n<\/p>\n<p><p>\u5229\u7528\u9664\u6cd5\u548c\u4e58\u6cd5\uff0c\u5c06\u6570\u5b57\u7684\u5176\u4ed6\u4f4d\u6570\u53bb\u6389\uff0c\u4ec5\u4fdd\u7559\u6700\u540e\u4e00\u4f4d\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 12345<\/p>\n<p>last_digit = number - (number \/\/ 10) * 10<\/p>\n<p>print(last_digit)  # \u8f93\u51fa\uff1a5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>number \/\/ 10<\/code> \u5f97\u5230 <code>1234<\/code>\uff0c\u518d\u4e58\u4ee5 <code>10<\/code> \u5f97\u5230 <code>12340<\/code>\uff0c\u7136\u540e\u7528 <code>12345<\/code> \u51cf\u53bb <code>12340<\/code>\uff0c\u5f97\u5230 <code>5<\/code>\u3002<\/p>\n<\/p>\n<p><h2>4.2 \u5e94\u7528\u573a\u666f<\/h2>\n<\/p>\n<p><p>\u6570\u5b66\u65b9\u6cd5\u9002\u7528\u4e8e\u5404\u79cd\u6574\u6570\uff0c\u4f46\u4e0d\u9002\u7528\u4e8e\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h2>4.3 \u4f18\u7f3a\u70b9<\/h2>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u4e0d\u9700\u8981\u8f6c\u6362\u6570\u636e\u7c7b\u578b\uff0c\u8ba1\u7b97\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a\u6b65\u9aa4\u8f83\u591a\uff0c\u4e0d\u5982\u6a21\u8fd0\u7b97\u7b80\u6d01\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u4f4d\u8fd0\u7b97<\/strong><\/h2>\n<p><p>\u901a\u8fc7\u4f4d\u8fd0\u7b97\u83b7\u53d6\u6570\u5b57\u7684\u6700\u540e\u4e00\u4f4d\u3002<\/p>\n<\/p>\n<p><h2>5.1 \u57fa\u672c\u539f\u7406<\/h2>\n<\/p>\n<p><p>\u4f4d\u8fd0\u7b97\u662f\u4e00\u79cd\u5e95\u5c42\u64cd\u4f5c\uff0c\u53ef\u4ee5\u76f4\u63a5\u64cd\u4f5c\u4e8c\u8fdb\u5236\u4f4d\u3002\u5bf9\u4e8e\u5341\u8fdb\u5236\u6570\uff0c\u4f4d\u8fd0\u7b97\u7684\u5e94\u7528\u8f83\u5c11\uff0c\u4f46\u5bf9\u4e8e\u4e8c\u8fdb\u5236\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u63a9\u7801\u64cd\u4f5c\u83b7\u53d6\u6700\u540e\u4e00\u4f4d\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 0b1101  # \u4e8c\u8fdb\u5236\u6570<\/p>\n<p>last_digit = number &amp; 1<\/p>\n<p>print(last_digit)  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>0b1101<\/code> \u7684\u4e8c\u8fdb\u5236\u5f62\u5f0f\u662f <code>1101<\/code>\uff0c\u901a\u8fc7 <code>&amp; 1<\/code> \u64cd\u4f5c\u83b7\u53d6\u6700\u540e\u4e00\u4f4d\u3002<\/p>\n<\/p>\n<p><h2>5.2 \u5e94\u7528\u573a\u666f<\/h2>\n<\/p>\n<p><p>\u4f4d\u8fd0\u7b97\u65b9\u6cd5\u9002\u7528\u4e8e\u4e8c\u8fdb\u5236\u6570\uff0c\u5c24\u5176\u5728\u5e95\u5c42\u7f16\u7a0b\u6216\u9700\u8981\u9ad8\u6548\u8ba1\u7b97\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h2>5.3 \u4f18\u7f3a\u70b9<\/h2>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u8ba1\u7b97\u6781\u5176\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a\u53ea\u9002\u7528\u4e8e\u4e8c\u8fdb\u5236\u6570\uff0c\u4e0d\u9002\u7528\u4e8e\u5341\u8fdb\u5236\u6570\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u7efc\u5408\u5e94\u7528<\/strong><\/h2>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u4e0d\u540c\u7684\u65b9\u6cd5\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f18\u5148\u8003\u8651\u6a21\u8fd0\u7b97\uff0c\u800c\u5728\u5904\u7406\u7279\u6b8a\u683c\u5f0f\u7684\u6570\u5b57\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>6.1 \u5b9e\u4f8b\u5206\u6790<\/h2>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6570\u5b57\u7684\u5217\u8868\uff0c\u9700\u8981\u83b7\u53d6\u6bcf\u4e2a\u6570\u5b57\u7684\u6700\u540e\u4e00\u4f4d\u5e76\u5b58\u50a8\u5230\u65b0\u5217\u8868\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [123, 456, 789, 101112]<\/p>\n<p>last_digits = [number % 10 for number in numbers]<\/p>\n<p>print(last_digits)  # \u8f93\u51fa\uff1a[3, 6, 9, 2]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u6a21\u8fd0\u7b97\u65b9\u6cd5\uff0c\u7ed3\u5408\u5217\u8868\u89e3\u6790\uff0c\u5feb\u901f\u83b7\u53d6\u4e86\u6bcf\u4e2a\u6570\u5b57\u7684\u6700\u540e\u4e00\u4f4d\u3002<\/p>\n<\/p>\n<p><h2>6.2 \u6027\u80fd\u6bd4\u8f83<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>timeit<\/code> \u6a21\u5757\u8fdb\u884c\u6d4b\u8bd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<p>number = 123456789<\/p>\n<h2><strong>\u6a21\u8fd0\u7b97<\/strong><\/h2>\n<p>mod_time = timeit.timeit(&#39;number % 10&#39;, globals=globals(), number=1000000)<\/p>\n<p>print(f&quot;\u6a21\u8fd0\u7b97\u65f6\u95f4\uff1a{mod_time}\u79d2&quot;)<\/p>\n<h2><strong>\u5b57\u7b26\u4e32\u5904\u7406<\/strong><\/h2>\n<p>str_time = timeit.timeit(&#39;int(str(number)[-1])&#39;, globals=globals(), number=1000000)<\/p>\n<p>print(f&quot;\u5b57\u7b26\u4e32\u5904\u7406\u65f6\u95f4\uff1a{str_time}\u79d2&quot;)<\/p>\n<h2><strong>\u5217\u8868\u5904\u7406<\/strong><\/h2>\n<p>list_time = timeit.timeit(&#39;int(list(str(number))[-1])&#39;, globals=globals(), number=1000000)<\/p>\n<p>print(f&quot;\u5217\u8868\u5904\u7406\u65f6\u95f4\uff1a{list_time}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6027\u80fd\u6d4b\u8bd5\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u6a21\u8fd0\u7b97\u65b9\u6cd5\u7684\u6548\u7387\u6700\u9ad8\uff0c\u5b57\u7b26\u4e32\u5904\u7406\u6b21\u4e4b\uff0c\u5217\u8868\u5904\u7406\u65b9\u6cd5\u6700\u6162\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/strong><\/h2>\n<p><p>\u5728\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\uff0c\u4e0b\u9762\u5c06\u4ecb\u7ecd\u8fd9\u4e9b\u95ee\u9898\u53ca\u5176\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><h2>7.1 \u5904\u7406\u8d1f\u6570<\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8d1f\u6570\uff0c\u6a21\u8fd0\u7b97\u548c\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u90fd\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7ed3\u679c\u7684\u7b26\u53f7\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = -12345<\/p>\n<p>last_digit = number % 10<\/p>\n<p>print(last_digit)  # \u8f93\u51fa\uff1a5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u867d\u7136\u8f93\u5165\u662f\u8d1f\u6570\uff0c\u4f46\u6700\u540e\u4e00\u4f4d\u7684\u7edd\u5bf9\u503c\u4ecd\u7136\u662f5\u3002<\/p>\n<\/p>\n<p><h2>7.2 \u5904\u7406\u6d6e\u70b9\u6570<\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 123.45<\/p>\n<p>last_digit = int(str(number)[-1])<\/p>\n<p>print(last_digit)  # \u8f93\u51fa\uff1a5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u518d\u4f7f\u7528\u6a21\u8fd0\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 123.45<\/p>\n<p>last_digit = int(number * 10) % 10<\/p>\n<p>print(last_digit)  # \u8f93\u51fa\uff1a5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>7.3 \u5904\u7406\u5927\u6570<\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\uff0c\u6a21\u8fd0\u7b97\u548c\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u90fd\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u5efa\u8bae\u4f18\u5148\u4f7f\u7528\u6a21\u8fd0\u7b97\uff0c\u56e0\u4e3a\u6548\u7387\u66f4\u9ad8\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 12345678901234567890<\/p>\n<p>last_digit = number % 10<\/p>\n<p>print(last_digit)  # \u8f93\u51fa\uff1a0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516b\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>Python\u4e2d\u83b7\u53d6\u6570\u5b57\u6700\u540e\u4e00\u4f4d\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5305\u62ec\u6a21\u8fd0\u7b97\u3001\u5b57\u7b26\u4e32\u5904\u7406\u3001\u5217\u8868\u5904\u7406\u3001\u6570\u5b66\u65b9\u6cd5\u548c\u4f4d\u8fd0\u7b97\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u5176\u4e2d\uff0c<strong>\u6a21\u8fd0\u7b97\u662f\u6700\u5e38\u7528\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5<\/strong>\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u6574\u6570\uff1b<strong>\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u9002\u7528\u8303\u56f4\u5e7f<\/strong>\uff0c\u80fd\u5904\u7406\u5404\u79cd\u5f62\u5f0f\u7684\u6570\u5b57\uff1b<strong>\u6570\u5b66\u65b9\u6cd5\u548c\u4f4d\u8fd0\u7b97\u65b9\u6cd5\u5219\u9002\u7528\u4e8e\u7279\u5b9a\u573a\u666f<\/strong>\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u6027\u80fd\u6d4b\u8bd5\uff0c\u4f18\u5316\u4ee3\u7801\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u53d6\u4e00\u4e2a\u6570\u5b57\u7684\u6700\u540e\u4e00\u4f4d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u53d6\u6a21\u8fd0\u7b97\u7b26 <code>%<\/code> \u6765\u63d0\u53d6\u6570\u5b57\u7684\u6700\u540e\u4e00\u4f4d\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <code>number % 10<\/code> \u53ef\u4ee5\u83b7\u53d6 <code>number<\/code> \u7684\u6700\u540e\u4e00\u4f4d\u6570\u5b57\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u6b63\u6574\u6570\u548c\u8d1f\u6574\u6570\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u63d0\u53d6\u6d6e\u70b9\u6570\u7684\u6700\u540e\u4e00\u4f4d\uff0c\u8be5\u600e\u4e48\u505a\uff1f<\/strong><br \/>\u5bf9\u4e8e\u6d6e\u70b9\u6570\uff0c\u9996\u5148\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u63d0\u53d6\u6700\u540e\u4e00\u4f4d\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>str()<\/code> \u51fd\u6570\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u518d\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u6700\u540e\u4e00\u4f4d\u3002\u8bf7\u6ce8\u610f\uff0c\u6d6e\u70b9\u6570\u7684\u6700\u540e\u4e00\u4f4d\u901a\u5e38\u662f\u5c0f\u6570\u70b9\u540e\u7684\u6570\u5b57\uff0c\u56e0\u6b64\u9700\u8981\u7279\u522b\u5904\u7406\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u8d1f\u6570\u7684\u6700\u540e\u4e00\u4f4d\u6570\u5b57\uff1f<\/strong><br \/>\u8d1f\u6570\u7684\u5904\u7406\u4e0e\u6b63\u6570\u7c7b\u4f3c\u3002\u53ef\u4ee5\u4f7f\u7528\u53d6\u6a21\u8fd0\u7b97\u7b26 <code>%<\/code> \u6765\u63d0\u53d6\u8d1f\u6570\u7684\u6700\u540e\u4e00\u4f4d\u3002\u8d1f\u6570\u7684\u6700\u540e\u4e00\u4f4d\u540c\u6837\u53ef\u4ee5\u901a\u8fc7 <code>-number % 10<\/code> \u6765\u83b7\u53d6\uff0c\u8fd9\u6837\u53ef\u4ee5\u4fdd\u8bc1\u7ed3\u679c\u4e3a\u6b63\u503c\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u63d0\u53d6\u6570\u5b57\u7684\u6700\u540e\u4e00\u4f4d\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528\u53d6\u6a21\u8fd0\u7b97\u7b26\u5916\uff0c\u8fd8\u53ef\u4ee5\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u76f4\u63a5\u8bbf\u95ee\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u3002\u901a\u8fc7 <code>str(number)[-1]<\/code> \u53ef\u4ee5\u83b7\u5f97\u6570\u5b57\u7684\u6700\u540e\u4e00\u4f4d\u5b57\u7b26\uff0c\u6ce8\u610f\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u9700\u8981\u786e\u4fdd\u5904\u7406\u7684\u662f\u6570\u5b57\u5b57\u7b26\u4e32\u800c\u4e0d\u662f\u8d1f\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53d6\u6570\u5b57\u7684\u6700\u540e\u4e00\u4f4d\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u6a21\u8fd0\u7b97\u3001\u5b57\u7b26\u4e32\u5904\u7406\u3001\u5217\u8868\u5904\u7406\u7b49\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u6a21\u8fd0\u7b97\u662f\u6700\u5e38\u89c1\u4e14 [&hellip;]","protected":false},"author":3,"featured_media":1134524,"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\/1134518"}],"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=1134518"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1134518\/revisions"}],"predecessor-version":[{"id":1134525,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1134518\/revisions\/1134525"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1134524"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1134518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1134518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1134518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}