{"id":1108818,"date":"2025-01-08T17:04:00","date_gmt":"2025-01-08T09:04:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1108818.html"},"modified":"2025-01-08T17:04:03","modified_gmt":"2025-01-08T09:04:03","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e7%bb%99%e4%b8%80%e4%b8%aa%e5%8f%98%e9%87%8f%e8%b5%8b%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1108818.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u7ed9\u4e00\u4e2a\u53d8\u91cf\u8d4b\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072306\/a4fba23b-73dd-45d9-a234-57a22a517005.webp\" alt=\"python\u4e2d\u5982\u4f55\u7ed9\u4e00\u4e2a\u53d8\u91cf\u8d4b\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u7ed9\u53d8\u91cf\u8d4b\u503c\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528\u7b49\u53f7<code>=<\/code>\u7b26\u53f7\u3001\u4f7f\u7528\u591a\u91cd\u8d4b\u503c\u3001\u4f7f\u7528\u89e3\u5305\u8d4b\u503c\u3001\u4f7f\u7528\u589e\u91cf\u8d4b\u503c\u3002\u4f7f\u7528\u7b49\u53f7<code>=<\/code>\u7b26\u53f7\u662f\u6700\u57fa\u672c\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u5728\u8be6\u7ec6\u63cf\u8ff0\u4e4b\u524d\uff0c\u5148\u4e86\u89e3\u4e00\u4e0bPython\u4e2d\u7684\u53d8\u91cf\u3002<\/strong> \u53d8\u91cf\u662f\u7528\u6765\u5b58\u50a8\u6570\u636e\u7684\u5bb9\u5668\uff0c\u5f53\u6211\u4eec\u5728\u7f16\u7a0b\u65f6\uff0c\u9700\u8981\u5b58\u50a8\u548c\u5904\u7406\u6570\u636e\uff0c\u6211\u4eec\u5c31\u4f1a\u4f7f\u7528\u53d8\u91cf\u3002\u53d8\u91cf\u7684\u8d4b\u503c\u5728Python\u4e2d\u662f\u52a8\u6001\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u4e0d\u9700\u8981\u58f0\u660e\u53d8\u91cf\u7684\u7c7b\u578b\uff0cPython\u4f1a\u6839\u636e\u4f60\u8d4b\u4e88\u7684\u503c\u81ea\u52a8\u63a8\u65ad\u53d8\u91cf\u7684\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u7b49\u53f7<code>=<\/code>\u7b26\u53f7<\/h3>\n<\/p>\n<p><p>\u8fd9\u662f\u6700\u57fa\u7840\u4e5f\u662f\u6700\u5e38\u89c1\u7684\u8d4b\u503c\u65b9\u5f0f\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u503c\u8d4b\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u5982\u4e0b\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>name = &quot;John&quot;<\/p>\n<p>pi = 3.14159<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>10<\/code>\u3001<code>&quot;John&quot;<\/code>\u548c<code>3.14159<\/code>\u5206\u522b\u88ab\u8d4b\u503c\u7ed9\u53d8\u91cf<code>x<\/code>\u3001<code>name<\/code>\u548c<code>pi<\/code>\u3002Python\u4f1a\u6839\u636e\u53f3\u4fa7\u7684\u503c\u81ea\u52a8\u63a8\u65ad\u51fa\u53d8\u91cf\u7684\u7c7b\u578b\uff0c<code>x<\/code>\u662f\u6574\u6570\uff0c<code>name<\/code>\u662f\u5b57\u7b26\u4e32\uff0c<code>pi<\/code>\u662f\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u7684\u7279\u70b9\u662f\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5927\u90e8\u5206\u57fa\u672c\u7684\u8d4b\u503c\u64cd\u4f5c\u3002<strong>\u4f18\u70b9\u5728\u4e8e\u4ee3\u7801\u6e05\u6670\u6613\u61c2\uff0c\u7f3a\u70b9\u662f\u5982\u679c\u8981\u8d4b\u503c\u591a\u4e2a\u53d8\u91cf\u9700\u8981\u91cd\u590d\u5199\u4ee3\u7801\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u591a\u91cd\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u591a\u91cd\u8d4b\u503c\u6307\u7684\u662f\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u540c\u65f6\u7ed9\u591a\u4e2a\u53d8\u91cf\u8d4b\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a, b, c = 1, 2, 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>a<\/code>\u3001<code>b<\/code>\u548c<code>c<\/code>\u5206\u522b\u88ab\u8d4b\u503c\u4e3a<code>1<\/code>\u3001<code>2<\/code>\u548c<code>3<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u7b80\u6d01\u660e\u4e86\uff0c\u53ef\u4ee5\u907f\u514d\u591a\u884c\u4ee3\u7801\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7b80\u6d01\u6027\u3002<\/p>\n<\/p>\n<p><p>\u591a\u91cd\u8d4b\u503c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u4ea4\u6362\u53d8\u91cf\u7684\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a, b = b, a<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u65f6\u975e\u5e38\u65b9\u4fbf\uff0c\u4e0d\u9700\u8981\u4e34\u65f6\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u89e3\u5305\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u89e3\u5305\u8d4b\u503c\u4e5f\u79f0\u4e3a\u5e8f\u5217\u89e3\u5305\u3002\u5b83\u5141\u8bb8\u4f60\u5c06\u4e00\u4e2a\u5e8f\u5217\uff08\u4f8b\u5982\u5217\u8868\u6216\u5143\u7ec4\uff09\u4e2d\u7684\u5143\u7d20\u89e3\u5305\u5230\u591a\u4e2a\u53d8\u91cf\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3]<\/p>\n<p>a, b, c = numbers<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u5217\u8868<code>numbers<\/code>\u4e2d\u7684\u5143\u7d20<code>1<\/code>\u3001<code>2<\/code>\u548c<code>3<\/code>\u5206\u522b\u88ab\u89e3\u5305\u8d4b\u503c\u7ed9\u53d8\u91cf<code>a<\/code>\u3001<code>b<\/code>\u548c<code>c<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9002\u7528\u4e8e\u5c06\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u5206\u522b\u8d4b\u503c\u7ed9\u4e0d\u540c\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u589e\u91cf\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u589e\u91cf\u8d4b\u503c\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8d4b\u503c\u65b9\u5f0f\uff0c\u5b83\u5c06\u8fd0\u7b97\u7b26\u4e0e\u8d4b\u503c\u64cd\u4f5c\u7ed3\u5408\u5728\u4e00\u8d77\uff0c\u4f8b\u5982<code>+=<\/code>\u3001<code>-=<\/code>\u3001<code>*=<\/code>\u3001<code>\/=<\/code>\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 5<\/p>\n<p>x += 3  # \u76f8\u5f53\u4e8e x = x + 3<\/p>\n<p>x *= 2  # \u76f8\u5f53\u4e8e x = x * 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>x<\/code>\u5148\u88ab\u8d4b\u503c\u4e3a<code>5<\/code>\uff0c\u7136\u540e\u901a\u8fc7\u589e\u91cf\u8d4b\u503c<code>+=<\/code>\u548c<code>*=<\/code>\u8fdb\u884c\u4fee\u6539\u3002\u8fd9\u79cd\u65b9\u5f0f\u7b80\u6d01\u4e14\u9ad8\u6548\uff0c\u5e38\u7528\u4e8e\u5faa\u73af\u548c\u7d2f\u79ef\u64cd\u4f5c\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u65b9\u6cd5\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u901a\u8fc7\u5185\u7f6e\u51fd\u6570\u6216\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u7ed9\u53d8\u91cf\u8d4b\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = input(&quot;Enter your name: &quot;)<\/p>\n<p>length = len(name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>input<\/code>\u51fd\u6570\u7684\u8fd4\u56de\u503c\u88ab\u8d4b\u503c\u7ed9\u53d8\u91cf<code>name<\/code>\uff0c<code>len<\/code>\u51fd\u6570\u7684\u8fd4\u56de\u503c\u88ab\u8d4b\u503c\u7ed9\u53d8\u91cf<code>length<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u6761\u4ef6\u8868\u8fbe\u5f0f\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u8fd8\u652f\u6301\u4f7f\u7528\u6761\u4ef6\u8868\u8fbe\u5f0f\u8fdb\u884c\u8d4b\u503c\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>y = 20<\/p>\n<p>max_value = x if x &gt; y else y<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u53d8\u91cf<code>max_value<\/code>\u88ab\u8d4b\u503c\u4e3a<code>x<\/code>\u548c<code>y<\/code>\u4e2d\u7684\u8f83\u5927\u503c\u3002\u8fd9\u79cd\u65b9\u5f0f\u7b80\u6d01\u4e14\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6761\u4ef6\u5224\u65ad\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u51fd\u6570\u8fd4\u56de\u503c\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u5b9a\u4e49\u51fd\u6570\u5e76\u901a\u8fc7\u51fd\u6570\u8fd4\u56de\u503c\u7ed9\u53d8\u91cf\u8d4b\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>result = add(5, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u51fd\u6570<code>add<\/code>\u7684\u8fd4\u56de\u503c\u88ab\u8d4b\u503c\u7ed9\u53d8\u91cf<code>result<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u5728\u6a21\u5757\u5316\u7f16\u7a0b\u4e2d\u975e\u5e38\u5e38\u89c1\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u7684\u5c5e\u6027\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u7ed9\u7c7b\u7684\u5c5e\u6027\u8d4b\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>person = Person(&quot;Alice&quot;, 30)<\/p>\n<p>person.name = &quot;Bob&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>Person<\/code>\u7c7b\u7684<code>name<\/code>\u548c<code>age<\/code>\u5c5e\u6027\u5206\u522b\u88ab\u8d4b\u503c\u3002\u5bf9\u8c61<code>person<\/code>\u7684<code>name<\/code>\u5c5e\u6027\u88ab\u91cd\u65b0\u8d4b\u503c\u4e3a<code>&quot;Bob&quot;<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u5b57\u5178\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u4e00\u79cd\u975e\u5e38\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u952e\u503c\u5bf9\u7684\u65b9\u5f0f\u7ed9\u5b57\u5178\u8d4b\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">person = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30}<\/p>\n<p>person[&quot;name&quot;] = &quot;Bob&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u5b57\u5178<code>person<\/code>\u7684<code>name<\/code>\u952e\u88ab\u91cd\u65b0\u8d4b\u503c\u4e3a<code>&quot;Bob&quot;<\/code>\u3002\u5b57\u5178\u8d4b\u503c\u7684\u7279\u70b9\u662f\u7075\u6d3b\u4e14\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5b58\u50a8\u548c\u64cd\u4f5c\u952e\u503c\u5bf9\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u96c6\u5408\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u662f\u4e00\u79cd\u65e0\u5e8f\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u65b9\u6cd5\u7ed9\u96c6\u5408\u8d4b\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = {1, 2, 3}<\/p>\n<p>numbers.add(4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u96c6\u5408<code>numbers<\/code>\u4e2d\u6dfb\u52a0\u4e86\u5143\u7d20<code>4<\/code>\u3002\u96c6\u5408\u8d4b\u503c\u7684\u7279\u70b9\u662f\u65e0\u5e8f\u4e14\u552f\u4e00\uff0c\u9002\u7528\u4e8e\u5904\u7406\u4e0d\u91cd\u590d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed9\u53d8\u91cf\u8d4b\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares = [x2 for x in range(10)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u53d8\u91cf<code>squares<\/code>\u88ab\u8d4b\u503c\u4e3a\u4e00\u4e2a\u5305\u542b<code>0<\/code>\u5230<code>9<\/code>\u7684\u5e73\u65b9\u6570\u7684\u5217\u8868\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u7279\u70b9\u662f\u7b80\u6d01\u4e14\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u751f\u6210\u5217\u8868\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\uff0c\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u521b\u5efa\u5b57\u5178\u7684\u65b9\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares = {x: x2 for x in range(10)}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u53d8\u91cf<code>squares<\/code>\u88ab\u8d4b\u503c\u4e3a\u4e00\u4e2a\u5305\u542b<code>0<\/code>\u5230<code>9<\/code>\u7684\u5e73\u65b9\u6570\u7684\u5b57\u5178\u3002\u5b57\u5178\u63a8\u5bfc\u5f0f\u7684\u7279\u70b9\u662f\u7b80\u6d01\u4e14\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u751f\u6210\u5b57\u5178\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u521b\u5efa\u96c6\u5408\u7684\u65b9\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares = {x2 for x in range(10)}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u53d8\u91cf<code>squares<\/code>\u88ab\u8d4b\u503c\u4e3a\u4e00\u4e2a\u5305\u542b<code>0<\/code>\u5230<code>9<\/code>\u7684\u5e73\u65b9\u6570\u7684\u96c6\u5408\u3002\u96c6\u5408\u63a8\u5bfc\u5f0f\u7684\u7279\u70b9\u662f\u7b80\u6d01\u4e14\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u751f\u6210\u96c6\u5408\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u521b\u5efa\u751f\u6210\u5668\u7684\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7ed9\u53d8\u91cf\u8d4b\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares = (x2 for x in range(10))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u53d8\u91cf<code>squares<\/code>\u88ab\u8d4b\u503c\u4e3a\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\u3002\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7684\u7279\u70b9\u662f\u60f0\u6027\u6c42\u503c\u4e14\u8282\u7701\u5185\u5b58\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u548c\u5c40\u90e8\u53d8\u91cf\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u548c\u5c40\u90e8\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10  # \u5168\u5c40\u53d8\u91cf<\/p>\n<p>def func():<\/p>\n<p>    x = 20  # \u5c40\u90e8\u53d8\u91cf<\/p>\n<p>    print(x)<\/p>\n<p>func()<\/p>\n<p>print(x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u53d8\u91cf<code>x<\/code>\u5728\u51fd\u6570\u5185\u90e8\u88ab\u91cd\u65b0\u8d4b\u503c\u4e3a\u5c40\u90e8\u53d8\u91cf<code>20<\/code>\uff0c\u5168\u5c40\u53d8\u91cf<code>x<\/code>\u7684\u503c\u4fdd\u6301\u4e0d\u53d8\u3002\u5168\u5c40\u53d8\u91cf\u548c\u5c40\u90e8\u53d8\u91cf\u8d4b\u503c\u7684\u7279\u70b9\u662f\u4f5c\u7528\u57df\u4e0d\u540c\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u7f16\u7a0b\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u4f7f\u7528\u5d4c\u5957\u6570\u636e\u7ed3\u6784\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u4f7f\u7528\u5d4c\u5957\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u8d4b\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {<\/p>\n<p>    &quot;name&quot;: &quot;Alice&quot;,<\/p>\n<p>    &quot;info&quot;: {<\/p>\n<p>        &quot;age&quot;: 30,<\/p>\n<p>        &quot;city&quot;: &quot;New York&quot;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>data[&quot;info&quot;][&quot;age&quot;] = 31<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u5d4c\u5957\u5b57\u5178<code>data<\/code>\u7684<code>info<\/code>\u952e\u7684<code>age<\/code>\u952e\u88ab\u91cd\u65b0\u8d4b\u503c\u4e3a<code>31<\/code>\u3002\u5d4c\u5957\u6570\u636e\u7ed3\u6784\u8d4b\u503c\u7684\u7279\u70b9\u662f\u7ed3\u6784\u590d\u6742\u4e14\u7075\u6d3b\uff0c\u9002\u7528\u4e8e\u590d\u6742\u6570\u636e\u7684\u5b58\u50a8\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e03\u3001\u4f7f\u7528\u6a21\u5757\u548c\u5305\u4e2d\u7684\u53d8\u91cf\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528\u6a21\u5757\u548c\u5305\u8fdb\u884c\u4ee3\u7801\u7ec4\u7ec7\u548c\u7ba1\u7406\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module1.py<\/p>\n<p>x = 10<\/p>\n<h2><strong>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/strong><\/h2>\n<p>import module1<\/p>\n<p>print(module1.x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u53d8\u91cf<code>x<\/code>\u5728<code>module1<\/code>\u6a21\u5757\u4e2d\u88ab\u8d4b\u503c\uff0c\u5e76\u5728<code>main<\/code>\u6a21\u5757\u4e2d\u88ab\u8bbf\u95ee\u3002\u6a21\u5757\u548c\u5305\u4e2d\u7684\u53d8\u91cf\u8d4b\u503c\u7684\u7279\u70b9\u662f\u7ec4\u7ec7\u7ed3\u6784\u6e05\u6670\uff0c\u9002\u7528\u4e8e\u5927\u578b\u9879\u76ee\u7684\u4ee3\u7801\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u516b\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u662f\u4e00\u79cd\u7528\u4e8e<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\u7684\u673a\u5236\uff0c\u4f8b\u5982\u6587\u4ef6\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6587\u4ef6\u5bf9\u8c61<code>file<\/code>\u5728\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4e2d\u88ab\u8d4b\u503c\uff0c\u5e76\u5728\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7ed3\u675f\u65f6\u81ea\u52a8\u5173\u95ed\u3002\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u8d4b\u503c\u7684\u7279\u70b9\u662f\u8d44\u6e90\u7ba1\u7406\u65b9\u4fbf\u4e14\u5b89\u5168\uff0c\u9002\u7528\u4e8e\u6587\u4ef6\u64cd\u4f5c\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e5d\u3001\u4f7f\u7528\u88c5\u9970\u5668\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u7528\u4e8e\u4fee\u6539\u51fd\u6570\u6216\u65b9\u6cd5\u884c\u4e3a\u7684\u673a\u5236\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def decorator(func):<\/p>\n<p>    def wrapper():<\/p>\n<p>        print(&quot;Before function&quot;)<\/p>\n<p>        func()<\/p>\n<p>        print(&quot;After function&quot;)<\/p>\n<p>    return wrapper<\/p>\n<p>@decorator<\/p>\n<p>def greet():<\/p>\n<p>    print(&quot;Hello, World!&quot;)<\/p>\n<p>greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>greet<\/code>\u51fd\u6570\u88ab<code>decorator<\/code>\u88c5\u9970\u5668\u88c5\u9970\uff0c\u88c5\u9970\u5668\u51fd\u6570<code>decorator<\/code>\u4e2d\u7684<code>wrapper<\/code>\u51fd\u6570\u88ab\u8d4b\u503c\u7ed9<code>greet<\/code>\u3002\u88c5\u9970\u5668\u8d4b\u503c\u7684\u7279\u70b9\u662f\u7075\u6d3b\u4e14\u5f3a\u5927\uff0c\u9002\u7528\u4e8e\u4fee\u6539\u51fd\u6570\u6216\u65b9\u6cd5\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u5341\u3001\u4f7f\u7528\u5143\u7c7b\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5143\u7c7b\u662f\u4e00\u79cd\u7528\u4e8e\u521b\u5efa\u7c7b\u7684\u7c7b\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Meta(type):<\/p>\n<p>    def __new__(cls, name, bases, dct):<\/p>\n<p>        dct[&quot;class_attr&quot;] = &quot;value&quot;<\/p>\n<p>        return super().__new__(cls, name, bases, dct)<\/p>\n<p>class MyClass(metaclass=Meta):<\/p>\n<p>    pass<\/p>\n<p>print(MyClass.class_attr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>MyClass<\/code>\u7c7b\u7684<code>class_attr<\/code>\u5c5e\u6027\u5728\u5143\u7c7b<code>Meta<\/code>\u4e2d\u88ab\u8d4b\u503c\u3002\u5143\u7c7b\u8d4b\u503c\u7684\u7279\u70b9\u662f\u9002\u7528\u4e8e\u7c7b\u7684\u521b\u5efa\u548c\u4fee\u6539\uff0c\u9002\u7528\u4e8e\u9ad8\u7ea7\u7f16\u7a0b\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53d8\u91cf\u8d4b\u503c\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5305\u62ec\u4f7f\u7528\u7b49\u53f7<code>=<\/code>\u7b26\u53f7\u3001\u4f7f\u7528\u591a\u91cd\u8d4b\u503c\u3001\u4f7f\u7528\u89e3\u5305\u8d4b\u503c\u3001\u4f7f\u7528\u589e\u91cf\u8d4b\u503c\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u65b9\u6cd5\u8d4b\u503c\u3001\u4f7f\u7528\u6761\u4ef6\u8868\u8fbe\u5f0f\u8d4b\u503c\u3001\u4f7f\u7528\u51fd\u6570\u8fd4\u56de\u503c\u8d4b\u503c\u3001\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u7684\u5c5e\u6027\u8d4b\u503c\u3001\u4f7f\u7528\u5b57\u5178\u8d4b\u503c\u3001\u4f7f\u7528\u96c6\u5408\u8d4b\u503c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8d4b\u503c\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u8d4b\u503c\u3001\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u8d4b\u503c\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u8d4b\u503c\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u548c\u5c40\u90e8\u53d8\u91cf\u8d4b\u503c\u3001\u4f7f\u7528\u5d4c\u5957\u6570\u636e\u7ed3\u6784\u8d4b\u503c\u3001\u4f7f\u7528\u6a21\u5757\u548c\u5305\u4e2d\u7684\u53d8\u91cf\u8d4b\u503c\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u8d4b\u503c\u3001\u4f7f\u7528\u88c5\u9970\u5668\u8d4b\u503c\u3001\u4f7f\u7528\u5143\u7c7b\u8d4b\u503c\u7b49\u3002\u6bcf\u79cd\u8d4b\u503c\u65b9\u6cd5\u90fd\u6709\u5176\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u7ed9\u53d8\u91cf\u8d4b\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8d4b\u503c\u53d8\u91cf\u7684\u8fc7\u7a0b\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ea\u9700\u4f7f\u7528\u201c=\u201d\u7b26\u53f7\u5c06\u4e00\u4e2a\u503c\u5206\u914d\u7ed9\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u6570\u5b5710\u8d4b\u503c\u7ed9\u53d8\u91cf<code>x<\/code>\uff1a<code>x = 10<\/code>\u3002\u8fd9\u884c\u4ee3\u7801\u4f1a\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>x<\/code>\u7684\u53d8\u91cf\uff0c\u5e76\u5c06\u5176\u503c\u8bbe\u7f6e\u4e3a10\u3002\u8d4b\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u3001\u5217\u8868\u7b49\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u5728Python\u4e2d\u7ed9\u53d8\u91cf\u8d4b\u4e88\u591a\u4e2a\u503c\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u5141\u8bb8\u60a8\u4f7f\u7528\u5143\u7ec4\u89e3\u5305\u7684\u65b9\u5f0f\u5728\u4e00\u884c\u4e2d\u4e3a\u591a\u4e2a\u53d8\u91cf\u540c\u65f6\u8d4b\u503c\u3002\u4f8b\u5982\uff1a<code>a, b, c = 1, 2, 3<\/code>\u3002\u8fd9\u6837\uff0c\u53d8\u91cf<code>a<\/code>\u5c06\u88ab\u8d4b\u503c\u4e3a1\uff0c<code>b<\/code>\u4e3a2\uff0c<code>c<\/code>\u4e3a3\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u5c06\u76f8\u540c\u7684\u503c\u8d4b\u7ed9\u591a\u4e2a\u53d8\u91cf\uff0c\u4f8b\u5982<code>x = y = z = 0<\/code>\uff0c\u8fd9\u4f1a\u5c060\u8d4b\u503c\u7ed9<code>x<\/code>\u3001<code>y<\/code>\u548c<code>z<\/code>\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u66f4\u6539\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u8be5\u600e\u4e48\u505a\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u66f4\u6539\u53d8\u91cf\u7684\u503c\uff0c\u53ea\u9700\u518d\u6b21\u4f7f\u7528\u8d4b\u503c\u8bed\u53e5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u60f3\u5c06\u53d8\u91cf<code>x<\/code>\u7684\u503c\u4ece10\u66f4\u6539\u4e3a20\uff0c\u53ea\u9700\u6267\u884c<code>x = 20<\/code>\u5373\u53ef\u3002\u8fd9\u5c06\u8986\u76d6\u539f\u6709\u7684\u503c\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u8fd0\u7b97\u7b26\u6765\u66f4\u65b0\u53d8\u91cf\uff0c\u4f8b\u5982<code>x += 5<\/code>\u4f1a\u5c06<code>x<\/code>\u7684\u503c\u589e\u52a05\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u7ed9\u53d8\u91cf\u8d4b\u503c\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528\u7b49\u53f7=\u7b26\u53f7\u3001\u4f7f\u7528\u591a\u91cd\u8d4b\u503c\u3001\u4f7f\u7528\u89e3\u5305\u8d4b\u503c\u3001\u4f7f\u7528\u589e\u91cf\u8d4b\u503c\u3002\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1108830,"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\/1108818"}],"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=1108818"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108818\/revisions"}],"predecessor-version":[{"id":1108834,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108818\/revisions\/1108834"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1108830"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1108818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1108818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1108818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}