{"id":1170459,"date":"2025-01-15T16:22:46","date_gmt":"2025-01-15T08:22:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1170459.html"},"modified":"2025-01-15T16:22:48","modified_gmt":"2025-01-15T08:22:48","slug":"%e5%9c%a8python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1170459.html","title":{"rendered":"\u5728python\u4e2d\u5982\u4f55\u5b9a\u4e49\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26071140\/c924086c-35c6-4872-921d-06450b8bacc8.webp\" alt=\"\u5728python\u4e2d\u5982\u4f55\u5b9a\u4e49\u53d8\u91cf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u53d8\u91cf\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u7b26\u201c=\u201d\u5c06\u4e00\u4e2a\u503c\u8d4b\u7ed9\u53d8\u91cf\u5373\u53ef\u3002\u4f8b\u5982\uff1ax = 5, name = &#39;Alice&#39;, is_active = True<\/strong>\u3002\u5176\u4e2d\uff0c\u53d8\u91cf\u540d\u53ef\u4ee5\u7531\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\u7ec4\u6210\uff0c\u4f46\u4e0d\u80fd\u4ee5\u6570\u5b57\u5f00\u5934\u3002\u5728Python\u4e2d\uff0c\u53d8\u91cf\u662f\u52a8\u6001\u7c7b\u578b\u7684\uff0c\u8fd9\u610f\u5473\u7740\u53d8\u91cf\u53ef\u4ee5\u968f\u65f6\u66f4\u6539\u5176\u503c\u548c\u7c7b\u578b\u3002\u8981\u6ce8\u610f\u7684\u662f\uff0c\u53d8\u91cf\u540d\u533a\u5206\u5927\u5c0f\u5199\uff0c\u6240\u4ee5\u201cvar\u201d\u548c\u201cVar\u201d\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u53d8\u91cf\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u4e2d\u5b9a\u4e49\u53d8\u91cf\u7684\u5404\u79cd\u65b9\u6cd5\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u57fa\u7840\u53d8\u91cf\u5b9a\u4e49<\/h3>\n<\/p>\n<p><h4>1. \u6570\u5b57\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6570\u5b57\u7c7b\u578b\u4e3b\u8981\u5305\u62ec\u6574\u6570\uff08int\uff09\u3001\u6d6e\u70b9\u6570\uff08float\uff09\u548c\u590d\u6570\uff08complex\uff09\u3002\u5b9a\u4e49\u8fd9\u4e9b\u53d8\u91cf\u65f6\uff0c\u76f4\u63a5\u8d4b\u503c\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6574\u6570<\/p>\n<p>x = 10<\/p>\n<h2><strong>\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>y = 20.5<\/p>\n<h2><strong>\u590d\u6570<\/strong><\/h2>\n<p>z = 1 + 2j<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u662f\u7531\u5f15\u53f7\u62ec\u8d77\u6765\u7684\u4e00\u7cfb\u5217\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u5355\u5f15\u53f7\u3001\u53cc\u5f15\u53f7\u6216\u4e09\u5f15\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5355\u5f15\u53f7\u5b9a\u4e49\u5b57\u7b26\u4e32<\/p>\n<p>name = &#39;Alice&#39;<\/p>\n<h2><strong>\u4f7f\u7528\u53cc\u5f15\u53f7\u5b9a\u4e49\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>greeting = &quot;Hello, World!&quot;<\/p>\n<h2><strong>\u4f7f\u7528\u4e09\u5f15\u53f7\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>message = &quot;&quot;&quot;This is a<\/p>\n<p>multiline string&quot;&quot;&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5e03\u5c14\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>\u5e03\u5c14\u7c7b\u578b\u53d8\u91cf\u53ea\u6709\u4e24\u4e2a\u503c\uff1aTrue\u548cFalse\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">is_active = True<\/p>\n<p>is_deleted = False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u53d8\u91cf\u547d\u540d\u89c4\u5219<\/h3>\n<\/p>\n<p><h4>1. \u5408\u6cd5\u6807\u8bc6\u7b26<\/h4>\n<\/p>\n<p><p>\u53d8\u91cf\u540d\u5fc5\u987b\u4ee5\u5b57\u6bcd\uff08a-z, A-Z\uff09\u6216\u4e0b\u5212\u7ebf\uff08_\uff09\u5f00\u5934\uff0c\u540e\u7eed\u5b57\u7b26\u53ef\u4ee5\u662f\u5b57\u6bcd\u3001\u6570\u5b57\uff080-9\uff09\u6216\u4e0b\u5212\u7ebf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">valid_name = &quot;John&quot;<\/p>\n<p>_valid_name = &quot;Jane&quot;<\/p>\n<p>valid_name2 = &quot;Doe&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u533a\u5206\u5927\u5c0f\u5199<\/h4>\n<\/p>\n<p><p>Python\u533a\u5206\u5927\u5c0f\u5199\uff0c\u6240\u4ee5<code>Variable<\/code>\u548c<code>variable<\/code>\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">Variable = 10<\/p>\n<p>variable = 20<\/p>\n<p>print(Variable)  # \u8f93\u51fa10<\/p>\n<p>print(variable)  # \u8f93\u51fa20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u907f\u514d\u4fdd\u7559\u5b57<\/h4>\n<\/p>\n<p><p>\u4e0d\u8981\u4f7f\u7528Python\u7684\u4fdd\u7559\u5b57\u4f5c\u4e3a\u53d8\u91cf\u540d\u3002\u8fd9\u4e9b\u4fdd\u7559\u5b57\u5728Python\u4e2d\u6709\u7279\u6b8a\u7684\u542b\u4e49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9519\u8bef\u793a\u4f8b<\/p>\n<p>if = 10  # SyntaxError: invalid syntax<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>keyword<\/code>\u6a21\u5757\u67e5\u770b\u6240\u6709\u4fdd\u7559\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import keyword<\/p>\n<p>print(keyword.kwlist)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u52a8\u6001\u7c7b\u578b\u548c\u7c7b\u578b\u8f6c\u6362<\/h3>\n<\/p>\n<p><h4>1. \u52a8\u6001\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>Python\u662f\u52a8\u6001\u7c7b\u578b\u8bed\u8a00\uff0c\u8fd9\u610f\u5473\u7740\u53d8\u91cf\u53ef\u4ee5\u968f\u65f6\u66f4\u6539\u5176\u503c\u548c\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">var = 10<\/p>\n<p>print(var)  # \u8f93\u51fa10<\/p>\n<p>var = &quot;Hello&quot;<\/p>\n<p>print(var)  # \u8f93\u51faHello<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7c7b\u578b\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff0c\u5982<code>int()<\/code>\u3001<code>float()<\/code>\u3001<code>str()<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = &quot;123&quot;<\/p>\n<p>y = int(x)  # \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/p>\n<p>print(y)  # \u8f93\u51fa123<\/p>\n<p>z = float(y)  # \u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570<\/p>\n<p>print(z)  # \u8f93\u51fa123.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5e38\u91cf\u7684\u5b9a\u4e49<\/h3>\n<\/p>\n<p><p>Python\u6ca1\u6709\u5185\u7f6e\u7684\u5e38\u91cf\u7c7b\u578b\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u547d\u540d\u7ea6\u5b9a\u6765\u5b9a\u4e49\u5e38\u91cf\uff0c\u5373\u4f7f\u7528\u5168\u5927\u5199\u5b57\u6bcd\u547d\u540d\u53d8\u91cf\uff0c\u8868\u660e\u5176\u503c\u4e0d\u5e94\u6539\u53d8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">PI = 3.14159<\/p>\n<p>GRAVITY = 9.8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136\u8fd9\u5e76\u4e0d\u80fd\u771f\u6b63\u9632\u6b62\u4fee\u6539\u8fd9\u4e9b\u53d8\u91cf\uff0c\u4f46\u901a\u8fc7\u7ea6\u5b9a\uff0c\u5f00\u53d1\u8005\u5e94\u5f53\u907f\u514d\u66f4\u6539\u8fd9\u4e9b\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u53d8\u91cf\u7684\u4f5c\u7528\u57df<\/h3>\n<\/p>\n<p><h4>1. \u5c40\u90e8\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5c40\u90e8\u53d8\u91cf\u662f\u5728\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u53ea\u80fd\u5728\u8be5\u51fd\u6570\u5185\u90e8\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_function():<\/p>\n<p>    local_var = 10<\/p>\n<p>    print(local_var)<\/p>\n<p>my_function()  # \u8f93\u51fa10<\/p>\n<h2><strong>print(local_var)  # NameError: name &#39;local_var&#39; is not defined<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5168\u5c40\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5168\u5c40\u53d8\u91cf\u662f\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u6574\u4e2a\u811a\u672c\u4e2d\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">global_var = 20<\/p>\n<p>def another_function():<\/p>\n<p>    print(global_var)<\/p>\n<p>another_function()  # \u8f93\u51fa20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. global\u5173\u952e\u5b57<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">counter = 0<\/p>\n<p>def increment():<\/p>\n<p>    global counter<\/p>\n<p>    counter += 1<\/p>\n<p>increment()<\/p>\n<p>print(counter)  # \u8f93\u51fa1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u53d8\u91cf\u547d\u540d\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><h4>1. \u6709\u610f\u4e49\u7684\u540d\u79f0<\/h4>\n<\/p>\n<p><p>\u53d8\u91cf\u540d\u5e94\u5f53\u6e05\u6670\u3001\u63cf\u8ff0\u6027\u5f3a\uff0c\u80fd\u591f\u53cd\u6620\u53d8\u91cf\u7684\u7528\u9014\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u63a8\u8350<\/p>\n<p>a = 10<\/p>\n<p>b = 20<\/p>\n<h2><strong>\u63a8\u8350<\/strong><\/h2>\n<p>age = 25<\/p>\n<p>height = 175<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u4e0b\u5212\u7ebf\u5206\u9694<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8f83\u957f\u7684\u53d8\u91cf\u540d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u5212\u7ebf\u5206\u9694\u5355\u8bcd\uff0c\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u63a8\u8350<\/p>\n<p>userage = 25<\/p>\n<h2><strong>\u63a8\u8350<\/strong><\/h2>\n<p>user_age = 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u907f\u514d\u4f7f\u7528\u5355\u5b57\u7b26\u540d\u79f0<\/h4>\n<\/p>\n<p><p>\u9664\u975e\u5728\u5faa\u73af\u4e2d\u4f7f\u7528\u8ba1\u6570\u5668\uff0c\u5426\u5219\u5e94\u907f\u514d\u4f7f\u7528\u5355\u5b57\u7b26\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u63a8\u8350<\/p>\n<p>x = 5<\/p>\n<h2><strong>\u63a8\u8350<\/strong><\/h2>\n<p>number_of_items = 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u89e3\u5305\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>Python\u652f\u6301\u89e3\u5305\u8d4b\u503c\uff0c\u53ef\u4ee5\u540c\u65f6\u5c06\u591a\u4e2a\u503c\u8d4b\u7ed9\u591a\u4e2a\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a, b, c = 1, 2, 3<\/p>\n<p>print(a)  # \u8f93\u51fa1<\/p>\n<p>print(b)  # \u8f93\u51fa2<\/p>\n<p>print(c)  # \u8f93\u51fa3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u89e3\u5305\u8d4b\u503c\u4ea4\u6362\u53d8\u91cf\u7684\u503c\uff0c\u800c\u65e0\u9700\u4e34\u65f6\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 5<\/p>\n<p>y = 10<\/p>\n<p>x, y = y, x<\/p>\n<p>print(x)  # \u8f93\u51fa10<\/p>\n<p>print(y)  # \u8f93\u51fa5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u53ef\u53d8\u5bf9\u8c61\u4e0e\u4e0d\u53ef\u53d8\u5bf9\u8c61<\/h3>\n<\/p>\n<p><h4>1. \u4e0d\u53ef\u53d8\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u548c\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u5bf9\u8c61\uff0c\u5b83\u4eec\u7684\u503c\u4e00\u65e6\u521b\u5efa\u5c31\u4e0d\u80fd\u6539\u53d8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>a = 20  # \u91cd\u65b0\u8d4b\u503c\uff0c\u4e0d\u662f\u4fee\u6539\u539f\u503c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u53ef\u53d8\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u3001\u5b57\u5178\u548c\u96c6\u5408\u662f\u53ef\u53d8\u5bf9\u8c61\uff0c\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u5176\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3]<\/p>\n<p>my_list.append(4)  # \u4fee\u6539\u5217\u8868\u5185\u5bb9<\/p>\n<p>print(my_list)  # \u8f93\u51fa[1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u6df1\u5165\u7406\u89e3\u53d8\u91cf\u7684\u5185\u5b58\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53d8\u91cf\u540d\u662f\u6307\u5411\u5bf9\u8c61\u7684\u5f15\u7528\uff08\u6216\u522b\u540d\uff09\uff0c\u800c\u4e0d\u662f\u5bf9\u8c61\u672c\u8eab\u3002\u5f53\u4f60\u5c06\u4e00\u4e2a\u53d8\u91cf\u8d4b\u503c\u7ed9\u53e6\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u5b83\u4eec\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = [1, 2, 3]<\/p>\n<p>b = a  # b\u548ca\u6307\u5411\u540c\u4e00\u4e2a\u5217\u8868\u5bf9\u8c61<\/p>\n<p>a.append(4)<\/p>\n<p>print(b)  # \u8f93\u51fa[1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>id()<\/code>\u51fd\u6570\u67e5\u770b\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>y = x<\/p>\n<p>print(id(x))  # \u8f93\u51fax\u7684\u5185\u5b58\u5730\u5740<\/p>\n<p>print(id(y))  # \u8f93\u51fay\u7684\u5185\u5b58\u5730\u5740\uff0c\u4e0ex\u76f8\u540c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528<code>None<\/code>\u8868\u793a\u7a7a\u503c<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>None<\/code>\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u5e38\u91cf\uff0c\u7528\u4e8e\u8868\u793a\u7a7a\u503c\u6216\u65e0\u503c\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u521d\u59cb\u5316\u53d8\u91cf\u6216\u4f5c\u4e3a\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = None<\/p>\n<p>def compute():<\/p>\n<p>    # \u4e00\u4e9b\u8ba1\u7b97\u903b\u8f91<\/p>\n<p>    return 42<\/p>\n<p>result = compute()<\/p>\n<p>print(result)  # \u8f93\u51fa42<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f<\/h3>\n<\/p>\n<p><p>\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u6307\u7684\u662f\u53d8\u91cf\u4ece\u521b\u5efa\u5230\u9500\u6bc1\u7684\u8fc7\u7a0b\u3002\u5c40\u90e8\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u901a\u5e38\u5728\u51fd\u6570\u8c03\u7528\u671f\u95f4\uff0c\u800c\u5168\u5c40\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u5728\u6574\u4e2a\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_function():<\/p>\n<p>    local_var = 10  # \u5c40\u90e8\u53d8\u91cf\uff0c\u4ec5\u5728\u51fd\u6570\u8c03\u7528\u671f\u95f4\u5b58\u5728<\/p>\n<p>    print(local_var)<\/p>\n<p>my_function()<\/p>\n<h2><strong>print(local_var)  # NameError: name &#39;local_var&#39; is not defined<\/strong><\/h2>\n<p>global_var = 20  # \u5168\u5c40\u53d8\u91cf\uff0c\u5b58\u5728\u4e8e\u6574\u4e2a\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4<\/p>\n<p>print(global_var)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u5783\u573e\u56de\u6536\u673a\u5236<\/h3>\n<\/p>\n<p><p>Python\u5177\u6709\u81ea\u52a8\u5783\u573e\u56de\u6536\u673a\u5236\uff0c\u7528\u4e8e\u7ba1\u7406\u5185\u5b58\u3002\u5f53\u4e00\u4e2a\u5bf9\u8c61\u6ca1\u6709\u4efb\u4f55\u5f15\u7528\u65f6\uff0c\u5783\u573e\u56de\u6536\u673a\u5236\u4f1a\u81ea\u52a8\u91ca\u653e\u8be5\u5bf9\u8c61\u7684\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = [1, 2, 3]<\/p>\n<p>b = a<\/p>\n<p>del a  # a\u4e0d\u518d\u5f15\u7528\u5217\u8868\u5bf9\u8c61<\/p>\n<p>print(b)  # \u8f93\u51fa[1, 2, 3]<\/p>\n<p>b = None  # \u5217\u8868\u5bf9\u8c61\u4e0d\u518d\u6709\u4efb\u4f55\u5f15\u7528\uff0c\u5185\u5b58\u5c06\u88ab\u91ca\u653e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u4f7f\u7528<code>del<\/code>\u5220\u9664\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u5173\u952e\u5b57\u5220\u9664\u53d8\u91cf\uff0c\u4ece\u800c\u91ca\u653e\u5176\u5f15\u7528\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>print(x)  # \u8f93\u51fa10<\/p>\n<p>del x<\/p>\n<h2><strong>print(x)  # NameError: name &#39;x&#39; is not defined<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u53d8\u91cf\u7684\u7c7b\u578b\u6ce8\u89e3<\/h3>\n<\/p>\n<p><p>\u5728Python 3.5\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u6ce8\u89e3\u4e3a\u53d8\u91cf\u3001\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u6307\u5b9a\u7c7b\u578b\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u53d8\u91cf\u7c7b\u578b\u6ce8\u89e3<\/p>\n<p>age: int = 25<\/p>\n<p>name: str = &quot;Alice&quot;<\/p>\n<h2><strong>\u51fd\u6570\u7c7b\u578b\u6ce8\u89e3<\/strong><\/h2>\n<p>def greet(name: str) -&gt; str:<\/p>\n<p>    return f&quot;Hello, {name}&quot;<\/p>\n<p>print(greet(&quot;Bob&quot;))  # \u8f93\u51faHello, Bob<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7c7b\u578b\u6ce8\u89e3\u4e0d\u4f1a\u5f71\u54cd\u4ee3\u7801\u7684\u8fd0\u884c\uff0c\u53ea\u662f\u4f5c\u4e3a\u5f00\u53d1\u8005\u7684\u63d0\u793a\u548c\u5de5\u5177\u7684\u53c2\u8003\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u4f7f\u7528<code>typing<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>typing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7c7b\u578b\u6ce8\u89e3\u9009\u9879\uff0c\u5982<code>List<\/code>\u3001<code>Dict<\/code>\u3001<code>Tuple<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from typing import List, Dict, Tuple<\/p>\n<h2><strong>\u5217\u8868\u7c7b\u578b\u6ce8\u89e3<\/strong><\/h2>\n<p>names: List[str] = [&quot;Alice&quot;, &quot;Bob&quot;, &quot;Charlie&quot;]<\/p>\n<h2><strong>\u5b57\u5178\u7c7b\u578b\u6ce8\u89e3<\/strong><\/h2>\n<p>ages: Dict[str, int] = {&quot;Alice&quot;: 25, &quot;Bob&quot;: 30}<\/p>\n<h2><strong>\u5143\u7ec4\u7c7b\u578b\u6ce8\u89e3<\/strong><\/h2>\n<p>coordinates: Tuple[int, int] = (10, 20)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5b9a\u4e49\u53d8\u91cf\u662fPython\u7f16\u7a0b\u4e2d\u6700\u57fa\u672c\u7684\u6280\u80fd\u4e4b\u4e00\u3002\u901a\u8fc7\u7406\u89e3\u53d8\u91cf\u7684\u57fa\u672c\u5b9a\u4e49\u65b9\u6cd5\u3001\u547d\u540d\u89c4\u5219\u3001\u7c7b\u578b\u8f6c\u6362\u3001\u4f5c\u7528\u57df\u3001\u5185\u5b58\u7ba1\u7406\u548c\u7c7b\u578b\u6ce8\u89e3\uff0c\u53ef\u4ee5\u7f16\u5199\u51fa\u66f4\u52a0\u6e05\u6670\u3001\u53ef\u7ef4\u62a4\u7684\u4ee3\u7801\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u9075\u5faa\u53d8\u91cf\u547d\u540d\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u5e76\u5229\u7528\u7c7b\u578b\u6ce8\u89e3\u548c<code>typing<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u7684\u8d28\u91cf\u548c\u53ef\u8bfb\u6027\u3002\u5e0c\u671b\u672c\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u638c\u63e1Python\u4e2d\u5b9a\u4e49\u53d8\u91cf\u7684\u5404\u79cd\u65b9\u6cd5\u548c\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5b9a\u4e49\u53d8\u91cf\u7684\u57fa\u672c\u8bed\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u53d8\u91cf\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\uff08=\uff09\u3002\u4f8b\u5982\uff0c\u8981\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a<code>my_variable<\/code>\u7684\u53d8\u91cf\u5e76\u8d4b\u503c\u4e3a10\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a<code>my_variable = 10<\/code>\u3002\u53d8\u91cf\u540d\u53ef\u4ee5\u5305\u542b\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\uff0c\u4f46\u4e0d\u80fd\u4ee5\u6570\u5b57\u5f00\u5934\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u53d8\u91cf\u7684\u547d\u540d\u89c4\u5219\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u53d8\u91cf\u547d\u540d\u9700\u8981\u9075\u5faa\u4e00\u4e9b\u89c4\u5219\u3002\u53d8\u91cf\u540d\u53ea\u80fd\u5305\u542b\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\uff0c\u4e0d\u80fd\u5305\u542b\u7a7a\u683c\u6216\u7279\u6b8a\u5b57\u7b26\uff0c\u4e14\u4e0d\u80fd\u4ee5\u6570\u5b57\u5f00\u5934\u3002\u6b64\u5916\uff0cPython\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\uff0c<code>myVariable<\/code>\u548c<code>myvariable<\/code>\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u53d8\u91cf\u3002\u4e3a\u4e86\u63d0\u9ad8\u53ef\u8bfb\u6027\uff0c\u63a8\u8350\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\u548c\u4e0b\u5212\u7ebf\u5206\u9694\u5355\u8bcd\uff0c\u4f8b\u5982<code>my_variable_name<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\u7684\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u540c\u65f6\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\u5e76\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u540c\u65f6\u5b9a\u4e49<code>a<\/code>, <code>b<\/code>, <code>c<\/code>\u4e09\u4e2a\u53d8\u91cf\uff1a<code>a, b, c = 1, 2, 3<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u63d0\u9ad8\u4e86\u53ef\u8bfb\u6027\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u503c\u7ed9\u591a\u4e2a\u53d8\u91cf\u8d4b\u503c\uff0c\u4f8b\u5982\uff1a<code>x = y = z = 0<\/code>\uff0c\u8fd9\u5c06\u628a<code>0<\/code>\u8d4b\u503c\u7ed9<code>x<\/code>\u3001<code>y<\/code>\u548c<code>z<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u53d8\u91cf\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u7b26\u201c=\u201d\u5c06\u4e00\u4e2a\u503c\u8d4b\u7ed9\u53d8\u91cf\u5373\u53ef\u3002\u4f8b\u5982\uff1ax = 5, name [&hellip;]","protected":false},"author":3,"featured_media":1170463,"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\/1170459"}],"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=1170459"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170459\/revisions"}],"predecessor-version":[{"id":1170465,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170459\/revisions\/1170465"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1170463"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1170459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1170459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1170459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}