{"id":1125277,"date":"2025-01-08T19:50:22","date_gmt":"2025-01-08T11:50:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1125277.html"},"modified":"2025-01-08T19:50:37","modified_gmt":"2025-01-08T11:50:37","slug":"python%e7%bc%96%e7%a8%8b%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e4%b8%80%e4%b8%aa%e5%8f%98%e9%87%8f%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1125277.html","title":{"rendered":"python\u7f16\u7a0b\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u7684\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090017\/419c8f5c-d268-4594-aa79-9a329e32631c.webp\" alt=\"python\u7f16\u7a0b\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\" \/><\/p>\n<p><p> <strong>\u5b9a\u4e49\u53d8\u91cf\u662fPython\u7f16\u7a0b\u7684\u57fa\u672c\u6280\u80fd\u4e4b\u4e00\u3002\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u53d8\u91cf\u7684\u503c\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26 <code>=<\/code>\u3002\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u9009\u62e9\u4e00\u4e2a\u53d8\u91cf\u540d\u3001\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u3001\u6307\u5b9a\u53d8\u91cf\u7684\u503c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf <code>x<\/code> \u5e76\u8d4b\u503c\u4e3a10\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u9009\u62e9\u5408\u9002\u7684\u53d8\u91cf\u540d\u3001\u786e\u4fdd\u53d8\u91cf\u540d\u9075\u5faa\u547d\u540d\u89c4\u8303\u3001\u7406\u89e3\u53d8\u91cf\u7684\u7c7b\u578b\u548c\u4f5c\u7528\u8303\u56f4<\/strong>\u662f\u5b9a\u4e49\u53d8\u91cf\u65f6\u9700\u8981\u6ce8\u610f\u7684\u5173\u952e\u70b9\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u8981\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684\u53d8\u91cf\u540d<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u53d8\u91cf\u540d\u662f\u7f16\u7a0b\u4e2d\u91cd\u8981\u7684\u4e00\u6b65\u3002\u53d8\u91cf\u540d\u5e94\u8be5\u5177\u6709\u63cf\u8ff0\u6027\uff0c\u4ee5\u4fbf\u4ee3\u7801\u53ef\u8bfb\u6027\u66f4\u5f3a\u3002<\/p>\n<\/p>\n<p><h4>\u63cf\u8ff0\u6027\u53d8\u91cf\u540d<\/h4>\n<\/p>\n<p><p>\u4e00\u4e2a\u597d\u7684\u53d8\u91cf\u540d\u80fd\u591f\u63cf\u8ff0\u53d8\u91cf\u7684\u7528\u9014\u3002\u4f8b\u5982\uff0c\u5982\u679c\u53d8\u91cf\u5b58\u50a8\u7684\u662f\u7528\u6237\u7684\u5e74\u9f84\uff0c\u53ef\u4ee5\u547d\u540d\u4e3a <code>user_age<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_age = 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u9075\u5faa\u547d\u540d\u89c4\u8303<\/h4>\n<\/p>\n<p><p>\u53d8\u91cf\u540d\u5e94\u9075\u5faaPython\u7684\u547d\u540d\u89c4\u8303\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u53d8\u91cf\u540d\u53ea\u80fd\u5305\u542b\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\uff08_\uff09\u3002<\/li>\n<li>\u53d8\u91cf\u540d\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e0d\u80fd\u662f\u6570\u5b57\u3002<\/li>\n<li>\u53d8\u91cf\u540d\u533a\u5206\u5927\u5c0f\u5199\uff08<code>age<\/code> \u548c <code>Age<\/code> \u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u53d8\u91cf\uff09\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">user_age = 25<\/p>\n<p>UserAge = 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u786e\u4fdd\u53d8\u91cf\u540d\u9075\u5faa\u547d\u540d\u89c4\u8303<\/h3>\n<\/p>\n<p><p>Python\u6709\u4e00\u5957\u4e25\u683c\u7684\u53d8\u91cf\u547d\u540d\u89c4\u5219\uff0c\u9075\u5faa\u8fd9\u4e9b\u89c4\u5219\u6709\u52a9\u4e8e\u907f\u514d\u8bed\u6cd5\u9519\u8bef\u548c\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h4>PEP 8 \u547d\u540d\u89c4\u8303<\/h4>\n<\/p>\n<p><p>PEP 8 \u662fPython\u7684\u7f16\u7801\u89c4\u8303\u6307\u5357\uff0c\u63a8\u8350\u53d8\u91cf\u540d\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\uff0c\u5355\u8bcd\u4e4b\u95f4\u4f7f\u7528\u4e0b\u5212\u7ebf\u5206\u9694\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">first_name = &quot;John&quot;<\/p>\n<p>last_name = &quot;Doe&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7406\u89e3\u53d8\u91cf\u7684\u7c7b\u578b\u548c\u4f5c\u7528\u8303\u56f4<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53d8\u91cf\u662f\u52a8\u6001\u7c7b\u578b\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u4e0d\u9700\u8981\u663e\u5f0f\u58f0\u660e\u53d8\u91cf\u7684\u7c7b\u578b\uff0cPython\u4f1a\u6839\u636e\u8d4b\u503c\u81ea\u52a8\u63a8\u65ad\u3002<\/p>\n<\/p>\n<p><h4>\u6570\u636e\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>Python\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">integer_var = 10<\/p>\n<p>float_var = 10.5<\/p>\n<p>string_var = &quot;Hello&quot;<\/p>\n<p>list_var = [1, 2, 3]<\/p>\n<p>tuple_var = (1, 2, 3)<\/p>\n<p>dict_var = {&quot;key&quot;: &quot;value&quot;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u53d8\u91cf\u4f5c\u7528\u8303\u56f4<\/h4>\n<\/p>\n<p><p>\u53d8\u91cf\u7684\u4f5c\u7528\u8303\u56f4\u51b3\u5b9a\u4e86\u53d8\u91cf\u5728\u4ee3\u7801\u4e2d\u7684\u53ef\u89c1\u6027\u3002Python\u4e2d\u53d8\u91cf\u7684\u4f5c\u7528\u8303\u56f4\u5206\u4e3a\u5c40\u90e8\u4f5c\u7528\u57df\u548c\u5168\u5c40\u4f5c\u7528\u57df\u3002<\/p>\n<\/p>\n<p><h5>\u5c40\u90e8\u53d8\u91cf<\/h5>\n<\/p>\n<p><p>\u5c40\u90e8\u53d8\u91cf\u662f\u5728\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u5176\u4f5c\u7528\u8303\u56f4\u4ec5\u9650\u4e8e\u51fd\u6570\u5185\u90e8\u3002\u4f8b\u5982\uff1a<\/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()<\/p>\n<p>print(local_var)  # \u8fd9\u884c\u4ee3\u7801\u4f1a\u629b\u51faNameError\u5f02\u5e38\uff0c\u56e0\u4e3alocal_var\u662f\u5c40\u90e8\u53d8\u91cf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u5168\u5c40\u53d8\u91cf<\/h5>\n<\/p>\n<p><p>\u5168\u5c40\u53d8\u91cf\u662f\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u5176\u4f5c\u7528\u8303\u56f4\u662f\u6574\u4e2a\u6a21\u5757\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">global_var = 10<\/p>\n<p>def my_function():<\/p>\n<p>    print(global_var)<\/p>\n<p>my_function()<\/p>\n<p>print(global_var)  # global_var\u5728\u51fd\u6570\u5185\u90e8\u548c\u5916\u90e8\u90fd\u53ef\u89c1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u5b9a\u4e49\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>Python\u652f\u6301\u4e30\u5bcc\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6bcf\u79cd\u6570\u636e\u7c7b\u578b\u6709\u5176\u7279\u5b9a\u7684\u7528\u9014\u3002\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u6839\u636e\u6570\u636e\u7684\u7279\u6027\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7c7b\u578b\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h4>\u6574\u6570\u548c\u6d6e\u70b9\u6570<\/h4>\n<\/p>\n<p><p>\u6574\u6570\u548c\u6d6e\u70b9\u6570\u662f\u6700\u5e38\u7528\u7684\u6570\u503c\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">integer_var = 10<\/p>\n<p>float_var = 10.5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u7528\u4e8e\u5b58\u50a8\u6587\u672c\u6570\u636e\uff0c\u4f7f\u7528\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\u8868\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string_var = &quot;Hello, World!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u652f\u6301\u591a\u79cd\u64cd\u4f5c\uff0c\u4f8b\u5982\u62fc\u63a5\u3001\u5207\u7247\u3001\u67e5\u627e\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b57\u7b26\u4e32\u62fc\u63a5<\/p>\n<p>full_name = &quot;John&quot; + &quot; &quot; + &quot;Doe&quot;<\/p>\n<h2><strong>\u5b57\u7b26\u4e32\u5207\u7247<\/strong><\/h2>\n<p>substring = full_name[0:4]  # \u8f93\u51fa: John<\/p>\n<h2><strong>\u5b57\u7b26\u4e32\u67e5\u627e<\/strong><\/h2>\n<p>position = full_name.find(&quot;Doe&quot;)  # \u8f93\u51fa: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u662f\u4e00\u79cd\u6709\u5e8f\u7684\u53ef\u53d8\u96c6\u5408\uff0c\u53ef\u4ee5\u5b58\u50a8\u4efb\u610f\u7c7b\u578b\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_var = [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5217\u8868\u652f\u6301\u591a\u79cd\u64cd\u4f5c\uff0c\u4f8b\u5982\u6dfb\u52a0\u3001\u5220\u9664\u3001\u6392\u5e8f\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u5143\u7d20<\/p>\n<p>list_var.append(6)<\/p>\n<h2><strong>\u5220\u9664\u5143\u7d20<\/strong><\/h2>\n<p>list_var.remove(3)<\/p>\n<h2><strong>\u5217\u8868\u6392\u5e8f<\/strong><\/h2>\n<p>list_var.sort()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5143\u7ec4<\/h4>\n<\/p>\n<p><p>\u5143\u7ec4\u662f\u4e00\u79cd\u6709\u5e8f\u7684\u4e0d\u53ef\u53d8\u96c6\u5408\uff0c\u7528\u4e8e\u5b58\u50a8\u591a\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">tuple_var = (1, 2, 3, 4, 5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5143\u7ec4\u4e00\u65e6\u5b9a\u4e49\uff0c\u5143\u7d20\u4e0d\u53ef\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><h4>\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u4e00\u79cd\u65e0\u5e8f\u7684\u952e\u503c\u5bf9\u96c6\u5408\uff0c\u7528\u4e8e\u5b58\u50a8\u5177\u6709\u6620\u5c04\u5173\u7cfb\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict_var = {&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 25}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b57\u5178\u652f\u6301\u591a\u79cd\u64cd\u4f5c\uff0c\u4f8b\u5982\u6dfb\u52a0\u3001\u5220\u9664\u3001\u67e5\u627e\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u952e\u503c\u5bf9<\/p>\n<p>dict_var[&quot;gender&quot;] = &quot;male&quot;<\/p>\n<h2><strong>\u5220\u9664\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>del dict_var[&quot;age&quot;]<\/p>\n<h2><strong>\u67e5\u627e\u503c<\/strong><\/h2>\n<p>name = dict_var.get(&quot;name&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u53d8\u91cf\u7684\u7c7b\u578b\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u7a0b\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u5bf9\u53d8\u91cf\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u7c7b\u578b\u8f6c\u6362\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>Python\u4f1a\u5728\u9700\u8981\u65f6\u81ea\u52a8\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u5f53\u6574\u6570\u548c\u6d6e\u70b9\u6570\u53c2\u4e0e\u8fd0\u7b97\u65f6\uff0c\u6574\u6570\u4f1a\u81ea\u52a8\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>y = 3.5<\/p>\n<p>result = x + y  # result \u7684\u503c\u4e3a 13.5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u9700\u8981\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\uff0c\u4f8b\u5982 <code>int()<\/code>\u3001<code>float()<\/code>\u3001<code>str()<\/code> \u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/p>\n<p>x = 10<\/p>\n<p>str_x = str(x)<\/p>\n<h2><strong>\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/strong><\/h2>\n<p>y = &quot;20&quot;<\/p>\n<p>int_y = int(y)<\/p>\n<h2><strong>\u6574\u6570\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>float_x = float(x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u3001\u6587\u4ef6\u8bfb\u5199\u7b49\u573a\u666f\u4e2d\u975e\u5e38\u5e38\u89c1\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u53d8\u91cf\u7684\u547d\u540d\u51b2\u7a81\u4e0e\u89e3\u51b3<\/h3>\n<\/p>\n<p><p>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u53d8\u91cf\u547d\u540d\u51b2\u7a81\u662f\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u3002\u547d\u540d\u51b2\u7a81\u4f1a\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\uff0c\u751a\u81f3\u5f15\u53d1\u9519\u8bef\u3002\u4ee5\u4e0b\u662f\u89e3\u51b3\u547d\u540d\u51b2\u7a81\u7684\u4e00\u4e9b\u7b56\u7565\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u547d\u540d\u7a7a\u95f4<\/h4>\n<\/p>\n<p><p>\u547d\u540d\u7a7a\u95f4\u662f\u89e3\u51b3\u53d8\u91cf\u547d\u540d\u51b2\u7a81\u7684\u6709\u6548\u65b9\u6cd5\u3002Python\u4e2d\u7684\u6a21\u5757\u548c\u5305\u63d0\u4f9b\u4e86\u547d\u540d\u7a7a\u95f4\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>import random<\/p>\n<h2><strong>\u4f7f\u7528\u6a21\u5757\u540d\u9650\u5b9a\u53d8\u91cf\u540d<\/strong><\/h2>\n<p>pi_value = math.pi<\/p>\n<p>random_value = random.random()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u524d\u7f00\u6216\u540e\u7f00<\/h4>\n<\/p>\n<p><p>\u5728\u53d8\u91cf\u540d\u4e2d\u6dfb\u52a0\u524d\u7f00\u6216\u540e\u7f00\u4e5f\u662f\u89e3\u51b3\u547d\u540d\u51b2\u7a81\u7684\u5e38\u89c1\u7b56\u7565\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_age_db = 25  # \u6570\u636e\u5e93\u4e2d\u7684\u7528\u6237\u5e74\u9f84<\/p>\n<p>user_age_form = 30  # \u8868\u5355\u4e2d\u7684\u7528\u6237\u5e74\u9f84<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u907f\u514d\u5168\u5c40\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u5c24\u5176\u662f\u5728\u5927\u578b\u9879\u76ee\u4e2d\u3002\u5168\u5c40\u53d8\u91cf\u5bb9\u6613\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\u548c\u610f\u5916\u4fee\u6539\u3002\u4f7f\u7528\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u4f20\u9012\u6570\u636e\uff0c\u53ef\u4ee5\u6709\u6548\u907f\u514d\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_age(birth_year, current_year):<\/p>\n<p>    age = current_year - birth_year<\/p>\n<p>    return age<\/p>\n<h2><strong>\u4f7f\u7528\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u4f20\u9012\u6570\u636e<\/strong><\/h2>\n<p>user_age = calculate_age(1990, 2023)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u4e0e\u5783\u573e\u56de\u6536<\/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\u5728Python\u4e2d\uff0c\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u7531\u4f5c\u7528\u57df\u51b3\u5b9a\u3002<\/p>\n<\/p>\n<p><h4>\u5c40\u90e8\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f<\/h4>\n<\/p>\n<p><p>\u5c40\u90e8\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u901a\u5e38\u5728\u51fd\u6570\u6267\u884c\u671f\u95f4\u3002\u5f53\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\uff0c\u5c40\u90e8\u53d8\u91cf\u4f1a\u88ab\u81ea\u52a8\u9500\u6bc1\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()<\/p>\n<h2><strong>local_var \u5728\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\u88ab\u9500\u6bc1<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5168\u5c40\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f<\/h4>\n<\/p>\n<p><p>\u5168\u5c40\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u901a\u5e38\u662f\u4ece\u521b\u5efa\u5230\u7a0b\u5e8f\u7ed3\u675f\u3002\u5f53\u7a0b\u5e8f\u7ec8\u6b62\u65f6\uff0c\u5168\u5c40\u53d8\u91cf\u4f1a\u88ab\u9500\u6bc1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">global_var = 10<\/p>\n<p>def my_function():<\/p>\n<p>    print(global_var)<\/p>\n<p>my_function()<\/p>\n<h2><strong>global_var \u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u88ab\u9500\u6bc1<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5783\u573e\u56de\u6536<\/h4>\n<\/p>\n<p><p>Python\u4f7f\u7528\u5783\u573e\u56de\u6536\u673a\u5236\u81ea\u52a8\u7ba1\u7406\u5185\u5b58\u3002\u5783\u573e\u56de\u6536\u5668\u4f1a\u5b9a\u671f\u68c0\u67e5\u5e76\u56de\u6536\u4e0d\u518d\u4f7f\u7528\u7684\u5bf9\u8c61\uff0c\u4ee5\u91ca\u653e\u5185\u5b58\u7a7a\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_function():<\/p>\n<p>    local_var = [1, 2, 3]<\/p>\n<p>    # local_var \u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u5176\u5f15\u7528\u8ba1\u6570\u4e3a 1<\/p>\n<p>my_function()<\/p>\n<h2><strong>local_var \u88ab\u9500\u6bc1\uff0c\u5176\u5f15\u7528\u8ba1\u6570\u53d8\u4e3a 0\uff0c\u5bf9\u8c61\u88ab\u5783\u573e\u56de\u6536<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5e38\u89c1\u9519\u8bef\u4e0e\u8c03\u8bd5\u6280\u5de7<\/h3>\n<\/p>\n<p><p>\u5728\u5b9a\u4e49\u53d8\u91cf\u65f6\uff0c\u521d\u5b66\u8005\u5e38\u5e38\u4f1a\u72af\u4e00\u4e9b\u9519\u8bef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u9519\u8bef\u53ca\u5176\u8c03\u8bd5\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h4>\u672a\u5b9a\u4e49\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u672a\u5b9a\u4e49\u7684\u53d8\u91cf\u4f1a\u5bfc\u81f4 <code>NameError<\/code> \u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_function():<\/p>\n<p>    print(undeclared_var)  # NameError: name &#39;undeclared_var&#39; is not defined<\/p>\n<p>my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u53d8\u91cf\u540d\u62fc\u5199\u9519\u8bef<\/h4>\n<\/p>\n<p><p>\u53d8\u91cf\u540d\u62fc\u5199\u9519\u8bef\u4e5f\u662f\u5e38\u89c1\u95ee\u9898\u3002\u5efa\u8bae\u4f7f\u7528IDE\u7684\u81ea\u52a8\u8865\u5168\u529f\u80fd\uff0c\u51cf\u5c11\u62fc\u5199\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_name = &quot;John&quot;<\/p>\n<p>print(user_nam)  # NameError: name &#39;user_nam&#39; is not defined<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7c7b\u578b\u9519\u8bef<\/h4>\n<\/p>\n<p><p>\u7c7b\u578b\u9519\u8bef\u901a\u5e38\u53d1\u751f\u5728\u4e0d\u517c\u5bb9\u7684\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u8fdb\u884c\u64cd\u4f5c\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>y = &quot;20&quot;<\/p>\n<p>result = x + y  # TypeError: unsupported operand type(s) for +: &#39;int&#39; and &#39;str&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8c03\u8bd5\u6280\u5de7<\/h4>\n<\/p>\n<ul>\n<li>\u4f7f\u7528 <code>print()<\/code> \u51fd\u6570\u8f93\u51fa\u53d8\u91cf\u7684\u503c\uff0c\u5e2e\u52a9\u5b9a\u4f4d\u9519\u8bef\u3002<\/li>\n<li>\u4f7f\u7528IDE\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u89c2\u5bdf\u53d8\u91cf\u7684\u53d8\u5316\u3002<\/li>\n<li>\u4f7f\u7528 <code>type()<\/code> \u51fd\u6570\u68c0\u67e5\u53d8\u91cf\u7684\u7c7b\u578b\uff0c\u786e\u4fdd\u64cd\u4f5c\u5408\u6cd5\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>y = &quot;20&quot;<\/p>\n<h2><strong>\u8f93\u51fa\u53d8\u91cf\u7684\u503c\u548c\u7c7b\u578b<\/strong><\/h2>\n<p>print(x, type(x))<\/p>\n<p>print(y, type(y))<\/p>\n<h2><strong>\u786e\u4fdd\u7c7b\u578b\u8f6c\u6362\u540e\u518d\u8fdb\u884c\u64cd\u4f5c<\/strong><\/h2>\n<p>result = x + int(y)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u53d8\u91cf\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u63cf\u8ff0\u6027\u53d8\u91cf\u540d<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u63cf\u8ff0\u6027\u53d8\u91cf\u540d\uff0c\u4f7f\u4ee3\u7801\u66f4\u6613\u8bfb\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>user_age = 25<\/p>\n<p>user_height = 175<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u9075\u5faa\u547d\u540d\u89c4\u8303<\/h4>\n<\/p>\n<p><p>\u9075\u5faaPEP 8\u547d\u540d\u89c4\u8303\uff0c\u4f7f\u4ee3\u7801\u98ce\u683c\u7edf\u4e00\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>\u907f\u514d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u4f7f\u7528\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u4f20\u9012\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u63a8\u8350<\/p>\n<p>global_var = 10<\/p>\n<p>def my_function():<\/p>\n<p>    print(global_var)<\/p>\n<h2><strong>\u63a8\u8350<\/strong><\/h2>\n<p>def calculate_age(birth_year, current_year):<\/p>\n<p>    age = current_year - birth_year<\/p>\n<p>    return age<\/p>\n<p>user_age = calculate_age(1990, 2023)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u6ce8\u91ca<\/h4>\n<\/p>\n<p><p>\u5728\u590d\u6742\u4ee3\u7801\u6bb5\u4e2d\u6dfb\u52a0\u6ce8\u91ca\uff0c\u5e2e\u52a9\u7406\u89e3\u4ee3\u7801\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u7528\u6237\u5e74\u9f84<\/p>\n<p>def calculate_age(birth_year, current_year):<\/p>\n<p>    # \u8ba1\u7b97\u5e74\u9f84<\/p>\n<p>    age = current_year - birth_year<\/p>\n<p>    return age<\/p>\n<p>user_age = calculate_age(1990, 2023)<\/p>\n<p>print(user_age)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5b9a\u4e49\u53d8\u91cf\u662fPython\u7f16\u7a0b\u7684\u57fa\u7840\u6280\u80fd\u3002\u5728\u5b9a\u4e49\u53d8\u91cf\u65f6\uff0c\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u53d8\u91cf\u540d\u3001\u786e\u4fdd\u53d8\u91cf\u540d\u9075\u5faa\u547d\u540d\u89c4\u8303\u3001\u7406\u89e3\u53d8\u91cf\u7684\u7c7b\u578b\u548c\u4f5c\u7528\u8303\u56f4\u3002\u901a\u8fc7\u4f7f\u7528\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u5b9a\u4e49\u53d8\u91cf\u3001\u7c7b\u578b\u8f6c\u6362\u3001\u907f\u514d\u547d\u540d\u51b2\u7a81\u3001\u7406\u89e3\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u4e0e\u5783\u573e\u56de\u6536\u3001\u8c03\u8bd5\u6280\u5de7\u4ee5\u53ca\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u8bfb\u8005\u80fd\u591f\u6df1\u5165\u7406\u89e3Python\u4e2d\u5b9a\u4e49\u53d8\u91cf\u7684\u65b9\u6cd5\u548c\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u548c\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u521b\u5efa\u548c\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ea\u9700\u9009\u62e9\u4e00\u4e2a\u540d\u79f0\u5e76\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\uff08=\uff09\u6765\u8bbe\u7f6e\u53d8\u91cf\u7684\u503c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5199 <code>x = 10<\/code> \u6765\u521b\u5efa\u4e00\u4e2a\u540d\u4e3ax\u7684\u53d8\u91cf\uff0c\u5e76\u5c06\u5176\u503c\u8bbe\u7f6e\u4e3a10\u3002Python\u4f1a\u6839\u636e\u8d4b\u503c\u7684\u7c7b\u578b\u81ea\u52a8\u63a8\u65ad\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u53d8\u91cf\u540d\u6709\u4ec0\u4e48\u547d\u540d\u89c4\u5219\uff1f<\/strong><br \/>\u53d8\u91cf\u540d\u7684\u547d\u540d\u89c4\u5219\u5305\u62ec\uff1a\u53d8\u91cf\u540d\u5fc5\u987b\u4ee5\u5b57\u6bcd\u6216\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u540e\u7eed\u5b57\u7b26\u53ef\u4ee5\u662f\u5b57\u6bcd\u3001\u6570\u5b57\u6216\u4e0b\u5212\u7ebf\u3002\u53d8\u91cf\u540d\u4e0d\u80fd\u5305\u542b\u7a7a\u683c\u6216\u7279\u6b8a\u7b26\u53f7\uff0c\u4e5f\u4e0d\u80fd\u662fPython\u7684\u4fdd\u7559\u5173\u952e\u5b57\u3002\u6b64\u5916\uff0c\u53d8\u91cf\u540d\u5bf9\u5927\u5c0f\u5199\u654f\u611f\uff0c\u4f8b\u5982\uff0c\u53d8\u91cf<code>myVar<\/code>\u548c<code>myvar<\/code>\u88ab\u89c6\u4e3a\u4e24\u4e2a\u4e0d\u540c\u7684\u53d8\u91cf\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u66f4\u6539\u53d8\u91cf\u7684\u503c\uff1f<\/strong><br \/>\u66f4\u6539\u53d8\u91cf\u7684\u503c\u540c\u6837\u7b80\u5355\u3002\u53ea\u9700\u518d\u6b21\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5148\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf <code>count = 5<\/code>\uff0c\u7136\u540e\u901a\u8fc7 <code>count = count + 1<\/code> \u6765\u589e\u52a0\u5b83\u7684\u503c\uff0c\u6216\u8005\u76f4\u63a5\u8d4b\u503c <code>count = 10<\/code> \u6765\u91cd\u65b0\u5b9a\u4e49\u8fd9\u4e2a\u53d8\u91cf\u3002Python\u4f1a\u76f4\u63a5\u66f4\u65b0\u53d8\u91cf\u7684\u503c\uff0c\u800c\u65e0\u9700\u58f0\u660e\u5b83\u7684\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5b9a\u4e49\u53d8\u91cf\u662fPython\u7f16\u7a0b\u7684\u57fa\u672c\u6280\u80fd\u4e4b\u4e00\u3002\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u53d8\u91cf\u7684\u503c\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26 =\u3002\u5173\u952e [&hellip;]","protected":false},"author":3,"featured_media":1125305,"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\/1125277"}],"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=1125277"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1125277\/revisions"}],"predecessor-version":[{"id":1125307,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1125277\/revisions\/1125307"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1125305"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1125277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1125277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1125277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}