{"id":1151027,"date":"2025-01-13T17:08:36","date_gmt":"2025-01-13T09:08:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1151027.html"},"modified":"2025-01-13T17:08:39","modified_gmt":"2025-01-13T09:08:39","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e5%8f%98%e9%87%8f%e9%95%bf%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1151027.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4e49\u53d8\u91cf\u957f\u5ea6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25181353\/eb6ee173-582c-43d2-bd90-cb54af96795a.webp\" alt=\"python\u5982\u4f55\u5b9a\u4e49\u53d8\u91cf\u957f\u5ea6\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u5b9a\u4e49\u53d8\u91cf\u957f\u5ea6\u7684\u65b9\u6cd5\u5305\u62ec\u3001\u52a8\u6001\u7c7b\u578b\u3001\u5217\u8868\u548c\u5b57\u5178\u7b49\u6570\u636e\u7ed3\u6784\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u4f7f\u7528\u52a8\u6001\u7c7b\u578b\u548c\u6570\u636e\u7ed3\u6784\u6765\u5b9e\u73b0\u7075\u6d3b\u7684\u957f\u5ea6\u5b9a\u4e49\u3002Python\u662f\u4e00\u79cd\u52a8\u6001\u7c7b\u578b\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u8fd9\u610f\u5473\u7740\u53d8\u91cf\u7684\u7c7b\u578b\u548c\u957f\u5ea6\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u52a8\u6001\u53d8\u5316\uff0c\u800c\u4e0d\u9700\u8981\u4e8b\u5148\u58f0\u660e\u5176\u957f\u5ea6\u6216\u7c7b\u578b\u3002\u901a\u8fc7\u8fd9\u79cd\u7279\u6027\uff0cPython\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u591a\u79cd\u6570\u636e\u7c7b\u578b\u548c\u957f\u5ea6\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><p><strong>\u52a8\u6001\u7c7b\u578b<\/strong>\uff1aPython\u4e2d\u7684\u53d8\u91cf\u4e0d\u9700\u8981\u58f0\u660e\u7c7b\u578b\uff0c\u53d8\u91cf\u7684\u7c7b\u578b\u548c\u957f\u5ea6\u53ef\u4ee5\u968f\u8d4b\u503c\u7684\u5185\u5bb9\u800c\u53d8\u5316\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u8d4b\u503c\u64cd\u4f5c\u5c06\u4e00\u4e2a\u6574\u6570\u53d8\u91cf\u53d8\u4e3a\u5b57\u7b26\u4e32\u6216\u5217\u8868\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>print(x)  # \u8f93\u51fa: 10<\/p>\n<p>x = &quot;Hello&quot;<\/p>\n<p>print(x)  # \u8f93\u51fa: Hello<\/p>\n<p>x = [1, 2, 3]<\/p>\n<p>print(x)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u52a8\u6001\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>Python\u662f\u4e00\u79cd\u52a8\u6001\u7c7b\u578b\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u8fd9\u610f\u5473\u7740\u53d8\u91cf\u7684\u7c7b\u578b\u548c\u957f\u5ea6\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u52a8\u6001\u53d8\u5316\uff0c\u800c\u4e0d\u9700\u8981\u4e8b\u5148\u58f0\u660e\u5176\u957f\u5ea6\u6216\u7c7b\u578b\u3002\u52a8\u6001\u7c7b\u578b\u7684\u7279\u6027\u4f7f\u5f97Python\u53d8\u5f97\u975e\u5e38\u7075\u6d3b\u548c\u6613\u4e8e\u4f7f\u7528\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u52a8\u6001\u7c7b\u578b\u7684\u7279\u70b9\u548c\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u53d8\u91cf\u7684\u7c7b\u578b\u548c\u957f\u5ea6\u52a8\u6001\u53d8\u5316<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53d8\u91cf\u7684\u7c7b\u578b\u548c\u957f\u5ea6\u53ef\u4ee5\u6839\u636e\u8d4b\u503c\u7684\u5185\u5bb9\u800c\u53d8\u5316\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6574\u6570\u53d8\u91cf\u53d8\u4e3a\u5b57\u7b26\u4e32\u6216\u5217\u8868\u3002\u8fd9\u79cd\u7075\u6d3b\u6027\u4f7f\u5f97Python\u5728\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u65f6\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>print(x)  # \u8f93\u51fa: 10<\/p>\n<p>x = &quot;Hello&quot;<\/p>\n<p>print(x)  # \u8f93\u51fa: Hello<\/p>\n<p>x = [1, 2, 3]<\/p>\n<p>print(x)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u53d8\u91cf<code>x<\/code>\u7684\u7c7b\u578b\u548c\u957f\u5ea6\u968f\u7740\u8d4b\u503c\u7684\u5185\u5bb9\u800c\u53d8\u5316\uff0c\u4ece\u6574\u6570\u53d8\u4e3a\u5b57\u7b26\u4e32\uff0c\u6700\u540e\u53d8\u4e3a\u5217\u8868\u3002\u8fd9\u79cd\u52a8\u6001\u53d8\u5316\u7684\u7279\u6027\u4f7f\u5f97Python\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u66f4\u52a0\u7b80\u6d01\u548c\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7c7b\u578b\u68c0\u67e5<\/h4>\n<\/p>\n<p><p>\u5c3d\u7ba1Python\u662f\u52a8\u6001\u7c7b\u578b\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u4f46\u6709\u65f6\u4ecd\u9700\u8981\u68c0\u67e5\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u53ef\u4ee5\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u53d8\u91cf\u7684\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>print(type(x))  # \u8f93\u51fa: &lt;class &#39;int&#39;&gt;<\/p>\n<p>x = &quot;Hello&quot;<\/p>\n<p>print(type(x))  # \u8f93\u51fa: &lt;class &#39;str&#39;&gt;<\/p>\n<p>x = [1, 2, 3]<\/p>\n<p>print(type(x))  # \u8f93\u51fa: &lt;class &#39;list&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u52a8\u6001\u68c0\u67e5\u53d8\u91cf\u7684\u7c7b\u578b\uff0c\u4ece\u800c\u6839\u636e\u9700\u8981\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5217\u8868<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5217\u8868\u662f\u4e00\u79cd\u53ef\u53d8\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5305\u542b\u4efb\u610f\u6570\u91cf\u7684\u5143\u7d20\uff0c\u5e76\u4e14\u53ef\u4ee5\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u7684\u5143\u7d20\u3002\u5217\u8868\u7684\u957f\u5ea6\u662f\u52a8\u6001\u53ef\u53d8\u7684\uff0c\u53ef\u4ee5\u968f\u65f6\u589e\u52a0\u6216\u5220\u9664\u5143\u7d20\u3002\u5217\u8868\u662f\u4e00\u79cd\u975e\u5e38\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5b58\u50a8\u548c\u64cd\u4f5c\u4e00\u7ec4\u6709\u5e8f\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u548c\u64cd\u4f5c\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4f7f\u7528\u65b9\u62ec\u53f7<code>[]<\/code>\u6765\u5305\u542b\u5143\u7d20\uff0c\u5e76\u4f7f\u7528\u9017\u53f7<code>,<\/code>\u5206\u9694\u5404\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u7d22\u5f15\u4ece0\u5f00\u59cb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(my_list[0])  # \u8f93\u51fa: 1<\/p>\n<p>print(my_list[2])  # \u8f93\u51fa: 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u83b7\u53d6\u5217\u8868\u7684\u5b50\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(my_list[1:3])  # \u8f93\u51fa: [2, 3]<\/p>\n<p>print(my_list[:2])   # \u8f93\u51fa: [1, 2]<\/p>\n<p>print(my_list[3:])   # \u8f93\u51fa: [4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4fee\u6539\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u662f\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u968f\u65f6\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3001\u589e\u52a0\u6216\u5220\u9664\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list[2] = 10<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 10, 4, 5]<\/p>\n<p>my_list.append(6)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 10, 4, 5, 6]<\/p>\n<p>my_list.insert(1, 0)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 0, 2, 10, 4, 5, 6]<\/p>\n<p>my_list.remove(10)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 0, 2, 4, 5, 6]<\/p>\n<p>del my_list[1]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u5217\u8868\u8fdb\u884c\u4fee\u6539\uff0c\u589e\u52a0\u6216\u5220\u9664\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5b57\u5178<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5b57\u5178\u662f\u4e00\u79cd\u65e0\u5e8f\u7684\u6570\u636e\u7ed3\u6784\uff0c\u901a\u8fc7\u952e\u503c\u5bf9\u6765\u5b58\u50a8\u6570\u636e\u3002\u5b57\u5178\u7684\u957f\u5ea6\u662f\u52a8\u6001\u53ef\u53d8\u7684\uff0c\u53ef\u4ee5\u968f\u65f6\u589e\u52a0\u6216\u5220\u9664\u952e\u503c\u5bf9\u3002\u5b57\u5178\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u67e5\u627e\u548c\u5b58\u50a8\u952e\u503c\u5bf9\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u548c\u64cd\u4f5c\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178\u53ef\u4ee5\u4f7f\u7528\u82b1\u62ec\u53f7<code>{}<\/code>\u6765\u5305\u542b\u952e\u503c\u5bf9\uff0c\u5e76\u4f7f\u7528\u5192\u53f7<code>:<\/code>\u5206\u9694\u952e\u548c\u503c\uff0c\u4f7f\u7528\u9017\u53f7<code>,<\/code>\u5206\u9694\u5404\u4e2a\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u952e\u6765\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(my_dict[&quot;name&quot;])  # \u8f93\u51fa: Alice<\/p>\n<p>print(my_dict[&quot;age&quot;])   # \u8f93\u51fa: 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4fee\u6539\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u968f\u65f6\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3001\u589e\u52a0\u6216\u5220\u9664\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict[&quot;age&quot;] = 26<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 26, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>my_dict[&quot;country&quot;] = &quot;USA&quot;<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 26, &#39;city&#39;: &#39;New York&#39;, &#39;country&#39;: &#39;USA&#39;}<\/p>\n<p>del my_dict[&quot;city&quot;]<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 26, &#39;country&#39;: &#39;USA&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u5b57\u5178\u8fdb\u884c\u4fee\u6539\uff0c\u589e\u52a0\u6216\u5220\u9664\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5143\u7ec4<\/h3>\n<\/p>\n<p><p>\u5143\u7ec4\u662fPython\u4e2d\u7684\u53e6\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7c7b\u4f3c\u4e8e\u5217\u8868\uff0c\u4f46\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\u3002\u8fd9\u610f\u5473\u7740\u4e00\u65e6\u5b9a\u4e49\u4e86\u5143\u7ec4\uff0c\u5176\u5143\u7d20\u5c31\u4e0d\u80fd\u66f4\u6539\u3002\u5143\u7ec4\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u53ef\u4ee5\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u548c\u64cd\u4f5c\u5143\u7ec4<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u5143\u7ec4\u53ef\u4ee5\u4f7f\u7528\u5706\u62ec\u53f7<code>()<\/code>\u6765\u5305\u542b\u5143\u7d20\uff0c\u5e76\u4f7f\u7528\u9017\u53f7<code>,<\/code>\u5206\u9694\u5404\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (1, 2, 3, 4, 5)<\/p>\n<p>print(my_tuple)  # \u8f93\u51fa: (1, 2, 3, 4, 5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u8bbf\u95ee\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u7d22\u5f15\u4ece0\u5f00\u59cb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(my_tuple[0])  # \u8f93\u51fa: 1<\/p>\n<p>print(my_tuple[2])  # \u8f93\u51fa: 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u83b7\u53d6\u5143\u7ec4\u7684\u5b50\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(my_tuple[1:3])  # \u8f93\u51fa: (2, 3)<\/p>\n<p>print(my_tuple[:2])   # \u8f93\u51fa: (1, 2)<\/p>\n<p>print(my_tuple[3:])   # \u8f93\u51fa: (4, 5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5143\u7ec4\u7684\u4e0d\u53ef\u53d8\u6027<\/h4>\n<\/p>\n<p><p>\u5143\u7ec4\u7684\u4e0d\u53ef\u53d8\u6027\u610f\u5473\u7740\u4e00\u65e6\u5b9a\u4e49\u4e86\u5143\u7ec4\uff0c\u5176\u5143\u7d20\u5c31\u4e0d\u80fd\u66f4\u6539\u3002\u5982\u679c\u5c1d\u8bd5\u4fee\u6539\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple[2] = 10  # \u4f1a\u5f15\u53d1TypeError: &#39;tuple&#39; object does not support item assignment<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c3d\u7ba1\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u8d4b\u503c\u6765\u6539\u53d8\u5143\u7ec4\u53d8\u91cf\u6240\u5f15\u7528\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (1, 2, 10, 4, 5)<\/p>\n<p>print(my_tuple)  # \u8f93\u51fa: (1, 2, 10, 4, 5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u96c6\u5408<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u662fPython\u4e2d\u7684\u4e00\u79cd\u65e0\u5e8f\u6570\u636e\u7ed3\u6784\uff0c\u5305\u542b\u552f\u4e00\u7684\u5143\u7d20\u3002\u96c6\u5408\u7684\u957f\u5ea6\u662f\u52a8\u6001\u53ef\u53d8\u7684\uff0c\u53ef\u4ee5\u968f\u65f6\u589e\u52a0\u6216\u5220\u9664\u5143\u7d20\u3002\u96c6\u5408\u9002\u7528\u4e8e\u9700\u8981\u5b58\u50a8\u552f\u4e00\u5143\u7d20\u5e76\u8fdb\u884c\u96c6\u5408\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u548c\u64cd\u4f5c\u96c6\u5408<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u96c6\u5408\u53ef\u4ee5\u4f7f\u7528\u82b1\u62ec\u53f7<code>{}<\/code>\u6765\u5305\u542b\u5143\u7d20\uff0c\u5e76\u4f7f\u7528\u9017\u53f7<code>,<\/code>\u5206\u9694\u5404\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_set = {1, 2, 3, 4, 5}<\/p>\n<p>print(my_set)  # \u8f93\u51fa: {1, 2, 3, 4, 5}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u96c6\u5408\u64cd\u4f5c\u6765\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_set.add(6)<\/p>\n<p>print(my_set)  # \u8f93\u51fa: {1, 2, 3, 4, 5, 6}<\/p>\n<p>my_set.remove(2)<\/p>\n<p>print(my_set)  # \u8f93\u51fa: {1, 3, 4, 5, 6}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u96c6\u5408\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u96c6\u5408\u652f\u6301\u591a\u79cd\u96c6\u5408\u64cd\u4f5c\uff0c\u5982\u5e76\u96c6\u3001\u4ea4\u96c6\u548c\u5dee\u96c6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">set1 = {1, 2, 3}<\/p>\n<p>set2 = {3, 4, 5}<\/p>\n<h2><strong>\u5e76\u96c6<\/strong><\/h2>\n<p>union_set = set1.union(set2)<\/p>\n<p>print(union_set)  # \u8f93\u51fa: {1, 2, 3, 4, 5}<\/p>\n<h2><strong>\u4ea4\u96c6<\/strong><\/h2>\n<p>intersection_set = set1.intersection(set2)<\/p>\n<p>print(intersection_set)  # \u8f93\u51fa: {3}<\/p>\n<h2><strong>\u5dee\u96c6<\/strong><\/h2>\n<p>difference_set = set1.difference(set2)<\/p>\n<p>print(difference_set)  # \u8f93\u51fa: {1, 2}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u96c6\u5408\u8fdb\u884c\u591a\u79cd\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u662fPython\u4e2d\u7684\u4e00\u79cd\u4e0d\u53ef\u53d8\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u6587\u672c\u6570\u636e\u3002\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u64cd\u4f5c\u6765\u751f\u6210\u65b0\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u4e32\u53ef\u4ee5\u4f7f\u7528\u5355\u5f15\u53f7<code>&#39;<\/code>\u6216\u53cc\u5f15\u53f7<code>&quot;<\/code>\u6765\u5305\u542b\u6587\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_string = &quot;Hello, world!&quot;<\/p>\n<p>print(my_string)  # \u8f93\u51fa: Hello, world!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u8bbf\u95ee\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\uff0c\u7d22\u5f15\u4ece0\u5f00\u59cb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(my_string[0])  # \u8f93\u51fa: H<\/p>\n<p>print(my_string[7])  # \u8f93\u51fa: w<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(my_string[0:5])  # \u8f93\u51fa: Hello<\/p>\n<p>print(my_string[:5])   # \u8f93\u51fa: Hello<\/p>\n<p>print(my_string[7:])   # \u8f93\u51fa: world!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5c3d\u7ba1\u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u64cd\u4f5c\u751f\u6210\u65b0\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">new_string = my_string.replace(&quot;world&quot;, &quot;Python&quot;)<\/p>\n<p>print(new_string)  # \u8f93\u51fa: Hello, Python!<\/p>\n<p>upper_string = my_string.upper()<\/p>\n<p>print(upper_string)  # \u8f93\u51fa: HELLO, WORLD!<\/p>\n<p>split_string = my_string.split(&quot;, &quot;)<\/p>\n<p>print(split_string)  # \u8f93\u51fa: [&#39;Hello&#39;, &#39;world!&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u751f\u6210\u548c\u5904\u7406\u65b0\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684\u53d8\u91cf\u957f\u5ea6\u662f\u52a8\u6001\u53ef\u53d8\u7684\uff0c\u8fd9\u4f7f\u5f97Python\u5728\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u548c\u957f\u5ea6\u7684\u6570\u636e\u65f6\u975e\u5e38\u7075\u6d3b\u548c\u65b9\u4fbf\u3002\u901a\u8fc7\u4f7f\u7528\u52a8\u6001\u7c7b\u578b\u3001\u5217\u8868\u3001\u5b57\u5178\u3001\u5143\u7ec4\u3001\u96c6\u5408\u548c\u5b57\u7b26\u4e32\u7b49\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9a\u4e49\u548c\u64cd\u4f5c\u4e0d\u540c\u957f\u5ea6\u7684\u53d8\u91cf\u3002\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u548c\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5c06\u6709\u52a9\u4e8e\u66f4\u9ad8\u6548\u5730\u7f16\u5199Python\u4ee3\u7801\u5e76\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u53d8\u91cf\u7684\u957f\u5ea6\u662f\u5426\u6709\u9650\u5236\uff1f<\/strong><br \/>Python\u4e2d\u7684\u53d8\u91cf\u672c\u8eab\u6ca1\u6709\u56fa\u5b9a\u7684\u957f\u5ea6\u9650\u5236\u3002\u53d8\u91cf\u7684\u957f\u5ea6\u4e3b\u8981\u53d6\u51b3\u4e8e\u6240\u5b58\u50a8\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u53ef\u4ee5\u662f\u4efb\u610f\u7684\uff0c\u53ea\u8981\u7cfb\u7edf\u5185\u5b58\u5141\u8bb8\uff1b\u800c\u5217\u8868\u3001\u5b57\u5178\u7b49\u6570\u636e\u7ed3\u6784\u7684\u957f\u5ea6\u540c\u6837\u53ef\u4ee5\u6839\u636e\u5b58\u50a8\u7684\u6570\u636e\u91cf\u800c\u53d8\u5316\u3002\u56e0\u6b64\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u9700\u8981\u52a8\u6001\u5730\u6dfb\u52a0\u6216\u4fee\u6539\u53d8\u91cf\u7684\u5185\u5bb9\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u957f\u5ea6\u9650\u5236\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5Python\u53d8\u91cf\u7684\u957f\u5ea6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>len()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u5404\u79cd\u6570\u636e\u7c7b\u578b\u7684\u957f\u5ea6\u3002\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u5217\u8868\u548c\u5b57\u5178\u7b49\uff0c<code>len()<\/code>\u51fd\u6570\u4f1a\u8fd4\u56de\u8fd9\u4e9b\u5bf9\u8c61\u4e2d\u5143\u7d20\u7684\u6570\u91cf\u3002\u4f8b\u5982\uff0c<code>len(my_string)<\/code>\u5c06\u8fd4\u56de\u5b57\u7b26\u4e32<code>my_string<\/code>\u7684\u5b57\u7b26\u6570\uff0c\u800c<code>len(my_list)<\/code>\u5c06\u8fd4\u56de\u5217\u8868<code>my_list<\/code>\u4e2d\u7684\u5143\u7d20\u4e2a\u6570\u3002\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u7b80\u5355\u660e\u4e86\uff0c\u9002\u5408\u4e8e\u5404\u79cd\u6570\u636e\u7c7b\u578b\u7684\u957f\u5ea6\u68c0\u67e5\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4e3aPython\u53d8\u91cf\u8bbe\u7f6e\u7279\u5b9a\u7684\u957f\u5ea6\u9650\u5236\u5417\uff1f<\/strong><br \/>\u867d\u7136Python\u672c\u8eab\u4e0d\u652f\u6301\u5bf9\u53d8\u91cf\u957f\u5ea6\u7684\u76f4\u63a5\u9650\u5236\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u6216\u4f7f\u7528\u7c7b\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u65f6\uff0c\u6dfb\u52a0\u4e00\u4e2a\u65b9\u6cd5\u4ee5\u68c0\u67e5\u53d8\u91cf\u7684\u957f\u5ea6\u662f\u5426\u5728\u6307\u5b9a\u8303\u56f4\u5185\uff0c\u4ece\u800c\u63a7\u5236\u53d8\u91cf\u7684\u6709\u6548\u6027\u3002\u6b64\u5916\uff0c\u5bf9\u4e8e\u7279\u5b9a\u7684\u5e94\u7528\u573a\u666f\uff0c\u4f7f\u7528\u6570\u636e\u9a8c\u8bc1\u5e93\uff08\u5982Pydantic\u6216Cerberus\uff09\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u957f\u5ea6\u9650\u5236\u548c\u9a8c\u8bc1\u89c4\u5219\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u5b9a\u4e49\u53d8\u91cf\u957f\u5ea6\u7684\u65b9\u6cd5\u5305\u62ec\u3001\u52a8\u6001\u7c7b\u578b\u3001\u5217\u8868\u548c\u5b57\u5178\u7b49\u6570\u636e\u7ed3\u6784\u3002 \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u4f7f\u7528\u52a8\u6001\u7c7b\u578b\u548c [&hellip;]","protected":false},"author":3,"featured_media":1151033,"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\/1151027"}],"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=1151027"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151027\/revisions"}],"predecessor-version":[{"id":1151034,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151027\/revisions\/1151034"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1151033"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1151027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1151027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1151027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}