{"id":1147141,"date":"2025-01-08T23:22:51","date_gmt":"2025-01-08T15:22:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1147141.html"},"modified":"2025-01-08T23:22:53","modified_gmt":"2025-01-08T15:22:53","slug":"python%e5%a6%82%e4%bd%95%e5%8f%96%e5%87%ba%e4%b8%80%e4%b8%aa%e5%9b%9b%e4%bd%8d%e6%95%b0%e7%9a%84%e6%af%8f%e4%b8%80%e4%bd%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1147141.html","title":{"rendered":"python\u5982\u4f55\u53d6\u51fa\u4e00\u4e2a\u56db\u4f4d\u6570\u7684\u6bcf\u4e00\u4f4d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182846\/6a22f008-c992-4f3c-bc92-630631715244.webp\" alt=\"python\u5982\u4f55\u53d6\u51fa\u4e00\u4e2a\u56db\u4f4d\u6570\u7684\u6bcf\u4e00\u4f4d\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u53d6\u51fa\u4e00\u4e2a\u56db\u4f4d\u6570\u7684\u6bcf\u4e00\u4f4d\uff1a<\/strong><\/p>\n<\/p>\n<p><p><strong>1. \u4f7f\u7528\u6570\u5b66\u8fd0\u7b97\u30012. \u4f7f\u7528\u5b57\u7b26\u4e32\u5904\u7406\u30013. \u4f7f\u7528\u5217\u8868\u89e3\u6790<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u5904\u7406<\/strong>\u662f\u6700\u4e3a\u76f4\u89c2\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u5229\u7528\u7d22\u5f15\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbf\u95ee\u6bcf\u4e00\u4f4d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u6570\u5b66\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u6570\u5b66\u8fd0\u7b97\u6765\u53d6\u51fa\u4e00\u4e2a\u56db\u4f4d\u6570\u7684\u6bcf\u4e00\u4f4d\u662f\u4e00\u4e2a\u4f20\u7edf\u4e14\u6709\u6548\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6574\u9664\u548c\u53d6\u4f59\u8fd0\u7b97\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u6574\u9664\u548c\u53d6\u4f59\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>\u6574\u9664\u8fd0\u7b97\u7b26<code>\/\/<\/code>\u548c\u53d6\u4f59\u8fd0\u7b97\u7b26<code>%<\/code>\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\uff0c\u9010\u6b65\u63d0\u53d6\u6bcf\u4e00\u4f4d\u6570\u5b57\u3002\u4e0b\u9762\u662f\u5177\u4f53\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 1234<\/p>\n<p>thousands = number \/\/ 1000<\/p>\n<p>hundreds = (number % 1000) \/\/ 100<\/p>\n<p>tens = (number % 100) \/\/ 10<\/p>\n<p>units = number % 10<\/p>\n<p>print(thousands, hundreds, tens, units)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u4f7f\u7528<code>\/\/<\/code>\u64cd\u4f5c\u7b26\u5c06\u6570\u5b57\u6574\u96641000\u5f97\u5230\u5343\u4f4d\u6570\uff0c\u7136\u540e\u4f7f\u7528\u53d6\u4f59\u64cd\u4f5c\u7b26<code>%<\/code>\u5f97\u5230\u5269\u4f59\u7684\u6570\u5b57\uff0c\u518d\u9010\u6b65\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u63d0\u53d6\u767e\u4f4d\u3001\u5341\u4f4d\u548c\u4e2a\u4f4d\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u662f\u4e00\u79cd\u76f4\u89c2\u4e14\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\u548c\u5207\u7247\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u53d6\u51fa\u6bcf\u4e00\u4f4d\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7<code>str()<\/code>\u51fd\u6570\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u6bcf\u4e00\u4f4d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 1234<\/p>\n<p>number_str = str(number)<\/p>\n<p>thousands = int(number_str[0])<\/p>\n<p>hundreds = int(number_str[1])<\/p>\n<p>tens = int(number_str[2])<\/p>\n<p>units = int(number_str[3])<\/p>\n<p>print(thousands, hundreds, tens, units)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u7684\u6570\u5b57\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cdPython\u7279\u6709\u7684\u7b80\u6d01\u8bed\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u5feb\u901f\u751f\u6210\u5217\u8868\u3002\u5728\u5904\u7406\u6570\u5b57\u7684\u6bcf\u4e00\u4f4d\u65f6\uff0c\u5217\u8868\u89e3\u6790\u4e5f\u80fd\u63d0\u4f9b\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5217\u8868\u89e3\u6790\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4f7f\u7528\u5217\u8868\u89e3\u6790\u7684\u65b9\u6cd5\u5c06\u6570\u5b57\u7684\u6bcf\u4e00\u4f4d\u63d0\u53d6\u51fa\u6765\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 1234<\/p>\n<p>digits = [int(digit) for digit in str(number)]<\/p>\n<p>print(digits)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5c06\u6570\u5b57\u9996\u5148\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u904d\u5386\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u5e76\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u7b26\u4e32\u5904\u7406\u662f\u6700\u4e3a\u76f4\u89c2\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\u548c\u5207\u7247\u64cd\u4f5c\u6765\u8f7b\u677e\u8bbf\u95ee\u6bcf\u4e00\u4f4d\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h4>\u8be6\u7ec6\u6b65\u9aa4<\/h4>\n<\/p>\n<ol>\n<li><strong>\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong>\uff1a\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/li>\n<li><strong>\u7d22\u5f15\u8bbf\u95ee<\/strong>\uff1a\u901a\u8fc7\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\u8bbf\u95ee\u6bcf\u4e00\u4f4d\u6570\u5b57\u3002<\/li>\n<li><strong>\u8f6c\u6362\u4e3a\u6574\u6570<\/strong>\uff1a\u5c06\u6bcf\u4e00\u4f4d\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570\u7c7b\u578b\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u7684\u6570\u503c\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><p>\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 5678<\/p>\n<p>number_str = str(number)<\/p>\n<p>thousands = int(number_str[0])<\/p>\n<p>hundreds = int(number_str[1])<\/p>\n<p>tens = int(number_str[2])<\/p>\n<p>units = int(number_str[3])<\/p>\n<p>print(&quot;\u5343\u4f4d\u6570:&quot;, thousands)<\/p>\n<p>print(&quot;\u767e\u4f4d\u6570:&quot;, hundreds)<\/p>\n<p>print(&quot;\u5341\u4f4d\u6570:&quot;, tens)<\/p>\n<p>print(&quot;\u4e2a\u4f4d\u6570:&quot;, units)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\u800c\u4e14\u76f4\u89c2\uff0c\u9002\u5408\u5728\u9700\u8981\u9891\u7e41\u8bbf\u95ee\u548c\u64cd\u4f5c\u6570\u5b57\u6bcf\u4e00\u4f4d\u7684\u573a\u666f\u4e0b\u4f7f\u7528\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u6570\u5b57\u8fdb\u884c\u5404\u79cd\u5904\u7406\uff0c\u5982\u6392\u5e8f\u3001\u6c42\u548c\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c<strong>\u4f7f\u7528\u6570\u5b66\u8fd0\u7b97\u3001\u5b57\u7b26\u4e32\u5904\u7406\u548c\u5217\u8868\u89e3\u6790<\/strong>\u662fPython\u4e2d\u53d6\u51fa\u4e00\u4e2a\u56db\u4f4d\u6570\u6bcf\u4e00\u4f4d\u7684\u4e09\u79cd\u5e38\u89c1\u65b9\u6cd5\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u5904\u7406<\/strong>\u662f\u6700\u4e3a\u76f4\u89c2\u548c\u7b80\u4fbf\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u7684\u6570\u5b57\u5904\u7406\u9700\u6c42\u3002\u901a\u8fc7\u5bf9\u8fd9\u4e9b\u65b9\u6cd5\u7684\u638c\u63e1\u548c\u7075\u6d3b\u5e94\u7528\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u64cd\u4f5c\u7684\u7b80\u4fbf\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u4e00\u4e2a\u56db\u4f4d\u6570\u5206\u89e3\u4e3a\u6bcf\u4e00\u4f4d\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u56db\u4f4d\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6765\u8f7b\u677e\u53d6\u51fa\u6bcf\u4e00\u4f4d\u6570\u5b57\u3002\u4f7f\u7528\u5b57\u7b26\u4e32\u7d22\u5f15\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">number = 1234\ndigits = [int(digit) for digit in str(number)]\nprint(digits)  # \u8f93\u51fa: [1, 2, 3, 4]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u660e\u4e86\uff0c\u9002\u5408\u5feb\u901f\u5206\u89e3\u6570\u5b57\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u63d0\u53d6\u56db\u4f4d\u6570\u7684\u5404\u4e2a\u6570\u5b57\uff1f<\/strong><br \/>\u9664\u4e86\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6570\u5b66\u8fd0\u7b97\u6765\u63d0\u53d6\u6bcf\u4e00\u4f4d\u3002\u4f8b\u5982\uff0c\u901a\u8fc7\u53d6\u6a21\u8fd0\u7b97\u548c\u6574\u9664\u8fd0\u7b97\u6765\u83b7\u53d6\u6bcf\u4e00\u4f4d\u6570\u5b57\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">number = 1234\nthousands = number \/\/ 1000\nhundreds = (number \/\/ 100) % 10\ntens = (number \/\/ 10) % 10\nunits = number % 10\nprint(thousands, hundreds, tens, units)  # \u8f93\u51fa: 1 2 3 4\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4f9d\u8d56\u5b57\u7b26\u4e32\u5904\u7406\uff0c\u9002\u5408\u9700\u8981\u8fdb\u884c\u6570\u5b57\u8fd0\u7b97\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u5728\u63d0\u53d6\u6570\u5b57\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u9700\u8981\u6ce8\u610f\uff1f<\/strong><br \/>\u63d0\u53d6\u56db\u4f4d\u6570\u65f6\uff0c\u786e\u4fdd\u8f93\u5165\u786e\u5b9e\u4e3a\u56db\u4f4d\u6570\u5b57\u5f88\u91cd\u8981\u3002\u9519\u8bef\u7684\u8f93\u5165\u53ef\u80fd\u5bfc\u81f4\u8fd0\u884c\u65f6\u9519\u8bef\u6216\u4e0d\u6b63\u786e\u7684\u7ed3\u679c\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u8fdb\u884c\u9a8c\u8bc1\uff1a  <\/p>\n<pre><code class=\"language-python\">number = 1234\nif 1000 &lt;= number &lt;= 9999:\n    # \u63d0\u53d6\u6570\u5b57\u7684\u4ee3\u7801\nelse:\n    print(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u56db\u4f4d\u6570\uff01&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u68c0\u67e5\u53ef\u4ee5\u907f\u514d\u7a0b\u5e8f\u51fa\u73b0\u610f\u5916\u60c5\u51b5\uff0c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u53d6\u51fa\u4e00\u4e2a\u56db\u4f4d\u6570\u7684\u6bcf\u4e00\u4f4d\uff1a 1. \u4f7f\u7528\u6570\u5b66\u8fd0\u7b97\u30012. \u4f7f\u7528\u5b57\u7b26\u4e32\u5904\u7406\u30013. \u4f7f\u7528\u5217\u8868\u89e3\u6790\u3002\u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1147148,"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\/1147141"}],"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=1147141"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1147141\/revisions"}],"predecessor-version":[{"id":1147152,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1147141\/revisions\/1147152"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1147148"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1147141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1147141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1147141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}