{"id":1179832,"date":"2025-01-15T18:29:24","date_gmt":"2025-01-15T10:29:24","guid":{"rendered":""},"modified":"2025-01-15T18:29:28","modified_gmt":"2025-01-15T10:29:28","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%ae%9e%e7%8e%b0%e5%be%aa%e7%8e%af%e8%be%93%e5%87%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1179832.html","title":{"rendered":"\u5982\u4f55\u7528python\u5b9e\u73b0\u5faa\u73af\u8f93\u51fa"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25114142\/445096d4-ba75-4911-89d2-e4cfc3ca04ef.webp\" alt=\"\u5982\u4f55\u7528python\u5b9e\u73b0\u5faa\u73af\u8f93\u51fa\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u5b9e\u73b0\u5faa\u73af\u8f93\u51fa\uff0c\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u9012\u5f52\u7b49\u65b9\u6cd5\u3002<\/strong> \u8fd9\u91cc\u4e3b\u8981\u4ecb\u7ecdfor\u5faa\u73af\u548cwhile\u5faa\u73af\u7684\u7528\u6cd5\u3002<strong>for\u5faa\u73af\u9002\u7528\u4e8e\u5df2\u77e5\u5faa\u73af\u6b21\u6570\u7684\u573a\u666f\uff0cwhile\u5faa\u73af\u9002\u7528\u4e8e\u57fa\u4e8e\u6761\u4ef6\u5224\u65ad\u7684\u573a\u666f\u3002<\/strong> \u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u4e00\u4e0bfor\u5faa\u73af\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001FOR\u5faa\u73af<\/h3>\n<\/p>\n<p><p>For\u5faa\u73af\u7528\u4e8e\u904d\u5386\u4e00\u4e2a\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\uff09\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\u5b83\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u4ece\u5e8f\u5217\u4e2d\u63d0\u53d6\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u6267\u884c\u5faa\u73af\u4f53\u5185\u7684\u4ee3\u7801\u3002\u5178\u578b\u7684for\u5faa\u73af\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528range\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u6574\u6570\u5e8f\u5217<\/p>\n<p>for i in range(5):<\/p>\n<p>    print(f&quot;\u5faa\u73af\u6b21\u6570: {i}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0cfor\u5faa\u73af\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for element in iterable:<\/p>\n<p>    # \u6267\u884c\u7684\u4ee3\u7801\u5757<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>iterable<\/code>\u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u4f8b\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\u3002\u5faa\u73af\u5c06\u4ece<code>iterable<\/code>\u4e2d\u4f9d\u6b21\u53d6\u51fa\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9<code>element<\/code>\uff0c\u7136\u540e\u6267\u884c\u5faa\u73af\u4f53\u5185\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u904d\u5386\u5217\u8868<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<p>for num in numbers:<\/p>\n<p>    print(num)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cfor\u5faa\u73af\u4f9d\u6b21\u4ece\u5217\u8868<code>numbers<\/code>\u4e2d\u53d6\u51fa\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528range\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728\u9700\u8981\u5faa\u73af\u56fa\u5b9a\u6b21\u6570\u65f6\uff0c\u901a\u5e38\u4f7f\u7528<code>range<\/code>\u51fd\u6570\u3002<code>range<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u6574\u6570\u5e8f\u5217\uff0c\u5e38\u7528\u4e8efor\u5faa\u73af\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u904d\u53860\u52304\u7684\u6574\u6570<\/p>\n<p>for i in range(5):<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>range<\/code>\u51fd\u6570\u7684\u5e38\u89c1\u7528\u6cd5\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>range(stop)<\/code>: \u751f\u6210\u4ece0\u5230<code>stop-1<\/code>\u7684\u6574\u6570\u5e8f\u5217\u3002<\/li>\n<li><code>range(start, stop)<\/code>: \u751f\u6210\u4ece<code>start<\/code>\u5230<code>stop-1<\/code>\u7684\u6574\u6570\u5e8f\u5217\u3002<\/li>\n<li><code>range(start, stop, step)<\/code>: \u751f\u6210\u4ece<code>start<\/code>\u5230<code>stop-1<\/code>\uff0c\u6b65\u957f\u4e3a<code>step<\/code>\u7684\u6574\u6570\u5e8f\u5217\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001WHILE\u5faa\u73af<\/h3>\n<\/p>\n<p><p>While\u5faa\u73af\u7528\u4e8e\u5728\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u65f6\u91cd\u590d\u6267\u884c\u4ee3\u7801\u3002\u5b83\u5728\u6bcf\u6b21\u8fed\u4ee3\u524d\u90fd\u4f1a\u68c0\u67e5\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u5219\u6267\u884c\u5faa\u73af\u4f53\u5185\u7684\u4ee3\u7801\uff0c\u5426\u5219\u9000\u51fa\u5faa\u73af\u3002\u5178\u578b\u7684while\u5faa\u73af\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u5faa\u73af\u53d8\u91cf<\/p>\n<p>i = 0<\/p>\n<h2><strong>\u6761\u4ef6\u8868\u8fbe\u5f0f<\/strong><\/h2>\n<p>while i &lt; 5:<\/p>\n<p>    print(f&quot;\u5faa\u73af\u6b21\u6570: {i}&quot;)<\/p>\n<p>    # \u66f4\u65b0\u5faa\u73af\u53d8\u91cf<\/p>\n<p>    i += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0cwhile\u5faa\u73af\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while condition:<\/p>\n<p>    # \u6267\u884c\u7684\u4ee3\u7801\u5757<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>condition<\/code>\u662f\u4e00\u4e2a\u5e03\u5c14\u8868\u8fbe\u5f0f\u3002\u53ea\u8981<code>condition<\/code>\u4e3a\u771f\uff0c\u5faa\u73af\u5c06\u7ee7\u7eed\u6267\u884c\uff1b\u4e00\u65e6<code>condition<\/code>\u4e3a\u5047\uff0c\u5faa\u73af\u5c06\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528while\u5faa\u73af\u6253\u53700\u52304\u7684\u6574\u6570<\/p>\n<p>i = 0<\/p>\n<p>while i &lt; 5:<\/p>\n<p>    print(i)<\/p>\n<p>    i += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cwhile\u5faa\u73af\u4f1a\u7ee7\u7eed\u6267\u884c\uff0c\u76f4\u5230<code>i<\/code>\u4e0d\u518d\u5c0f\u4e8e5\u4e3a\u6b62\u3002<\/p>\n<\/p>\n<p><h4>2. \u9632\u6b62\u65e0\u9650\u5faa\u73af<\/h4>\n<\/p>\n<p><p>while\u5faa\u73af\u5bb9\u6613\u5bfc\u81f4\u65e0\u9650\u5faa\u73af\uff0c\u5373\u6761\u4ef6\u8868\u8fbe\u5f0f\u6c38\u8fdc\u4e3a\u771f\uff0c\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u9000\u51fa\u3002\u4e3a\u4e86\u9632\u6b62\u8fd9\u79cd\u60c5\u51b5\uff0c\u786e\u4fdd\u5728\u5faa\u73af\u4f53\u5185\u6709\u4ee3\u7801\u80fd\u6539\u53d8\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u503c\uff0c\u4f7f\u5176\u6700\u7ec8\u53d8\u4e3a\u5047\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9519\u8bef\u793a\u8303\uff1a\u65e0\u9650\u5faa\u73af<\/p>\n<p>i = 0<\/p>\n<p>while i &lt; 5:<\/p>\n<p>    print(i)<\/p>\n<p>    # \u5fd8\u8bb0\u66f4\u65b0i\uff0c\u5bfc\u81f4\u65e0\u9650\u5faa\u73af<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7531\u4e8e<code>i<\/code>\u6ca1\u6709\u88ab\u66f4\u65b0\uff0c\u6761\u4ef6\u8868\u8fbe\u5f0f<code>i &lt; 5<\/code>\u59cb\u7ec8\u4e3a\u771f\uff0c\u5faa\u73af\u5c06\u6c38\u8fdc\u4e0d\u4f1a\u7ed3\u675f\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6b63\u786e\u793a\u8303\uff1a\u9632\u6b62\u65e0\u9650\u5faa\u73af<\/p>\n<p>i = 0<\/p>\n<p>while i &lt; 5:<\/p>\n<p>    print(i)<\/p>\n<p>    i += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5d4c\u5957\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u662ffor\u5faa\u73af\u8fd8\u662fwhile\u5faa\u73af\uff0c\u90fd\u53ef\u4ee5\u5d4c\u5957\u4f7f\u7528\uff0c\u5373\u5728\u4e00\u4e2a\u5faa\u73af\u4f53\u5185\u518d\u4f7f\u7528\u53e6\u4e00\u4e2a\u5faa\u73af\u3002\u8fd9\u5728\u5904\u7406\u591a\u7ef4\u6570\u636e\u7ed3\u6784\u65f6\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u4e8c\u7ef4\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u904d\u5386\u4e8c\u7ef4\u5217\u8868<\/p>\n<p>matrix = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p>for row in matrix:<\/p>\n<p>    for elem in row:<\/p>\n<p>        print(elem, end=&#39; &#39;)<\/p>\n<p>    print()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5916\u5c42for\u5faa\u73af\u904d\u5386\u4e8c\u7ef4\u5217\u8868\u7684\u6bcf\u4e00\u884c\uff0c\u5185\u5c42for\u5faa\u73af\u904d\u5386\u6bcf\u884c\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u6700\u7ec8\u8f93\u51fa\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5728\u6bcf\u884c\u7ed3\u675f\u540e\u6362\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5faa\u73af\u63a7\u5236\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u5728\u5faa\u73af\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u3001<code>continue<\/code>\u548c<code>else<\/code>\u8bed\u53e5\u6765\u63a7\u5236\u5faa\u73af\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h4>1. break\u8bed\u53e5<\/h4>\n<\/p>\n<p><p><code>break<\/code>\u8bed\u53e5\u7528\u4e8e\u7acb\u5373\u7ec8\u6b62\u5faa\u73af\uff0c\u4e0d\u518d\u6267\u884c\u5faa\u73af\u4f53\u5185\u5269\u4f59\u7684\u4ee3\u7801\uff0c\u4e5f\u4e0d\u518d\u8fdb\u884c\u4e0b\u4e00\u6b21\u8fed\u4ee3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(10):<\/p>\n<p>    if i == 5:<\/p>\n<p>        break<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53<code>i<\/code>\u7b49\u4e8e5\u65f6\uff0c<code>break<\/code>\u8bed\u53e5\u7ec8\u6b62\u5faa\u73af\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a0\u52304\u3002<\/p>\n<\/p>\n<p><h4>2. continue\u8bed\u53e5<\/h4>\n<\/p>\n<p><p><code>continue<\/code>\u8bed\u53e5\u7528\u4e8e\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\u7684\u5269\u4f59\u4ee3\u7801\uff0c\u76f4\u63a5\u8fdb\u5165\u4e0b\u4e00\u6b21\u8fed\u4ee3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(10):<\/p>\n<p>    if i % 2 == 0:<\/p>\n<p>        continue<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53<code>i<\/code>\u4e3a\u5076\u6570\u65f6\uff0c<code>continue<\/code>\u8bed\u53e5\u8df3\u8fc7\u6253\u5370\u64cd\u4f5c\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a1, 3, 5, 7, 9\u3002<\/p>\n<\/p>\n<p><h4>3. else\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>\u5728for\u6216while\u5faa\u73af\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>else<\/code>\u8bed\u53e5\u3002<code>else<\/code>\u8bed\u53e5\u5728\u5faa\u73af\u6b63\u5e38\u7ed3\u675f\u540e\u6267\u884c\uff08\u5373\u6ca1\u6709\u9047\u5230<code>break<\/code>\u8bed\u53e5\u65f6\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(5):<\/p>\n<p>    print(i)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5faa\u73af\u7ed3\u675f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53for\u5faa\u73af\u6b63\u5e38\u7ed3\u675f\u540e\uff0c<code>else<\/code>\u8bed\u53e5\u5c06\u88ab\u6267\u884c\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a0\u52304\u548c\u201c\u5faa\u73af\u7ed3\u675f\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><h4>1. \u8ba1\u7b971\u5230100\u7684\u548c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528for\u5faa\u73af\u8ba1\u7b971\u5230100\u7684\u548c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">total = 0<\/p>\n<p>for i in range(1, 101):<\/p>\n<p>    total += i<\/p>\n<p>print(f&quot;1\u5230100\u7684\u548c\u4e3a: {total}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528while\u5faa\u73af\u8ba1\u7b971\u5230100\u7684\u548c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">total = 0<\/p>\n<p>i = 1<\/p>\n<p>while i &lt;= 100:<\/p>\n<p>    total += i<\/p>\n<p>    i += 1<\/p>\n<p>print(f&quot;1\u5230100\u7684\u548c\u4e3a: {total}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6253\u5370\u4e5d\u4e5d\u4e58\u6cd5\u8868<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5d4c\u5957for\u5faa\u73af\u6253\u5370\u4e5d\u4e5d\u4e58\u6cd5\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(1, 10):<\/p>\n<p>    for j in range(1, i + 1):<\/p>\n<p>        print(f&quot;{i}*{j}={i*j}&quot;, end=&#39; &#39;)<\/p>\n<p>    print()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u67e5\u627e\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528for\u5faa\u73af\u67e5\u627e\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [3, 5, 7, 2, 8, 1, 9, 4]<\/p>\n<p>max_num = numbers[0]<\/p>\n<p>for num in numbers:<\/p>\n<p>    if num &gt; max_num:<\/p>\n<p>        max_num = num<\/p>\n<p>print(f&quot;\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\u4e3a: {max_num}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u9012\u5f52<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5faa\u73af\u5f62\u5f0f\uff0c\u5176\u4e2d\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u3002\u9012\u5f52\u901a\u5e38\u7528\u4e8e\u89e3\u51b3\u5206\u6cbb\u95ee\u9898\uff0c\u5982\u4e8c\u5206\u67e5\u627e\u3001\u5feb\u901f\u6392\u5e8f\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u8ba1\u7b97\u9636\u4e58<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u9012\u5f52\u8ba1\u7b97n\u7684\u9636\u4e58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def factorial(n):<\/p>\n<p>    if n == 0:<\/p>\n<p>        return 1<\/p>\n<p>    else:<\/p>\n<p>        return n * factorial(n - 1)<\/p>\n<p>print(f&quot;5\u7684\u9636\u4e58\u4e3a: {factorial(5)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6590\u6ce2\u90a3\u5951\u6570\u5217<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u9012\u5f52\u8ba1\u7b97\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2cn\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def fibonacci(n):<\/p>\n<p>    if n &lt;= 0:<\/p>\n<p>        return 0<\/p>\n<p>    elif n == 1:<\/p>\n<p>        return 1<\/p>\n<p>    else:<\/p>\n<p>        return fibonacci(n - 1) + fibonacci(n - 2)<\/p>\n<p>print(f&quot;\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2c10\u9879\u4e3a: {fibonacci(10)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001Python\u4e2d\u7684\u9ad8\u7ea7\u5faa\u73af\u7279\u6027<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7279\u6027\uff0c\u4f7f\u5faa\u73af\u66f4\u52a0\u7b80\u6d01\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>1. \u5217\u8868\u63a8\u5bfc\u5f0f<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u7528\u4e8e\u751f\u6210\u5217\u8868\u3002\u5b83\u53ef\u4ee5\u66ff\u4ee3for\u5faa\u73af\u6765\u521b\u5efa\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528for\u5faa\u73af\u751f\u6210\u5217\u8868<\/p>\n<p>squares = []<\/p>\n<p>for x in range(10):<\/p>\n<p>    squares.append(x  2)<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u5217\u8868<\/strong><\/h2>\n<p>squares = [x  2 for x in range(10)]<\/p>\n<p>print(squares)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5b57\u5178\u63a8\u5bfc\u5f0f\u548c\u96c6\u5408\u63a8\u5bfc\u5f0f<\/h4>\n<\/p>\n<p><p>\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\uff0cPython\u8fd8\u652f\u6301\u5b57\u5178\u63a8\u5bfc\u5f0f\u548c\u96c6\u5408\u63a8\u5bfc\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b57\u5178\u63a8\u5bfc\u5f0f<\/p>\n<p>squares_dict = {x: x  2 for x in range(10)}<\/p>\n<p>print(squares_dict)<\/p>\n<h2><strong>\u96c6\u5408\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p>squares_set = {x  2 for x in range(10)}<\/p>\n<p>print(squares_set)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\uff0c\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\uff0c\u4f46\u751f\u6210\u5668\u662f\u60f0\u6027\u6c42\u503c\u7684\uff0c\u53ef\u4ee5\u8282\u7701\u5185\u5b58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/p>\n<p>squares_gen = (x  2 for x in range(10))<\/p>\n<p>for square in squares_gen:<\/p>\n<p>    print(square)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5b9e\u6218\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1. \u6587\u4ef6\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5faa\u73af\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        print(line.strip())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6570\u636e\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5faa\u73af\u5904\u7406\u6570\u636e\uff0c\u4f8b\u5982\u8ba1\u7b97\u5e73\u5747\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [23, 45, 67, 89, 12, 37, 58]<\/p>\n<p>total = 0<\/p>\n<p>for num in data:<\/p>\n<p>    total += num<\/p>\n<p>average = total \/ len(data)<\/p>\n<p>print(f&quot;\u6570\u636e\u7684\u5e73\u5747\u503c\u4e3a: {average}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u56fe\u5f62\u7ed8\u5236<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5faa\u73af\u7ed8\u5236\u7b80\u5355\u7684\u56fe\u5f62\uff0c\u4f8b\u5982\u661f\u53f7\u7ec4\u6210\u7684\u4e09\u89d2\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">height = 5<\/p>\n<p>for i in range(1, height + 1):<\/p>\n<p>    print(&#39;*&#39; * i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5faa\u73af\u7684\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u5faa\u73af\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u6280\u5de7\u6765\u63d0\u9ad8\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h4>1. \u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u5faa\u73af\u4e2d\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\uff0c\u4f8b\u5982\u5c06\u5faa\u73af\u4e0d\u53d8\u7684\u4ee3\u7801\u79fb\u51fa\u5faa\u73af\u4f53\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u63a8\u8350\uff1a\u91cd\u590d\u8ba1\u7b97len(data)<\/p>\n<p>for i in range(len(data)):<\/p>\n<p>    process(data[i])<\/p>\n<h2><strong>\u63a8\u8350\uff1a\u63d0\u524d\u8ba1\u7b97len(data)<\/strong><\/h2>\n<p>data_len = len(data)<\/p>\n<p>for i in range(data_len):<\/p>\n<p>    process(data[i])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u5e93<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Python\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u63a8\u8350\uff1a\u624b\u52a8\u8ba1\u7b97sum<\/p>\n<p>total = 0<\/p>\n<p>for num in data:<\/p>\n<p>    total += num<\/p>\n<h2><strong>\u63a8\u8350\uff1a\u4f7f\u7528\u5185\u7f6esum\u51fd\u6570<\/strong><\/h2>\n<p>total = sum(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5c3d\u91cf\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u901a\u5e38\u6bd4for\u5faa\u73af\u66f4\u9ad8\u6548\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528for\u5faa\u73af\u751f\u6210\u5217\u8868<\/p>\n<p>squares = []<\/p>\n<p>for x in range(10):<\/p>\n<p>    squares.append(x  2)<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u5217\u8868<\/strong><\/h2>\n<p>squares = [x  2 for x in range(10)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5faa\u73af\u662f\u7f16\u7a0b\u4e2d\u6700\u57fa\u7840\u3001\u6700\u5e38\u7528\u7684\u63a7\u5236\u7ed3\u6784\u4e4b\u4e00\uff0c\u638c\u63e1\u5176\u57fa\u672c\u7528\u6cd5\u548c\u9ad8\u7ea7\u7279\u6027\u5bf9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u4e2dfor\u5faa\u73af\u548cwhile\u5faa\u73af\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u4e30\u5bcc\u7684\u793a\u4f8b\u5c55\u793a\u4e86\u5b83\u4eec\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u91cd\u8981\u6027\u3002\u540c\u65f6\uff0c\u8fd8\u4ecb\u7ecd\u4e86\u9012\u5f52\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7b49\u9ad8\u7ea7\u7279\u6027\uff0c\u5e0c\u671b\u8bfb\u8005\u80fd\u591f\u901a\u8fc7\u672c\u6587\u6df1\u5165\u7406\u89e3\u5e76\u7075\u6d3b\u8fd0\u7528Python\u4e2d\u7684\u5faa\u73af\u7ed3\u6784\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u5b9e\u73b0\u5faa\u73af\u8f93\u51fa\u7684\u57fa\u672c\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b9e\u73b0\u5faa\u73af\u8f93\u51fa\u7684\u57fa\u672c\u65b9\u6cd5\u662f\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u6216<code>while<\/code>\u5faa\u73af\u3002<code>for<\/code>\u5faa\u73af\u901a\u5e38\u7528\u4e8e\u904d\u5386\u5217\u8868\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u800c<code>while<\/code>\u5faa\u73af\u5219\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\u91cd\u590d\u6267\u884c\u4ee3\u7801\u5757\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u4e00\u4e2a\u8303\u56f4\u5e76\u6253\u5370\u6bcf\u4e2a\u6570\u5b57\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">for i in range(5):\n    print(i)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa0\u52304\u7684\u6570\u5b57\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u5faa\u73af\u8f93\u51fa\u6280\u5de7\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5faa\u73af\u8f93\u51fa\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u6280\u5de7\u6765\u589e\u5f3a\u53ef\u8bfb\u6027\u548c\u529f\u80fd\u6027\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5feb\u901f\u751f\u6210\u4e00\u4e2a\u5217\u8868\u5e76\u8fdb\u884c\u8f93\u51fa\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">squared_numbers = [x**2 for x in range(10)]\nprint(squared_numbers)\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u7ed3\u5408<code>enumerate()<\/code>\u51fd\u6570\u4f7f\u7528<code>for<\/code>\u5faa\u73af\uff0c\u53ef\u4ee5\u5728\u8f93\u51fa\u65f6\u540c\u65f6\u83b7\u5f97\u7d22\u5f15\u548c\u503c\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5b9e\u73b0\u5faa\u73af\u8f93\u51fa\u65f6\u7684\u6761\u4ef6\u63a7\u5236\uff1f<\/strong><br \/>\u5728\u5faa\u73af\u8f93\u51fa\u8fc7\u7a0b\u4e2d\uff0c\u6761\u4ef6\u63a7\u5236\u5e38\u5e38\u7528\u4e8e\u51b3\u5b9a\u4f55\u65f6\u505c\u6b62\u5faa\u73af\u6216\u8df3\u8fc7\u67d0\u4e9b\u8fed\u4ee3\u3002\u901a\u8fc7<code>break<\/code>\u548c<code>continue<\/code>\u8bed\u53e5\uff0c\u53ef\u4ee5\u7075\u6d3b\u63a7\u5236\u5faa\u73af\u7684\u6267\u884c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>break<\/code>\u53ef\u4ee5\u5728\u627e\u5230\u7279\u5b9a\u6761\u4ef6\u65f6\u9000\u51fa\u5faa\u73af\uff0c\u800c<code>continue<\/code>\u5219\u53ef\u4ee5\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\uff0c\u7ee7\u7eed\u4e0b\u4e00\u6b21\u5faa\u73af\uff1a  <\/p>\n<pre><code class=\"language-python\">for i in range(10):\n    if i == 5:\n        break  # \u5f53i\u7b49\u4e8e5\u65f6\uff0c\u9000\u51fa\u5faa\u73af\n    print(i)\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u5728\u8f93\u51fa0\u52304\u540e\u505c\u6b62\uff0c\u800c\u4e0d\u4f1a\u8f93\u51fa5\u53ca\u4e4b\u540e\u7684\u6570\u5b57\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u5b9e\u73b0\u5faa\u73af\u8f93\u51fa\uff0c\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u9012\u5f52\u7b49\u65b9\u6cd5\u3002 \u8fd9\u91cc\u4e3b\u8981\u4ecb\u7ecdfor\u5faa\u73af\u548cwhi [&hellip;]","protected":false},"author":3,"featured_media":1179845,"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\/1179832"}],"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=1179832"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179832\/revisions"}],"predecessor-version":[{"id":1179846,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179832\/revisions\/1179846"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1179845"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1179832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1179832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1179832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}