{"id":1089073,"date":"2025-01-08T13:50:10","date_gmt":"2025-01-08T05:50:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1089073.html"},"modified":"2025-01-08T13:50:12","modified_gmt":"2025-01-08T05:50:12","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba0%e5%88%b0100%e7%9a%84%e5%a5%87%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1089073.html","title":{"rendered":"python\u5982\u4f55\u8f93\u51fa0\u5230100\u7684\u5947\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202153\/b4cb2c6f-b8de-4c75-b648-4b04c021f6b9.webp\" alt=\"python\u5982\u4f55\u8f93\u51fa0\u5230100\u7684\u5947\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8f93\u51fa0\u5230100\u7684\u5947\u6570\u6709\u591a\u79cd\u65b9\u6cd5<\/strong>\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528for\u5faa\u73af\u3001\u5217\u8868\u89e3\u6790\u548cwhile\u5faa\u73af\u7b49\u3002<strong>\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528for\u5faa\u73af\u548crange\u51fd\u6570<\/strong>\uff0c\u56e0\u4e3a\u5b83\u4eec\u7b80\u6d01\u660e\u4e86\u3001\u6613\u4e8e\u7406\u89e3\u548c\u9ad8\u6548\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528for\u5faa\u73af\u548crange\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528for\u5faa\u73af\u7ed3\u5408range\u51fd\u6570\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002range\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u8303\u56f4\u5185\u7684\u6570\u5b57\u5e8f\u5217\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u6b65\u957f\u4e3a2\u6765\u751f\u6210\u5947\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(1, 101, 2):<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0crange(1, 101, 2)\u4f1a\u751f\u6210\u4e00\u4e2a\u4ece1\u5f00\u59cb\u5230100\u7684\u5947\u6570\u5e8f\u5217\u3002for\u5faa\u73af\u5c06\u904d\u5386\u8fd9\u4e2a\u5e8f\u5217\u5e76\u6253\u5370\u6bcf\u4e00\u4e2a\u5947\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u751f\u6210\u548c\u5904\u7406\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6765\u751f\u62100\u5230100\u7684\u5947\u6570\uff0c\u5e76\u5728\u751f\u6210\u7684\u540c\u65f6\u8f93\u51fa\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">odd_numbers = [i for i in range(1, 101) if i % 2 != 0]<\/p>\n<p>for num in odd_numbers:<\/p>\n<p>    print(num)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u5217\u8868\u89e3\u6790[i for i in range(1, 101) if i % 2 != 0]\u4f1a\u751f\u6210\u4e00\u4e2a\u5305\u542b0\u5230100\u5947\u6570\u7684\u5217\u8868\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5e76\u6253\u5370\u6bcf\u4e00\u4e2a\u5947\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528while\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u867d\u7136for\u5faa\u73af\u548c\u5217\u8868\u89e3\u6790\u662f\u66f4\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u4f46\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528while\u5faa\u73af\u6765\u5b9e\u73b0\u540c\u6837\u7684\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">i = 1<\/p>\n<p>while i &lt;= 100:<\/p>\n<p>    print(i)<\/p>\n<p>    i += 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521d\u59cb\u5316i\u4e3a1\uff0c\u5faa\u73af\u6761\u4ef6\u4e3ai &lt;= 100\uff0c\u6bcf\u6b21\u5faa\u73af\u540e\u5c06i\u589e\u52a02\uff0c\u4ece\u800c\u786e\u4fddi\u59cb\u7ec8\u4e3a\u5947\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u4f7f\u7528yield\u5173\u952e\u5b57\u7684\u7279\u6b8a\u51fd\u6570\uff0c\u5b83\u4eec\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u9010\u4e2a\u751f\u6210\u503c\uff0c\u800c\u4e0d\u9700\u8981\u4e00\u6b21\u6027\u751f\u6210\u6240\u6709\u503c\u3002\u751f\u6210\u5668\u975e\u5e38\u9002\u5408\u5904\u7406\u5927\u6570\u636e\u96c6\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u4f1a\u6d88\u8017\u592a\u591a\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def odd_numbers():<\/p>\n<p>    for i in range(1, 101, 2):<\/p>\n<p>        yield i<\/p>\n<p>for num in odd_numbers():<\/p>\n<p>    print(num)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570odd_numbers\uff0c\u5b83\u4f7f\u7528yield\u5173\u952e\u5b57\u9010\u4e2a\u751f\u6210\u5947\u6570\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u751f\u6210\u5668\u5e76\u6253\u5370\u6bcf\u4e00\u4e2a\u5947\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u4f7f\u7528for\u5faa\u73af\u3001\u5217\u8868\u89e3\u6790\u3001while\u5faa\u73af\u8fd8\u662f\u751f\u6210\u5668\uff0c<strong>\u8f93\u51fa0\u5230100\u7684\u5947\u6570\u5728Python\u4e2d\u90fd\u662f\u975e\u5e38\u7b80\u5355\u548c\u9ad8\u6548\u7684<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u4e2a\u4eba\u504f\u597d\u3002<strong>\u5bf9\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c\u63a8\u8350\u4f7f\u7528for\u5faa\u73af\u548crange\u51fd\u6570\u7684\u7ec4\u5408\uff0c\u56e0\u4e3a\u5b83\u4eec\u7b80\u6d01\u660e\u4e86\u3001\u6613\u4e8e\u7406\u89e3<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u751f\u62100\u5230100\u4e4b\u95f4\u7684\u5947\u6570\u5217\u8868\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u751f\u62100\u5230100\u4e4b\u95f4\u7684\u5947\u6570\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u6bb5\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">odd_numbers = [num for num in range(101) if num % 2 != 0]\nprint(odd_numbers)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5947\u6570\u7684\u5217\u8868\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u8f93\u51fa0\u5230100\u7684\u5947\u6570\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u4f7f\u7528\u5faa\u73af\u7684\u65b9\u5f0f\u53ef\u4ee5\u66f4\u6e05\u6670\u5730\u5c55\u793a\u6bcf\u4e2a\u5947\u6570\u7684\u8f93\u51fa\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">for num in range(101):\n    if num % 2 != 0:\n        print(num)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u9010\u4e2a\u68c0\u67e5\u6bcf\u4e2a\u6570\u5b57\u5e76\u6253\u5370\u5947\u6570\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u8f93\u51fa\u5947\u6570\u65f6\uff0c\u80fd\u5426\u81ea\u5b9a\u4e49\u8f93\u51fa\u683c\u5f0f\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u6253\u5370\u8bed\u53e5\u6765\u6539\u53d8\u8f93\u51fa\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u5728\u540c\u4e00\u884c\u8f93\u51fa\u6240\u6709\u5947\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>end<\/code>\u53c2\u6570\uff1a  <\/p>\n<pre><code class=\"language-python\">for num in range(101):\n    if num % 2 != 0:\n        print(num, end=&#39; &#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u6240\u6709\u5947\u6570\u5c06\u4f1a\u5728\u540c\u4e00\u884c\u4e2d\u4ee5\u7a7a\u683c\u5206\u9694\u8f93\u51fa\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8f93\u51fa0\u5230100\u7684\u5947\u6570\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528for\u5faa\u73af\u3001\u5217\u8868\u89e3\u6790\u548cwhile\u5faa\u73af\u7b49\u3002\u6700\u5e38\u89c1\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1089077,"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\/1089073"}],"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=1089073"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089073\/revisions"}],"predecessor-version":[{"id":1089078,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089073\/revisions\/1089078"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1089077"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1089073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1089073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1089073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}