{"id":192,"date":"2023-10-26T16:26:39","date_gmt":"2023-10-26T08:26:39","guid":{"rendered":"http:\/\/ai.gitpp.com\/?p=192"},"modified":"2023-10-30T16:02:10","modified_gmt":"2023-10-30T08:02:10","slug":"3-%e9%9d%9e%e6%ad%a3%e5%bc%8f%e4%bb%8b%e7%bb%8dpython","status":"publish","type":"post","link":"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/3-%e9%9d%9e%e6%ad%a3%e5%bc%8f%e4%bb%8b%e7%bb%8dpython\/","title":{"rendered":"\u300aPython\u5165\u95e8\u6559\u7a0b\u300b3. \u975e\u6b63\u5f0f\u4ecb\u7ecdPython"},"content":{"rendered":"\n<ol class=\"wp-block-list\">\n<li>\u5f00\u80c3\u83dc<\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/2-%e4%bd%bf%e7%94%a8-python-%e8%a7%a3%e9%87%8a%e5%99%a8\/\">\u4f7f\u7528 Python \u89e3\u91ca\u5668<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/3-%e9%9d%9e%e6%ad%a3%e5%bc%8f%e4%bb%8b%e7%bb%8dpython\/\">\u975e\u6b63\u5f0f\u4ecb\u7ecdPython<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/4-%e6%b7%b1%e5%85%a5%e6%b5%81%e7%a8%8b%e6%8e%a7%e5%88%b6\/\">\u6df1\u5165\u6d41\u7a0b\u63a7\u5236<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/5-%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84\/\">\u6570\u636e\u7ed3\u6784<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/6-%e6%a8%a1%e5%9d%97\/\">\u6a21\u5757<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/7-%e8%be%93%e5%85%a5%e5%92%8c%e8%be%93%e5%87%ba\/\">\u8f93\u5165\u548c\u8f93\u51fa<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/8-%e9%94%99%e8%af%af%e5%92%8c%e5%bc%82%e5%b8%b8\/\">\u9519\u8bef\u548c\u5f02\u5e38<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/9-%e7%b1%bb\/\">\u7c7b<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/10-%e6%a0%87%e5%87%86%e5%ba%93%e7%9a%84%e7%ae%80%e6%98%8e%e4%bb%8b%e7%bb%8d\/\">\u6807\u51c6\u5e93\u7684\u7b80\u660e\u4ecb\u7ecd<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/11-%e6%a0%87%e5%87%86%e5%ba%93%e7%9a%84%e7%ae%80%e6%98%8e%e4%bb%8b%e7%bb%8d%ef%bc%88%e7%ac%ac%e4%ba%8c%e9%83%a8%e5%88%86%ef%bc%89\/\">\u6807\u51c6\u5e93\u7684\u7b80\u660e\u4ecb\u7ecd\uff08\u7b2c\u4e8c\u90e8\u5206\uff09<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/12-%e7%8e%b0%e5%9c%a8%e5%b9%b2%e4%bb%80%e4%b9%88\/\">\u73b0\u5728\u5e72\u4ec0\u4e48?<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/13-%e4%ba%a4%e4%ba%92%e5%bc%8f%e8%be%93%e5%85%a5%e7%bc%96%e8%be%91%e5%8f%8a%e5%8e%86%e5%8f%b2%e6%9b%bf%e4%bb%a3\/\">\u4ea4\u4e92\u5f0f\u8f93\u5165\u7f16\u8f91\u53ca\u5386\u53f2\u66ff\u4ee3<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/14-%e6%b5%ae%e7%82%b9%e7%ae%97%e6%9c%af-%e9%97%ae%e9%a2%98%e5%92%8c%e9%99%90%e5%88%b6\/\">\u6d6e\u70b9\u7b97\u672f: \u95ee\u9898\u548c\u9650\u5236<\/a><\/li>\n<\/ol>\n\n\n\n<p>\u672c\u7ae0\u7684pdf \u7248\u672c\u4e0b\u8f7d: Python_Tutorial_zh-cn_liam0205.pdf<\/p>\n\n\n\n<p>\u5728\u4ee5\u4e0b\u7684\u4f8b\u5b50\u4e2d, \u8f93\u5165\u548c\u8f93\u51fa\u901a\u8fc7\u63d0\u793a\u7b26 (<code>&gt;&gt;&gt;<\/code>&nbsp;\u548c&nbsp;<code>...<\/code>) \u6765\u533a\u5206: \u8981\u8fd0\u884c\u793a\u4f8b, \u4f60\u5fc5\u987b\u5728\u63d0\u793a\u7b26\u51fa\u73b0\u540e\u952e\u5165\u63d0\u793a\u7b26\u540e\u9762\u7684\u6240\u6709\u5185\u5bb9; \u4e0d\u4ee5\u63d0\u793a\u7b26\u5f00\u5934\u7684\u884c\u662f\u89e3\u91ca\u5668\u7684\u8f93\u51fa.<\/p>\n\n\n\n<p>\u6ce8\u610f, \u5728\u4f8b\u5b50\u4e2d\u6709\u4ee5\u4e00\u4e2a\u6b21\u63d0\u793a\u7b26\u72ec\u5360\u4e00\u884c\u65f6\u610f\u5473\u7740\u4f60\u5fc5\u987b\u52a0\u5165\u4e00\u4e2a\u7a7a\u884c; \u7528\u6765\u8868\u793a\u591a\u884c\u547d\u4ee4\u7684\u7ed3\u675f.<\/p>\n\n\n\n<p>\u8fd9\u91cc\u6709\u8bb8\u591a\u4f8b\u5b50, \u90fd\u662f\u5728\u4ea4\u4e92\u5f0f\u7684\u63d0\u793a\u7b26\u540e\u4e2d\u8f93\u5165, \u5305\u62ec\u6ce8\u91ca. Python \u4e2d\u7684\u6ce8\u91ca\u4ee5\u4e00\u4e2a\u4e95\u53f7,&nbsp;<code>#<\/code>&nbsp;\u5f00\u5934, \u4e00\u76f4\u5ef6\u4f38\u5230\u8be5\u7269\u7406\u884c\u7684\u6700\u540e. \u6ce8\u91ca\u65e2\u53ef\u4ee5\u51fa\u73b0\u5728\u4e00\u884c\u7684\u5f00\u5934,\u4e5f\u53ef\u4ee5\u8ddf\u7740\u7a7a\u767d\u6216\u4ee3\u7801\u540e\u9762, \u4f46\u4e0d\u80fd\u5728\u5b57\u7b26\u4e32\u91cc\u9762. \u5728\u5b57\u7b26\u4e32\u91cc\u9762\u7684\u4e95\u53f7\u53ea\u662f\u4e00\u4e2a\u4e95\u53f7\u5b57\u7b26. \u56e0\u4e3a\u6ce8\u91ca\u4f7f\u7528\u6765\u4f7f\u4ee3\u7801\u6e05\u6670\u7684, \u800c\u4e0d\u4f1a\u88ab Python \u89e3\u91ca, \u6240\u4ee5\u5728\u952e\u5165\u4f8b\u5b50\u662f\u53ef\u4ee5\u7701\u7565\u5b83\u4eec.<\/p>\n\n\n\n<p>\u4f8b\u5982:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><em># \u8fd9\u662f\u7b2c\u4e00\u4e2a\u6ce8\u91ca<\/em> <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">SPAM = 1      <em># \u8fd9\u662f\u7b2c\u4e8c\u4e2a\u6ce8\u91ca<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">                      <em># &#8230; \u800c\u73b0\u5728\u662f\u7b2c\u4e09\u4e2a!<\/em> <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">STRING = &#8220;# \u8fd9\u4e0d\u662f\u6ce8\u91ca.&#8221;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3.1. \u628a Python \u5f53\u8ba1\u7b97\u5668\u4f7f\u7528<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/introduction.html#tut-calculator\"><\/a><\/h2>\n\n\n\n<p>\u8ba9\u6211\u4eec\u5c1d\u8bd5\u4e00\u4e9b\u7b80\u5355\u7684 Python \u547d\u4ee4. \u6253\u5f00\u89e3\u91ca\u5668, \u7b49\u5f85\u4e3b\u63d0\u793a\u7b26,&nbsp;<code>&gt;&gt;&gt;<\/code>, \u51fa\u73b0. (\u8fd9\u4e0d\u4f1a\u5f88\u4e45)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.1.1. \u6570\u503c<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/introduction.html#tut-numbers\"><\/a><\/h3>\n\n\n\n<p>\u89e3\u91ca\u5668\u53ef\u4f5c\u4e3a\u7b80\u5355\u7684\u8ba1\u7b97\u5668: \u8f93\u5165\u8868\u8fbe\u5f0f\u7ed9\u5b83, \u5b83\u5c06\u8f93\u51fa\u8868\u8fbe\u5f0f\u7684\u503c. \u8868\u8fbe\u5f0f\u8bed\u6cd5\u5f88\u76f4\u767d: \u64cd\u4f5c\u7b26\u00a0<code>+<\/code>,\u00a0<code>-<\/code>,\u00a0<code>*<\/code>,\u00a0<code>\/<\/code>\u00a0\u5c31\u50cf\u5927\u591a\u6570\u8bed\u8a00\u4e00\u6837\u5de5\u4f5c (\u4f8b\u5982, Pasal \u548c C); \u5706\u62ec\u53f7\u7528\u4ee5\u5206\u7ec4. \u4f8b\u5982:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>2+2 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">4 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong><em># \u8fd9\u662f\u6ce8\u91ca<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>&#8230; <\/strong>2+2 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">4 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>2+2    <em>#   \u4ee3\u7801\u540c\u4e00\u884c\u7684\u6ce8\u91ca<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> 4 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>(50-5*6)\/4 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">5.0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong>8\/5 <em># \u6574\u6570\u76f8\u9664\u65f6\u5e76\u4e0d\u4f1a\u4e22\u5931\u5c0f\u6570\u90e8\u5206<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> 1.6<\/p>\n\n\n\n<p>\u6ce8\u610f: \u5728\u4f60\u90a3\u513f,\u53ef\u80fd\u7ed3\u679c\u5e76\u4e0d\u5b8c\u5168\u76f8\u540c;\u56e0\u4e3a\u4e0d\u540c\u673a\u5668\u4e0a\u7684\u6d6e\u70b9\u6570\u7ed3\u679c\u53ef\u80fd\u4e0d\u540c \u5f85\u4f1a\u6211\u4eec\u4f1a\u8bb2\u5982\u4f55\u63a7\u5236\u6d6e\u70b9\u6570\u8f93\u51fa\u5730\u663e\u793a. \u5177\u4f53\u53c2\u89c1&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/floatingpoint.html#tut-fp-issues\">\u6d6e\u70b9\u7b97\u672f: \u95ee\u9898\u548c\u9650\u5236<\/a>&nbsp;\u4e2d\u5173\u4e8e\u6d6e\u70b9\u6570\u7684\u7ec6\u8282\u548c\u8868\u793a\u6cd5\u7684\u5b8c\u6574\u8ba8\u8bba.<\/p>\n\n\n\n<p>\u8981\u4ece\u6574\u6570\u76f8\u9664\u4e2d\u5f97\u5230\u4e00\u4e2a\u6574\u6570, \u4e22\u5f03\u4efb\u4f55\u5c0f\u6570\u90e8\u5206, \u53ef\u4ee5\u4f7f\u7528\u53e6\u4e00\u4e2a\u64cd\u4f5c\u7b26,\u00a0<code>\/\/<\/code>:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong><em># \u6574\u6570\u76f8\u9664\u8fd4\u56de\u5730\u677f\u6570:<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>&#8230; <\/strong>7\/\/3 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">2 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>7\/\/-3 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">-3<\/p>\n\n\n\n<p>\u7b49\u53f7 (<code>'='<\/code>) \u7528\u4e8e\u628a\u4e00\u4e2a\u503c\u5206\u914d\u7ed9\u4e00\u4e2a\u53d8\u91cf. \u5176\u540e\u4e0d\u4f1a\u8f93\u51fa\u4efb\u4f55\u7ed3\u679c,\u800c\u662f\u4e0b\u4e00\u4e2a\u4ea4\u4e92\u63d0\u793a\u7b26:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>width = 20<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong>height = 5*9 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>width * height<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> 900<\/p>\n\n\n\n<p>\u4e00\u4e2a\u503c\u53ef\u4ee5\u540c\u65f6\u88ab\u8d4b\u7ed9\u591a\u4e2a\u53d8\u91cf:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>x = y = z = 0 <em># \u7ed9 x, y \u548c z \u8d4b\u503c 0<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>x <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong>y <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">0 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>z <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">0<\/p>\n\n\n\n<p>\u53d8\u91cf\u5728\u4f7f\u7528\u4e4b\u524d\u5fc5\u987b\u8981\u88ab \u201c\u5b9a\u4e49\u201d (\u5206\u914d\u4e00\u4e2a\u503c), \u5426\u5219\u4f1a\u4ea7\u751f\u4e00\u4e2a\u9519\u8bef:<\/p>\n\n\n\n<p><strong>>>> <\/strong><em># \u5c1d\u8bd5\u8bbf\u95ee\u672a\u5b9a\u4e49\u7684\u53d8\u91cf<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>&#8230; <\/strong>n <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">Traceback (most recent call last): <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">File &#8220;&lt;stdin>&#8221;, line 1, in &lt;module><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> NameError: name &#8216;n&#8217; is not defined<\/p>\n\n\n\n<p>Python \u5b8c\u5168\u652f\u6301\u6d6e\u70b9\u6570; \u5728\u6df7\u5408\u8ba1\u7b97\u65f6,Pyhton \u4f1a\u628a\u6574\u578b\u8f6c\u6362\u6210\u4e3a\u6d6e\u70b9\u6570:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>3 * 3.75 \/ 1.5 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">7.5 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>7.0 \/ 2 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">3.5<\/p>\n\n\n\n<p>\u590d\u6570\u4e5f\u6709\u652f\u6301; \u865a\u6570\u90e8\u5206\u5199\u5f97\u65f6\u5019\u8981\u52a0\u4e0a\u540e\u7f00,\u00a0<code>j<\/code>\u00a0\u6216\u00a0<code>i<\/code>. \u5b9e\u90e8\u975e\u96f6\u7684\u590d\u6570\u88ab\u5199\u4f5c\u00a0<code>(real+imagj)<\/code>, \u4e5f\u53ef\u4ee5\u901a\u8fc7\u51fd\u5f0f\u00a0<code>complex(real,\u00a0imag)<\/code>\u00a0\u751f\u6210.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>1j * 1J<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> (-1+0j) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>1j * complex(0, 1) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">(-1+0j) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>3+1j*3 (3+3j) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>(3+1j)*3 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">(9+3j) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>(1+2j)\/(1+1j)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> (1.5+0.5j)<\/p>\n\n\n\n<p>\u590d\u6570\u603b\u662f\u8868\u8fbe\u4e3a\u4e24\u4e2a\u6d6e\u70b9\u6570, \u5b9e\u90e8\u548c\u865a\u90e8. \u8981\u4ece\u590d\u6570\u00a0<em>z<\/em>\u00a0\u4e2d\u62bd\u53d6\u8fd9\u4e9b\u90e8\u5206,\u4f7f\u7528\u00a0<code>z.real<\/code>\u00a0\u548c\u00a0<code>z.imag<\/code><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a=1.5+0.5j <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a.real<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> 1.5<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong>a.imag <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">0.5<\/p>\n\n\n\n<p>\u6d6e\u70b9\u6570\u548c\u6574\u6570\u7684\u8f6c\u6362\u51fd\u5f0f (<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#float\"><code>float()<\/code><\/a>,\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#int\"><code>int()<\/code><\/a>) \u4e0d\u80fd\u7528\u4e8e\u590d\u6570 \u2014 \u6ca1\u6709\u6b63\u786e\u7684\u65b9\u6cd5\u80fd\u628a\u4e00\u4e2a\u590d\u6570\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b9e\u6570. \u4f7f\u7528\u00a0<code>abs(z)<\/code>\u00a0\u5f97\u5230\u5b83\u7684\u6a21 (\u4ee5\u4e00\u4e2a\u6d6e\u70b9\u6570), \u4f7f\u7528\u00a0<code>z.real<\/code>\u00a0\u5f97\u5230\u4ed6\u7684\u5b9e\u90e8:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a=3.0+4.0j <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>float(a) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">Traceback (most recent call last): <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">File &#8220;&lt;stdin>&#8221;, line 1, in ? <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">TypeError: can&#8217;t convert complex to float; use abs(z) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a.real <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">3.0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong>a.imag <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">4.0 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>abs(a)   <em># sqrt(a.real**2 + a.imag**2)<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> 5.0<\/p>\n\n\n\n<p>\u5728\u4ea4\u4e92\u6a21\u5f0f\u4e0b, \u6700\u540e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u503c\u88ab\u5206\u914d\u7ed9\u53d8\u91cf\u00a0<code>_<\/code>. \u8fd9\u610f\u5473\u7740\u5f53\u4f60\u628a Python \u7528\u4f5c\u684c\u9762\u8ba1\u7b97\u5668\u65f6, \u53ef\u4ee5\u65b9\u4fbf\u7684\u8fdb\u884c\u8fde\u7eed\u8ba1\u7b97, \u4f8b\u5982:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>tax = 12.5 \/ 100<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong>price = 100.50<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>price * tax<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> 12.5625<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong>price + _ 113.0625 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>round(_, 2) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">113.06<\/p>\n\n\n\n<p>\u6211\u4eec\u5e94\u8be5\u5c06\u8fd9\u8fd9\u4e2a\u53d8\u91cf\u89c6\u4f5c\u53ea\u8bfb\u7684. \u4e0d\u8981\u8bd5\u56fe\u7ed9\u5b83\u8d4b\u503c \u2014 \u5426\u5219\u4f60\u4f1a\u521b\u5efa\u4e00\u4e2a\u540c\u540d\u7684\u5c40\u90e8\u53d8\u91cf, \u800c\u9690\u85cf\u539f\u672c\u5185\u7f6e\u53d8\u91cf\u7684\u9b54\u672f\u6548\u679c.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.1.2. \u5b57\u7b26\u4e32<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/introduction.html#tut-strings\"><\/a><\/h3>\n\n\n\n<p>\u9664\u4e86\u6570\u5b57, Python \u4e5f\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u5f0f\u6765\u64cd\u4f5c\u5b57\u7b26\u4e32. \u5b57\u7b26\u4e32\u662f\u7528\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\u5305\u88f9\u8d77\u6765\u7684:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>&#8216;spam eggs&#8217;<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8216;spam eggs&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>&#8216;doesn<strong>\\&#8217;<\/strong>t&#8217;<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8220;doesn&#8217;t&#8221;<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong>&#8220;doesn&#8217;t&#8221;<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8220;doesn&#8217;t&#8221; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>&#8216;&#8221;Yes,&#8221; he said.&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">&#8216;&#8221;Yes,&#8221; he said.&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>&#8220;<strong>\\&#8221;<\/strong>Yes,<strong>\\&#8221;<\/strong> he said.&#8221;<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">&#8216;&#8221;Yes,&#8221; he said.&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>&#8216;&#8221;Isn<strong>\\&#8217;<\/strong>t,&#8221; she said.&#8217;<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8216;&#8221;Isn\\&#8217;t,&#8221; she said.&#8217;<\/p>\n\n\n\n<p>\u89e3\u91ca\u5668\u4ee5\u5b57\u7b26\u4e32\u952e\u5165\u65f6\u76f8\u540c\u7684\u65b9\u5f0f\u6253\u5370\u5b83\u4eec: \u5728\u5f15\u53f7\u91cc\u9762, \u4f7f\u7528\u5f15\u53f7\u6216\u5176\u5b83\u4f7f\u7528\u53cd\u659c\u6760\u7684\u8f6c\u4e49\u5b57\u7b26\u4ee5\u8bf4\u660e\u7cbe\u786e\u7684\u503c. \u5f53\u5b57\u7b26\u4e32\u5305\u542b\u5355\u5f15\u53f7\u800c\u6ca1\u6709\u53cc\u5f15\u53f7\u65f6, \u5c31\u4f7f\u7528 \u53cc\u5f15\u53f7\u5305\u56f4\u5b83, \u5426\u5219, \u4f7f\u7528\u5355\u5f15\u53f7.&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#print\"><code>print()<\/code><\/a>&nbsp;\u51fd\u6570\u4e3a\u5982\u6b64\u7684\u5b57\u7b26\u4e32\u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u53ef\u8bfb\u7684\u8f93\u51fa.<\/p>\n\n\n\n<p>\u5b57\u7b26\u4e32\u6709\u51e0\u79cd\u65b9\u6cd5\u6765\u8de8\u8d8a\u591a\u884c. \u7ee7\u7eed\u884c\u53ef\u4ee5\u88ab\u4f7f\u7528, \u5728\u4e00\u884c\u6700\u540e\u52a0\u4e0a\u4e00\u4e2a\u53cd\u659c\u6760\u4ee5\u8868\u660e\u4e0b\u4e00\u884c\u662f\u8fd9\u884c\u7684\u903b\u8f91\u5ef6\u7eed:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">hello = &#8220;\u8fd9\u662f\u4e00\u4e2a\u76f8\u5f53\u957f\u7684\u5b57\u7b26\u4e32\u5305\u542b<strong>\\n\\<\/strong> <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u51e0\u884c\u6587\u672c, \u5c31\u50cf\u4f60\u5728 C \u91cc\u505a\u7684\u4e00\u6837.<strong>\\n\\<\/strong><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> \u6ce8\u610f\u5f00\u901a\u7684\u7a7a\u767d\u662f<strong>\\<\/strong> <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u6709\u610f\u4e49\u7684.&#8221; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">print(hello)<\/p>\n\n\n\n<p>\u6ce8\u610f, \u6362\u884c\u4f9d\u65e7\u9700\u8981\u5728\u5b57\u7b26\u4e32\u91cc\u5d4c\u5165\u00a0<code>\\n<\/code>\u00a0\u2013 \u5728\u540e\u9762\u7684\u53cd\u659c\u6760\u540e\u9762\u7684\u6362\u884c\u88ab\u4e22\u5f03\u4e86. \u8be5\u793a\u4f8b\u4f1a\u6253\u5370\u5982\u4e0b\u5185\u5bb9:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u8fd9\u662f\u4e00\u4e2a\u76f8\u5f53\u957f\u7684\u5b57\u7b26\u4e32\u5305\u542b<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> \u51e0\u884c\u6587\u672c, \u5c31\u50cf\u4f60\u5728 C \u91cc\u505a\u7684\u4e00\u6837.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> \u6ce8\u610f\u5f00\u901a\u7684\u7a7a\u767d\u662f \u6709\u610f\u4e49\u7684.<\/p>\n\n\n\n<p>\u53e6\u4e00\u79cd\u65b9\u6cd5, \u5b57\u7b26\u4e32\u53ef\u4ee5\u4f7f\u7528\u4e00\u5bf9\u5339\u914d\u7684\u4e09\u5f15\u53f7\u5bf9\u5305\u56f4:\u00a0<code>\"\"\"<\/code>\u00a0\u6216\u00a0<code>'''<\/code>. \u5f53\u4f7f\u7528\u4e09\u5f15\u53f7\u65f6, \u56de\u8f66\u4e0d\u9700\u8981\u88ab\u820d\u5f03, \u4ed6\u4eec\u4f1a\u5305\u542b\u5728\u5b57\u7b26\u4e32\u91cc. \u4e8e\u662f\u4e0b\u9762\u7684\u4f8b\u5b50\u4f7f\u7528\u4e86\u4e00\u4e2a\u53cd\u659c\u6760\u6765\u907f\u514d\u6700\u521d\u4e0d\u60f3\u8981\u7684\u7a7a\u884c.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">print(&#8220;&#8221;&#8221;<strong>\\<\/strong> <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u7528\u9014: thingy [OPTIONS] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">-h                            \u663e\u793a\u7528\u9014\u4fe1\u606f <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">-H hostname                    \u8fde\u63a5\u5230\u7684\u4e3b\u673a\u540d <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">&#8220;&#8221;&#8221;)<\/p>\n\n\n\n<p>\u4ea7\u751f\u5982\u4e0b\u8f93\u5165:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u7528\u9014: thingy [OPTIONS] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> -h                                     \u663e\u793a\u7528\u9014\u4fe1\u606f<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> -H hostname                    \u8fde\u63a5\u5230\u7684\u4e3b\u673a\u540d<\/p>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u628a\u5b57\u7b26\u4e32\u53d8\u4e3a\u4e00\u4e2a \u201c\u672a\u5904\u7406\u201d \u5b57\u7b26\u4e32,\u00a0<code>\\n<\/code>\u00a0\u5e8f\u5217\u4e0d\u4f1a\u8f6c\u4e49\u6210\u56de\u8f66, \u4f46\u662f\u884c\u672b\u7684\u53cd\u659c\u6760, \u4ee5\u53ca\u6e90\u4e2d\u7684\u56de\u8f66\u7b26, \u90fd\u4f1a\u5f53\u6210\u6570\u636e\u5305\u542b\u5728\u5b57\u7b26\u4e32\u91cc. \u56e0\u6b64, \u8fd9\u4e2a\u4f8b\u5b50: <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">hello = r&#8221;\u8fd9\u662f\u4e00\u4e2a\u76f8\u5f53\u957f\u7684\u5b57\u7b26\u4e32\u5305\u542b\\n<strong>\\<\/strong> <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u51e0\u884c\u6587\u672c, \u5c31\u50cf\u4f60\u5728 C \u91cc\u505a\u7684\u4e00\u6837.&#8221; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">print(hello)<\/p>\n\n\n\n<p>\u5c06\u4f1a\u6253\u5370:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u8fd9\u662f\u4e00\u4e2a\u76f8\u5f53\u957f\u7684\u5b57\u7b26\u4e32\u5305\u542b\\n\\ <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u51e0\u884c\u6587\u672c, \u5c31\u50cf\u4f60\u5728 C \u91cc\u505a\u7684\u4e00\u6837.<\/p>\n\n\n\n<p>\u5b57\u7b26\u4e32\u53ef\u4ee5\u4f7f\u7528\u00a0<code>+<\/code>\u00a0\u64cd\u4f5c\u7b26\u6765\u8fde\u63a5 (\u7c98\u5728\u4e00\u8d77), \u4f7f\u7528\u00a0<code>*<\/code>\u00a0\u64cd\u4f5c\u7b26\u91cd\u590d:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>word = &#8216;Help&#8217; + &#8216;A&#8217;<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong>word<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8216;HelpA&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>&#8216;&lt;&#8216; + word*5 + &#8216;>&#8217;<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8216;&lt;HelpAHelpAHelpAHelpAHelpA>&#8217;<\/p>\n\n\n\n<p>\u4e24\u4e2a\u9760\u7740\u4e00\u8d77\u7684\u5b57\u7b26\u4e32\u4f1a\u81ea\u52a8\u7684\u8fde\u63a5; \u4e0a\u9762\u4f8b\u5b50\u7684\u7b2c\u4e00\u884c\u4e5f\u53ef\u4ee5\u5199\u6210\u00a0<code>word\u00a0=\u00a0'Help'\u00a0'A'<\/code>; \u8fd9\u53ea\u80fd\u7528\u4e8e\u4e24\u4e2a\u5b57\u7b26\u4e32\u5e38\u91cf, \u800c\u4e0d\u80fd\u7528\u4e8e\u4efb\u610f\u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>&#8216;str&#8217; &#8216;ing&#8217; <em># &lt;- \u53ef\u4ee5<\/em> <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">&#8216;string&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>&#8216;str&#8217;.strip() + &#8216;ing&#8217; <em># &lt;- \u53ef\u4ee5<\/em> <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">&#8216;string&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>&#8216;str&#8217;.strip() &#8216;ing&#8217; <em># &lt;- \u4e0d\u6b63\u786e<\/em> <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">File &#8220;&lt;stdin>&#8221;, line 1, in ?<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8216;str&#8217;.strip() &#8216;ing&#8217; ^ <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">SyntaxError: invalid syntax<\/p>\n\n\n\n<p>\u5b57\u7b26\u4e32\u53ef\u4ee5\u4f7f\u7528\u4e0b\u6807 (\u7d22\u5f15); \u5c31\u50cf C \u4e00\u6837, \u5b57\u7b26\u4e32\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u7684\u4e0b\u6807 (\u7d22\u5f15) \u4e3a 0. \u6ca1\u6709\u72ec\u7acb\u7684\u5b57\u7b26\u4e32\u7c7b\u578b; \u4e00\u4e2a\u5b57\u7b26\u4e32\u5c31\u662f\u4e00\u4e2a\u5927\u5c0f\u4e3a\u4e00\u7684\u5b57\u7b26\u4e32. \u5c31\u50cf Icon \u7a0b\u5e8f\u8bed\u8a00\u4e00\u6837, \u5b50\u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7*\u5207\u7247\u7b26\u53f7*\u6307\u5b9a: \u5192\u53f7\u5206\u9694\u7684\u4e24\u4e2a\u7d22\u5f15.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>word[4] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">&#8216;A&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>word[0:2]<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8216;He&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>word[2:4]<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8216;lp&#8217;<\/p>\n\n\n\n<p>\u5207\u7247\u7d22\u5f15\u6709\u4e00\u4e9b\u6709\u7528\u7684\u9ed8\u8ba4\u503c; \u7701\u7565\u7684\u7b2c\u4e00\u4e2a\u7d22\u5f15\u9ed8\u8ba4\u4e3a\u96f6, \u7701\u7565\u7684\u7b2c\u4e8c\u4e2a\u7d22\u5f15\u9ed8\u8ba4\u4e3a\u5b57\u7b26\u4e32\u7684\u5927\u5c0f.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>word[:2] <em># \u5934\u4e24\u4e2a\u5b57\u7b26<\/em> <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">&#8216;He&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>word[2:] <em># \u9664\u4e86\u5934\u4e24\u4e2a\u5b57\u7b26<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8216;lpA&#8217;<\/p>\n\n\n\n<p>\u4e0e C \u5b57\u7b26\u4e32\u4e0d\u4e00\u6837, Python \u5b57\u7b26\u4e32\u4e0d\u53ef\u4ee5\u6539\u53d8. \u7ed9\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\u4f4d\u7f6e\u8d4b\u503c\u4f1a\u4ea7\u751f\u4e00\u4e2a\u9519\u8bef:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>word[0] = &#8216;x&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">Traceback (most recent call last):<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> File &#8220;&lt;stdin>&#8221;, line 1, in ?<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> TypeError: &#8216;str&#8217; object does not support item assignment<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong>word[:1] = &#8216;Splat&#8217;<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> Traceback (most recent call last): <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">File &#8220;&lt;stdin>&#8221;, line 1, in ?<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> TypeError: &#8216;str&#8217; object does not support slice assignment<\/p>\n\n\n\n<p>\u7136\u800c, \u4f7f\u7528\u5185\u5bb9\u7ec4\u5408\u521b\u5efa\u65b0\u5b57\u7b26\u4e32\u662f\u7b80\u5355\u548c\u6709\u6548\u7684:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>&#8216;x&#8217; + word[1:]<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8216;xelpA&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>&#8216;Splat&#8217; + word[4] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">&#8216;SplatA&#8217;<\/p>\n\n\n\n<p>\u8fd9\u6709\u4e00\u4e2a\u6709\u7528\u7684\u5207\u7247\u64cd\u4f5c\u7684\u6052\u7b49\u5f0f:\u00a0<code>s[:i]\u00a0+\u00a0s[i:]<\/code>\u00a0\u7b49\u4e8e\u00a0<code>s<\/code>.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>word[:2] + word[2:]<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8216;HelpA&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>word[:3] + word[3:] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">&#8216;HelpA&#8217;<\/p>\n\n\n\n<p>\u9000\u5316\u7684\u5207\u7247\u7d22\u5f15\u88ab\u5904\u7406\u5730\u5f88\u4f18\u96c5: \u592a\u5927\u7684\u7d22\u5f15\u4f1a\u88ab\u5b57\u7b26\u4e32\u5927\u5c0f\u6240\u4ee3\u66ff, \u4e0a\u754c\u6bd4\u4e0b\u754c\u5c0f\u5c31\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>word[1:100] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">&#8216;elpA&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>word[10:] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">&#8221; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>word[2:1]<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8221;<\/p>\n\n\n\n<p>\u7d22\u5f15\u53ef\u4ee5\u662f\u8d1f\u6570, \u90a3\u6837\u5c31\u4f1a\u4ece\u53f3\u8fb9\u5f00\u59cb\u7b97\u8d77. \u4f8b\u5982:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>word[-1] <em># \u6700\u540e\u4e00\u4e2a\u5b57\u7b26<\/em> <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">&#8216;A&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>word[-2] <em># \u5012\u6570\u7b2c\u4e8c\u4e2a\u5b57\u7b26<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8216;p&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>word[-2:] <em># \u6700\u540e\u4e24\u4e2a\u5b57\u7b26<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8216;pA&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>word[:-2] <em># \u9664\u6700\u540e\u4e24\u4e2a\u5b57\u7b26\u7684\u5176\u4ed6\u5b57\u7b26<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8216;Hel&#8217;<\/p>\n\n\n\n<p>\u4f46\u662f\u8981\u6ce8\u610f, -0 \u4e0e 0 \u662f\u5b8c\u5168\u4e00\u6837\u7684, \u56e0\u6b64\u5b83\u4e0d\u4f1a\u4ece\u53f3\u8fb9\u5f00\u59cb\u6570!<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>word[-0] <em># (\u56e0\u4e3a -0 \u7b49\u4e8e 0)<\/em> <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">&#8216;H&#8217;<\/p>\n\n\n\n<p>\u8d8a\u754c\u7684\u8d1f\u5207\u7247\u7d22\u5f15\u4f1a\u88ab\u622a\u65ad, \u5f53\u4e0d\u8981\u5bf9\u5355\u5143\u7d20 (\u975e\u5207\u7247) \u7d22\u5f15\u4f7f\u7528\u8d8a\u754c\u7d22\u5f15.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>word[-100:] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">&#8216;HelpA&#8217;<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong>word[-10]    <em># \u9519\u8bef<\/em> <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">Traceback (most recent call last): <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">File &#8220;&lt;stdin>&#8221;, line 1, in ? <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">IndexError: string index out of range<\/p>\n\n\n\n<p>\u8bb0\u5fc6\u5207\u7247\u5de5\u4f5c\u65b9\u5f0f\u7684\u4e00\u4e2a\u65b9\u6cd5\u662f\u628a\u7d22\u5f15\u770b\u4f5c\u662f\u5b57\u7b26*\u4e4b\u95f4*\u7684\u70b9, \u7b2c\u4e00\u5b57\u7b26\u7684\u5de6\u8fb9\u8bb0\u4f5c 0. \u5305\u542b\u00a0<em>n<\/em>\u00a0\u4e2a\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u7684\u53f3\u8fb9\u7684\u7d22\u5f15\u5c31\u662f\u00a0<em>n<\/em>, \u4f8b\u5982: <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"212\" height=\"95\" src=\"http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-4.png\" alt=\"\" class=\"wp-image-319\"\/><\/figure>\n\n\n\n<p>\u7b2c\u4e00\u884c\u7ed9\u51fa\u4e86\u7d22\u5f15 0&#8230;5 \u5728\u5b57\u7b26\u4e32\u91cc\u7684\u4f4d\u7f6e; \u7b2c\u4e8c\u884c\u7ed9\u51fa\u4e86\u76f8\u5e94\u7684\u8d1f\u7d22\u5f15.&nbsp;<em>i<\/em>&nbsp;\u5230&nbsp;<em>j<\/em>&nbsp;\u7684\u5207\u7247\u7531\u6807\u53f7\u4e3a&nbsp;<em>i<\/em>&nbsp;\u548c&nbsp;<em>j<\/em>&nbsp;\u7684\u8fb9\u7f18\u4e2d\u95f4\u7684\u5b57\u7b26\u6240\u6784\u6210.<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u6ca1\u6709\u8d8a\u754c\u975e\u8d1f\u7d22\u5f15, \u5207\u7247\u7684\u957f\u5ea6\u5c31\u662f\u4e24\u4e2a\u7d22\u5f15\u4e4b\u5dee. \u4f8b\u5982,&nbsp;<code>word[1:3]<\/code>&nbsp;\u7684\u957f\u5ea6\u662f 2.<\/p>\n\n\n\n<p>\u5185\u5efa\u51fd\u6570\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#len\"><code>len()<\/code><\/a>\u00a0\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u957f\u5ea6:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>s = &#8216;supercalifragilisticexpialidocious&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>len(s) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">34<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">See also<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#typesseq\">Sequence Types \u2014 str, bytes, bytearray, list, tuple, range<\/a><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u5b57\u7b26\u4e32\u662f*\u5e8f\u5217\u7c7b\u578b*\u7684\u4f8b\u5b50, \u652f\u6301\u8be5\u7c7b\u578b\u7684\u4e00\u822c\u64cd\u4f5c.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#string-methods\">String Methods<\/a><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u5b57\u7b26\u4e32\u652f\u6301\u5927\u91cf\u7528\u4e0e\u57fa\u672c\u53d8\u6362\u548c\u641c\u7d22\u7684\u65b9\u6cd5.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/string.html#string-formatting\">String Formatting<\/a><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u5728\u8fd9\u63cf\u8ff0\u4e86\u4f7f\u7528\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#str.format\"><code>str.format()<\/code><\/a>\u00a0\u683c\u5f0f\u5b57\u7b26\u4e32\u7684\u4fe1\u606f.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#old-string-formatting\">Old String Formatting Operations<\/a><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u5f53\u5b57\u7b26\u4e32\u548c Unicode \u5b57\u7b26\u4e32\u4e3a\u00a0<code>%<\/code>\u00a0\u64cd\u4f5c\u7b26\u7684\u5de6\u64cd\u4f5c\u6570\u65f6, \u8001\u7684\u683c\u5f0f\u64cd\u4f5c\u5c31\u4f1a\u88ab\u8c03\u7528, \u5728\u8fd9\u91cc\u63cf\u8ff0\u4e86\u66f4\u591a\u7ec6\u8282.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.1.3. \u5173\u4e8e Unicode<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/introduction.html#unicode\"><\/a><\/h3>\n\n\n\n<p>\u81ea Python 3.0 \u5f00\u59cb, \u6240\u6709\u5b57\u7b26\u4e32\u90fd\u652f\u6301 Unicode (\u53c2\u89c1&nbsp;<a href=\"http:\/\/www.unicode.org\/\">http:\/\/www.unicode.org\/<\/a>). Unicode \u7684\u76ca\u5904\u5728\u4e8e\u5b83\u4e3a\u81ea\u53e4\u81f3\u4eca\u6240\u6709\u6587\u672c\u4e2d\u4f7f\u7528\u7684\u6bcf\u4e2a\u5b57\u7b26\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5e8f\u53f7. \u5728\u4ee5\u524d, \u53ea\u6709 256 \u4e2a\u5e8f\u53f7\u8868\u793a\u6587\u5b57\u5b57\u7b26. \u4e00\u822c\u5730, \u6587\u672c\u88ab\u4e00\u4e2a\u6620\u5c04\u5e8f\u53f7\u5230\u6587\u672c\u5b57\u7b26\u7684\u7f16\u7801\u9875\u6240\u9650\u5236. \u5c24\u5176\u5728\u8f6f\u4ef6\u7684\u56fd\u9645\u5316 (internationalization, \u901a\u5e38\u88ab\u5199\u4f5c&nbsp;<code>i18n<\/code>&nbsp;\u2014&nbsp;<code>'i'<\/code>&nbsp;+ 18 \u4e2a\u5b57\u7b26 +&nbsp;<code>'n'<\/code>) \u65f6\u5c24\u5176\u6df7\u4e71. Unicode \u901a\u8fc7\u4e3a\u6240\u6709\u6587\u672c\u5b9a\u4e49\u4e00\u4e2a\u7f16\u7801\u9875\u89e3\u51b3\u4e86\u8fd9\u4e9b\u96be\u9898.<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u60f3\u5728\u5b57\u7b26\u4e32\u91cc\u52a0\u5165\u7279\u6b8a\u5b57\u7b26, \u53ef\u4ee5\u4f7f\u7528\u00a0<em>Unicode-Escape<\/em>\u00a0\u7f16\u7801. \u4e0b\u9762\u7684\u4f8b\u5b50\u8bf4\u660e\u4e86\u5982\u4f55\u505a\u5230\u8fd9\u70b9:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>&#8216;Hello<strong>\\u0020<\/strong>World !&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">&#8216;Hello World !&#8217;<\/p>\n\n\n\n<p>\u8f6c\u4e49\u5e8f\u5217&nbsp;<code>\\u0020<\/code>&nbsp;\u8868\u660e\u5728\u7ed9\u51fa\u7684\u4f4d\u7f6e, \u4f7f\u7528\u5e8f\u53f7\u503c 0x0020 (\u7a7a\u683c\u5b57\u7b26), \u63d2\u5165\u8fd9\u4e2a Unicode \u5b57\u7b26.<\/p>\n\n\n\n<p>\u5176\u5b83\u5b57\u7b26\u901a\u8fc7\u76f4\u63a5\u5730\u4f7f\u7528\u5b83\u4eec\u5404\u81ea\u7684\u5e8f\u53f7\u503c\u4f5c\u4e3a Unicode \u5e8f\u53f7\u800c\u88ab\u89e3\u91ca. \u5982\u679c\u4f60\u6709\u4f7f\u7528\u6807\u51c6 Latin-1 \u7f16\u7801, \u5728\u5f88\u591a\u897f\u65b9\u56fd\u5bb6\u91cc\u4f7f\u7528, \u7684\u5b57\u7b26\u4e32, \u4f60\u4f1a\u65b9\u4fbf\u5730\u53d1\u73b0 Unicode \u7684\u524d 256 \u4e2a\u5b57\u7b26\u4e0e Latin-1 \u7684\u4e00\u6837.<\/p>\n\n\n\n<p>\u9664\u8fd9\u4e9b\u6807\u51c6\u7f16\u7801\u4ee5\u5916, Python \u8fd8\u63d0\u4f9b\u4e86\u6574\u5957\u5176\u5b83\u65b9\u6cd5, \u901a\u8fc7\u4e00\u4e2a\u5df2\u77e5\u7f16\u7801\u7684\u57fa\u7840\u6765\u521b\u5efa Unicode \u5b57\u7b26\u4e32.<\/p>\n\n\n\n<p>\u5b57\u7b26\u4e32\u5bf9\u8c61\u63d0\u4f9b\u4e86\u4e00\u4e2a\u00a0<code>encode()<\/code>\u00a0\u65b9\u6cd5, \u7528\u4e8e\u4f7f\u7528\u4e00\u4e2a\u7279\u6b8a\u7f16\u7801\u8f6c\u6362\u5b57\u7b26\u4e32\u5230\u5b57\u8282\u5e8f\u5217, \u8be5\u65b9\u6cd5\u5e26\u6709\u4e00\u4e2a\u53c2\u6570, \u7f16\u7801\u7684\u540d\u5b57. \u4f18\u5148\u9009\u62e9\u7f16\u7801\u7684\u5c0f\u5199\u540d\u5b57.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>&#8220;\u00c4pfel&#8221;.encode(&#8216;utf-8&#8217;) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">b&#8217;\\xc3\\x84pfel&#8217;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.1.4. \u5217\u8868<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/introduction.html#tut-lists\"><\/a><\/h3>\n\n\n\n<p>Python \u6709\u4e00\u4e9b*\u590d\u5408*\u6570\u636e\u7c7b\u578b, \u7528\u6765\u628a\u5176\u5b83\u503c\u5206\u7ec4. \u6700\u5168\u80fd\u7684\u5c31\u662f\u00a0<em>list<\/em>, \u5b83\u53ef\u4ee5\u5199\u4e3a\u5728\u65b9\u62ec\u53f7\u4e2d\u7684\u901a\u8fc7\u9017\u53f7\u5206\u9694\u7684\u4e00\u5217\u503c (\u9879). \u5217\u8868\u7684\u9879\u5e76\u4e0d\u9700\u8981\u662f\u540c\u4e00\u7c7b\u578b.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a = [&#8216;spam&#8217;, &#8216;eggs&#8217;, 100, 1234] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">[&#8216;spam&#8217;, &#8216;eggs&#8217;, 100, 1234]<\/p>\n\n\n\n<p>\u5c31\u50cf\u5b57\u7b26\u4e32\u7d22\u5f15, \u5217\u8868\u7684\u7d22\u5f15\u4ece 0 \u5f00\u59cb, \u5217\u8868\u4e5f\u53ef\u4ee5\u5207\u7247, \u8fde\u63a5\u7b49\u7b49:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a[0]<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> &#8216;spam&#8217; <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a[3] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">1234 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a[-2] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">100 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a[1:-1]<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> [&#8216;eggs&#8217;, 100]<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong>a[:2] + [&#8216;bacon&#8217;, 2*2]<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> [&#8216;spam&#8217;, &#8216;eggs&#8217;, &#8216;bacon&#8217;, 4] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>3*a[:3] + [&#8216;Boo!&#8217;]<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> [&#8216;spam&#8217;, &#8216;eggs&#8217;, 100, &#8216;spam&#8217;, &#8216;eggs&#8217;, 100, &#8216;spam&#8217;, &#8216;eggs&#8217;, 100, &#8216;Boo!&#8217;]<\/p>\n\n\n\n<p>\u6240\u6709\u7684\u5207\u7247\u64cd\u4f5c\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u8bf7\u6c42\u5143\u7d20\u7684\u65b0\u5217\u8868. \u8fd9\u610f\u5473\u7740, \u4e0b\u9762\u7684\u7684\u5207\u7247\u8fd4\u56de\u5217\u8868\u00a0<em>a<\/em>\u00a0\u7684\u4e00\u4e2a\u6d45\u590d\u5236:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a[:] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">[&#8216;spam&#8217;, &#8216;eggs&#8217;, 100, 1234]<\/p>\n\n\n\n<p>\u4e0d\u50cf*\u4e0d\u53ef\u53d8*\u7684\u5b57\u7b26\u4e32, \u6539\u53d8\u5217\u8868\u4e2d\u5355\u4e2a\u5143\u7d20\u662f\u53ef\u80fd\u7684.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> [&#8216;spam&#8217;, &#8216;eggs&#8217;, 100, 1234] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a[2] = a[2] + 23<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong>a <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">[&#8216;spam&#8217;, &#8216;eggs&#8217;, 123, 1234]<\/p>\n\n\n\n<p>\u4e3a\u5207\u7247\u8d4b\u503c\u540c\u6837\u53ef\u80fd, \u8fd9\u751a\u81f3\u80fd\u6539\u53d8\u5b57\u7b26\u4e32\u7684\u5927\u5c0f, \u6216\u8005\u5b8c\u5168\u7684\u6e05\u9664\u5b83:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong><em># \u66ff\u4ee3\u4e00\u4e9b\u9879:<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>&#8230; <\/strong>a[0:2] = [1, 12] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">[1, 12, 123, 1234] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong><em># \u79fb\u9664\u4e00\u4e9b:<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>&#8230; <\/strong>a[0:2] = [] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> [123, 1234]<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong><em># \u63d2\u5165\u4e00\u4e9b:<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>&#8230; <\/strong>a[1:1] = [&#8216;bletch&#8217;, &#8216;xyzzy&#8217;] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a [123, &#8216;bletch&#8217;, &#8216;xyzzy&#8217;, 1234] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong><em># \u5728\u5f00\u59cb\u5904\u63d2\u5165\u81ea\u8eab (\u7684\u4e00\u4e2a\u62f7\u8d1d)<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a[:0] = a <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">[123, &#8216;bletch&#8217;, &#8216;xyzzy&#8217;, 1234, 123, &#8216;bletch&#8217;, &#8216;xyzzy&#8217;, 1234]<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>>>> <\/strong><em># \u6e05\u9664\u5217\u8868: \u7528\u7a7a\u5217\u8868\u66ff\u4ee3\u6240\u6709\u7684\u9879<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a[:] = [] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> []<\/p>\n\n\n\n<p>\u5185\u5efa\u51fd\u6570\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#len\"><code>len()<\/code><\/a>\u00a0\u540c\u6837\u5bf9\u5217\u8868\u6709\u6548:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>a = [&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;, &#8216;d&#8217;] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>len(a)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> 4<\/p>\n\n\n\n<p>\u5d4c\u5957\u5217\u8868 (\u521b\u5efa\u5305\u542b\u5176\u5b83\u5217\u8868\u7684\u5217\u8868) \u662f\u53ef\u80fd\u7684, \u4f8b\u5982:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>q = [2, 3] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>p = [1, q, 4] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>len(p) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">3 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>p[1]<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> [2, 3] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>p[1][0]<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> 2<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5728\u5217\u8868\u672b\u5c3e\u52a0\u5165\u4e00\u4e9b\u4e1c\u897f:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>p[1].append(&#8216;xtra&#8217;) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>p<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> [1, [2, 3, &#8216;xtra&#8217;], 4] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong>q<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> [2, 3, &#8216;xtra&#8217;]<\/p>\n\n\n\n<p>\u6ce8\u610f\u5728\u6700\u540e\u7684\u4f8b\u5b50\u91cc,&nbsp;<code>p[1]<\/code>&nbsp;\u548c&nbsp;<code>q<\/code>&nbsp;\u786e\u5b9e\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61! \u6211\u4eec\u5728\u4ee5\u540e\u4f1a\u56de\u5230*\u5bf9\u8c61\u8bed\u4e49*.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3.2. \u7f16\u7a0b\u7b2c\u4e00\u6b65<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/introduction.html#tut-firststeps\"><\/a><\/h2>\n\n\n\n<p>\u5f53\u7136, \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Python \u505a\u6bd4 2 + 2 \u66f4\u590d\u6742\u7684\u4efb\u52a1. \u4f8b\u5982, \u6211\u4eec\u53ef\u4ee5\u5982\u4e0b\u7684\u5199\u51fa\u00a0<em>Fibonacci<\/em>\u00a0\u5e8f\u5217\u7684\u6700\u521d\u5b50\u5e8f\u5217:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> <\/strong><em># Fibonacci \u5e8f\u5217:<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>&#8230; <\/strong><em># \u4e24\u4e2a\u5143\u7d20\u7684\u503c\u5b9a\u4e49\u4e0b\u4e00\u4e2a<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>&#8230; <\/strong>a, b = 0, 1 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>> while<\/strong> b &lt; 10: <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>&#8230;<\/strong> print(b)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> <strong>&#8230;<\/strong> a, b = b, a+b <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>&#8230;<\/strong> 1  1  2  3  5  8<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4ecb\u7ecd\u4e86\u51e0\u4e2a\u65b0\u7279\u6027.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c\u4e00\u884c\u5305\u62ec\u4e00\u6b21*\u591a\u91cd\u8d4b\u503c*: \u53d8\u91cf&nbsp;<code>a<\/code>&nbsp;\u548c&nbsp;<code>b<\/code>&nbsp;\u540c\u65f6\u5730\u5f97\u5230\u65b0\u503c 0 \u548c 1. \u5728\u6700\u540e\u4e00\u884c\u53c8\u4f7f\u7528\u4e86\u4e00\u6b21, \u6f14\u793a\u4e86\u53f3\u8fb9\u7684\u8868\u8fbe\u5f0f\u5728\u4efb\u4f55\u8d4b\u503c\u4e4b\u524d\u5c31\u5df2\u7ecf\u88ab\u8ba1\u7b97\u4e86. \u53f3\u8fb9\u8868\u8fbe\u5f0f\u4ece\u5de6\u81f3\u53f3\u5730\u8ba1\u7b97.<\/li>\n\n\n\n<li>\u5f53\u6761\u4ef6 (\u5728\u8fd9\u91cc:&nbsp;<code>b&nbsp;&lt;&nbsp;10<\/code>) \u4fdd\u6301\u4e3a\u771f\u65f6,&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#while\"><code>while<\/code><\/a>&nbsp;\u5faa\u73af\u4f1a\u4e00\u76f4\u6267\u884c. \u5728 Python \u4e2d, \u5c31\u50cf C \u91cc\u4e00\u6837, \u4efb\u4f55\u975e\u96f6\u6574\u6570\u90fd\u4e3a\u771f; \u96f6\u4e3a\u5047. \u6761\u4ef6\u4e5f\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\u6216\u5217\u8868, \u5b9e\u9645\u4e0a\u53ef\u4ee5\u662f\u4efb\u610f\u5e8f\u5217; \u957f\u5ea6\u4e0d\u4e3a\u96f6\u65f6\u5c31\u4e3a\u771f, \u7a7a\u5e8f\u5217\u4e3a\u5047. \u672c\u4f8b\u4e2d\u4f7f\u7528\u7684\u6d4b\u8bd5\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6bd4\u8f83. \u6807\u51c6\u6bd4\u8f83\u7b26\u4e0e C \u4e2d\u5199\u5f97\u4e00\u6837:&nbsp;<code>&lt;<\/code>&nbsp;(\u5c0f\u4e8e),&nbsp;<code>&gt;<\/code>&nbsp;(\u5927\u4e8e),&nbsp;<code>==<\/code>&nbsp;(\u7b49\u4e8e),&nbsp;<code>&lt;=<\/code>&nbsp;(\u5c0f\u4e8e\u6216\u7b49\u4e8e),&nbsp;<code>&gt;=<\/code>&nbsp;(\u5927\u4e8e\u6216\u7b49\u4e8e) \u548c&nbsp;<code>!=<\/code>&nbsp;(\u4e0d\u7b49\u4e8e).<\/li>\n\n\n\n<li>\u5faa\u73af*\u4f53*\u662f*\u7f29\u8fdb*\u7684: \u7f29\u8fdb\u662f Python \u5206\u7ec4\u8bed\u53e5\u7684\u65b9\u6cd5. Python \u4e0d (\u5230\u76ee\u524d!) \u63d0\u4f9b\u667a\u80fd\u8f93\u5165\u884c\u7f16\u8f91\u529f\u80fd, \u56e0\u6b64, \u4f60\u9700\u8981\u4e3a\u6bcf\u4e2a\u7f29\u8fdb\u952e\u5165\u5236\u8868\u7b26\u6216\u7a7a\u683c. \u5728\u7ec3\u4e60\u4e2d, \u4f60\u4f1a\u4f7f\u7528\u4e00\u4e2a\u6587\u672c\u7f16\u8f91\u5668\u6765\u4e3a Python \u51c6\u5907\u66f4\u590d\u6742\u7684\u8f93\u5165; \u5927\u591a\u6587\u672c\u7f16\u8f91\u5668\u5e26\u6709\u81ea\u52a8\u7f29\u8fdb\u529f\u80fd. \u5f53\u4e00\u4e2a\u590d\u5408\u8bed\u53e5\u4ea4\u4e92\u5730\u8f93\u5165\u65f6, \u5fc5\u987b\u8ddf\u4e0a\u4e00\u4e2a\u7a7a\u884c\u4ee5\u8868\u660e\u8bed\u53e5\u7ed3\u675f (\u56e0\u4e3a\u8bed\u6cd5\u5206\u6790\u5668\u731c\u4e0d\u5230\u4f55\u65f6\u4f60\u952e\u5165\u4e86\u6700\u540e\u4e00\u884c). \u6ce8\u610f, \u5728\u540c\u4e00\u57fa\u672c\u5757\u91cc\u7684\u6bcf\u4e00\u884c\u5fc5\u987b\u4ee5\u540c\u4e00\u4e2a\u6570\u91cf\u7f29\u8fdb.<\/li>\n\n\n\n<li><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#print\"><code>print()<\/code><\/a>\u00a0\u51fd\u6570\u5199\u51fa\u7ed9\u5b83\u7684\u8868\u8fbe\u662f\u7684\u503c. \u5b83\u4e0e\u5c31\u5199\u51fa\u4f60\u60f3\u8981\u5199\u7684\u8868\u8fbe\u5f0f\u6709\u6240\u4e0d\u540c (\u5c31\u50cf\u6211\u4eec\u5728\u4e4b\u524d\u8ba1\u7b97\u5668\u4f8b\u5b50\u4e2d\u4e00\u6837), \u5b83\u53ef\u4ee5\u5904\u7406\u591a\u4e2a\u8868\u8fbe\u5f0f, \u6d6e\u70b9\u6570, \u548c\u5b57\u7b26\u4e32. \u6253\u5370\u5b57\u7b26\u4e32\u65f6\u6ca1\u6709\u5f15\u53f7, \u5728\u4e0d\u540c\u9879\u4e4b\u95f4\u63d2\u5165\u4e86\u4e00\u4e2a\u7a7a\u683c, \u56e0\u6b64, \u4f60\u53ef\u4ee5\u628a\u4e1c\u897f\u683c\u5f0f\u5f97\u6f02\u4eae\u3002 \u5173\u952e\u8bcd\u00a0<em>end<\/em>\u00a0\u53ef\u4ee5\u7528\u6765\u907f\u514d\u8f93\u51fa\u540e\u7684\u56de\u8f66, \u6216\u8005\u4ee5\u4e00\u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u7ed3\u675f\u8f93\u51fa\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7ae0\u7684pdf \u7248\u672c\u4e0b\u8f7d: Python_Tutorial_zh-cn_liam0205.pdf \u5728\u4ee5\u4e0b\u7684\u4f8b\u5b50\u4e2d [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-192","post","type-post","status-publish","format-standard","hentry","category-python"],"blocksy_meta":"","_links":{"self":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=192"}],"version-history":[{"count":19,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/192\/revisions"}],"predecessor-version":[{"id":310,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/192\/revisions\/310"}],"wp:attachment":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=192"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}