{"id":556,"date":"2025-10-23T14:54:58","date_gmt":"2025-10-23T06:54:58","guid":{"rendered":"http:\/\/www.pyteam.cn\/?p=556"},"modified":"2025-10-23T14:54:58","modified_gmt":"2025-10-23T06:54:58","slug":"python3-%e5%9f%ba%e7%a1%80%e8%af%ad%e6%b3%95","status":"publish","type":"post","link":"http:\/\/www.pyteam.cn\/index.php\/2025\/10\/23\/python3-%e5%9f%ba%e7%a1%80%e8%af%ad%e6%b3%95\/","title":{"rendered":"Python3 \u57fa\u7840\u8bed\u6cd5"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p><strong>\u7f16\u7801<\/strong><strong><\/strong><\/p>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cPython 3 \u6e90\u7801\u6587\u4ef6\u4ee5&nbsp;<strong>UTF-8<\/strong>&nbsp;\u7f16\u7801\uff0c\u6240\u6709\u5b57\u7b26\u4e32\u90fd\u662f unicode \u5b57\u7b26\u4e32\u3002 \u5f53\u7136\u4f60\u4e5f\u53ef\u4ee5\u4e3a\u6e90\u7801\u6587\u4ef6\u6307\u5b9a\u4e0d\u540c\u7684\u7f16\u7801\uff1a<\/p>\n\n\n\n<p># -*- coding: cp-1252 -*-<\/p>\n\n\n\n<p>\u4e0a\u8ff0\u5b9a\u4e49\u5141\u8bb8\u5728\u6e90\u6587\u4ef6\u4e2d\u4f7f\u7528 Windows-1252 \u5b57\u7b26\u96c6\u4e2d\u7684\u5b57\u7b26\u7f16\u7801\uff0c\u5bf9\u5e94\u9002\u5408\u8bed\u8a00\u4e3a\u4fdd\u52a0\u5229\u4e9a\u8bed\u3001\u767d\u7f57\u65af\u8bed\u3001\u9a6c\u5176\u987f\u8bed\u3001\u4fc4\u8bed\u3001\u585e\u5c14\u7ef4\u4e9a\u8bed\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u6807\u8bc6\u7b26<\/strong><strong><\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c\u4e00\u4e2a\u5b57\u7b26\u5fc5\u987b\u662f\u5b57\u6bcd\u8868\u4e2d\u5b57\u6bcd\u6216\u4e0b\u5212\u7ebf\u00a0<strong>_<\/strong>\u00a0\u3002<\/li>\n\n\n\n<li>\u6807\u8bc6\u7b26\u7684\u5176\u4ed6\u7684\u90e8\u5206\u7531\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\u7ec4\u6210\u3002<\/li>\n\n\n\n<li>\u6807\u8bc6\u7b26\u5bf9\u5927\u5c0f\u5199\u654f\u611f\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5728 Python 3 \u4e2d\uff0c\u53ef\u4ee5\u7528\u4e2d\u6587\u4f5c\u4e3a\u53d8\u91cf\u540d\uff0c\u975e ASCII \u6807\u8bc6\u7b26\u4e5f\u662f\u5141\u8bb8\u7684\u4e86\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>python\u4fdd\u7559\u5b57<\/strong><strong><\/strong><\/p>\n\n\n\n<p>\u4fdd\u7559\u5b57\u5373\u5173\u952e\u5b57\uff0c\u6211\u4eec\u4e0d\u80fd\u628a\u5b83\u4eec\u7528\u4f5c\u4efb\u4f55\u6807\u8bc6\u7b26\u540d\u79f0\u3002Python \u7684\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a keyword \u6a21\u5757\uff0c\u53ef\u4ee5\u8f93\u51fa\u5f53\u524d\u7248\u672c\u7684\u6240\u6709\u5173\u952e\u5b57\uff1a<\/p>\n\n\n\n<p>&gt;&gt;&gt;&nbsp;import&nbsp;keyword<\/p>\n\n\n\n<p>&gt;&gt;&gt;&nbsp;keyword.kwlist<\/p>\n\n\n\n<p>[&#8216;False&#8217;,&nbsp;&#8216;None&#8217;,&nbsp;&#8216;True&#8217;,&nbsp;&#8216;and&#8217;,&nbsp;&#8216;as&#8217;,&nbsp;&#8216;assert&#8217;,&nbsp;&#8216;break&#8217;,&nbsp;&#8216;class&#8217;,&nbsp;&#8216;continue&#8217;,&nbsp;&#8216;def&#8217;,&nbsp;&#8216;del&#8217;,&nbsp;&#8216;elif&#8217;,&nbsp;&#8216;else&#8217;,&nbsp;&#8216;except&#8217;,&nbsp;&#8216;finally&#8217;,&nbsp;&#8216;for&#8217;,&nbsp;&#8216;from&#8217;,&nbsp;&#8216;global&#8217;,&nbsp;&#8216;if&#8217;,&nbsp;&#8216;import&#8217;,&nbsp;&#8216;in&#8217;,&nbsp;&#8216;is&#8217;,&nbsp;&#8216;lambda&#8217;,&nbsp;&#8216;nonlocal&#8217;,&nbsp;&#8216;not&#8217;,&nbsp;&#8216;or&#8217;,&nbsp;&#8216;pass&#8217;,&nbsp;&#8216;raise&#8217;,&nbsp;&#8216;return&#8217;,&nbsp;&#8216;try&#8217;,&nbsp;&#8216;while&#8217;,&nbsp;&#8216;with&#8217;,&nbsp;&#8216;yield&#8217;]<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u6ce8\u91ca<\/strong><strong><\/strong><\/p>\n\n\n\n<p>Python\u4e2d\u5355\u884c\u6ce8\u91ca\u4ee5&nbsp;<strong>#<\/strong>&nbsp;\u5f00\u5934\uff0c\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p><strong>\u5b9e\u4f8b(Python 3.0+)<\/strong><strong><\/strong><\/p>\n\n\n\n<p>#!\/usr\/bin\/python3&nbsp;# \u7b2c\u4e00\u4e2a\u6ce8\u91ca&nbsp;print&nbsp;(&#8220;Hello, Python!&#8221;)&nbsp;# \u7b2c\u4e8c\u4e2a\u6ce8\u91ca<\/p>\n\n\n\n<p>\u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<p>Hello,&nbsp;Python!<\/p>\n\n\n\n<p>\u591a\u884c\u6ce8\u91ca\u53ef\u4ee5\u7528\u591a\u4e2a&nbsp;<strong>#<\/strong>&nbsp;\u53f7\uff0c\u8fd8\u6709&nbsp;<strong>&#8221;&#8217;<\/strong>&nbsp;\u548c&nbsp;<strong>&#8220;&#8221;&#8221;<\/strong>\uff1a<\/p>\n\n\n\n<p><strong>\u5b9e\u4f8b(Python 3.0+)<\/strong><strong><\/strong><\/p>\n\n\n\n<p>#!\/usr\/bin\/python3&nbsp;# \u7b2c\u4e00\u4e2a\u6ce8\u91ca&nbsp;# \u7b2c\u4e8c\u4e2a\u6ce8\u91ca&nbsp;&#8221;&#8217;&nbsp;\u7b2c\u4e09\u6ce8\u91ca \u7b2c\u56db\u6ce8\u91ca &#8221;&#8217;&nbsp;&#8220;&#8221;&#8221;&nbsp;\u7b2c\u4e94\u6ce8\u91ca \u7b2c\u516d\u6ce8\u91ca &#8220;&#8221;&#8221;&nbsp;print&nbsp;(&#8220;Hello, Python!&#8221;)<\/p>\n\n\n\n<p>\u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<p>Hello,&nbsp;Python!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u884c\u4e0e\u7f29\u8fdb<\/strong><strong><\/strong><\/p>\n\n\n\n<p>python\u6700\u5177\u7279\u8272\u7684\u5c31\u662f\u4f7f\u7528\u7f29\u8fdb\u6765\u8868\u793a\u4ee3\u7801\u5757\uff0c\u4e0d\u9700\u8981\u4f7f\u7528\u5927\u62ec\u53f7&nbsp;<strong>{}<\/strong>&nbsp;\u3002<\/p>\n\n\n\n<p>\u7f29\u8fdb\u7684\u7a7a\u683c\u6570\u662f\u53ef\u53d8\u7684\uff0c\u4f46\u662f\u540c\u4e00\u4e2a\u4ee3\u7801\u5757\u7684\u8bed\u53e5\u5fc5\u987b\u5305\u542b\u76f8\u540c\u7684\u7f29\u8fdb\u7a7a\u683c\u6570\u3002\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p><strong>\u5b9e\u4f8b(Python 3.0+)<\/strong><strong><\/strong><\/p>\n\n\n\n<p>if&nbsp;True: print&nbsp;(&#8220;True&#8221;)&nbsp;else: print&nbsp;(&#8220;False&#8221;)<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u4ee3\u7801\u6700\u540e\u4e00\u884c\u8bed\u53e5\u7f29\u8fdb\u6570\u7684\u7a7a\u683c\u6570\u4e0d\u4e00\u81f4\uff0c\u4f1a\u5bfc\u81f4\u8fd0\u884c\u9519\u8bef\uff1a<\/p>\n\n\n\n<p><strong>\u5b9e\u4f8b<\/strong><strong><\/strong><\/p>\n\n\n\n<p><strong>if<\/strong>&nbsp;True:<br>&nbsp; &nbsp;&nbsp;<strong>print<\/strong>&nbsp;(&#8220;Answer&#8221;)<br>&nbsp; &nbsp;&nbsp;<strong>print<\/strong>&nbsp;(&#8220;True&#8221;)<br><strong>else<\/strong>:<br>&nbsp; &nbsp;&nbsp;<strong>print<\/strong>&nbsp;(&#8220;Answer&#8221;)<br>&nbsp;&nbsp; <strong>print<\/strong>&nbsp;(&#8220;False&#8221;)&nbsp;&nbsp; &nbsp;# \u7f29\u8fdb\u4e0d\u4e00\u81f4\uff0c\u4f1a\u5bfc\u81f4\u8fd0\u884c\u9519\u8bef<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u7531\u4e8e\u7f29\u8fdb\u4e0d\u4e00\u81f4\uff0c\u6267\u884c\u540e\u4f1a\u51fa\u73b0\u7c7b\u4f3c\u4ee5\u4e0b\u9519\u8bef\uff1a<\/p>\n\n\n\n<p>&nbsp;File&nbsp;&#8220;test.py&#8221;,&nbsp;line 6<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;(&#8220;False&#8221;)&nbsp;&nbsp;&nbsp;&nbsp;# \u7f29\u8fdb\u4e0d\u4e00\u81f4\uff0c\u4f1a\u5bfc\u81f4\u8fd0\u884c\u9519\u8bef<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^<\/p>\n\n\n\n<p>IndentationError:&nbsp;unindent does not&nbsp;match any outer indentation level<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u591a\u884c\u8bed\u53e5<\/strong><strong><\/strong><\/p>\n\n\n\n<p>Python \u901a\u5e38\u662f\u4e00\u884c\u5199\u5b8c\u4e00\u6761\u8bed\u53e5\uff0c\u4f46\u5982\u679c\u8bed\u53e5\u5f88\u957f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u53cd\u659c\u6760&nbsp;<strong>\\<\/strong>&nbsp;\u6765\u5b9e\u73b0\u591a\u884c\u8bed\u53e5\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<p>total =&nbsp;item_one +&nbsp;\\<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item_two +&nbsp;\\<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item_three<\/p>\n\n\n\n<p>\u5728 [], {}, \u6216 () \u4e2d\u7684\u591a\u884c\u8bed\u53e5\uff0c\u4e0d\u9700\u8981\u4f7f\u7528\u53cd\u659c\u6760&nbsp;<strong>\\<\/strong>\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<p>total =&nbsp;[&#8216;item_one&#8217;,&nbsp;&#8216;item_two&#8217;,&nbsp;&#8216;item_three&#8217;,<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8216;item_four&#8217;,&nbsp;&#8216;item_five&#8217;]<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u6570\u5b57(Number)\u7c7b\u578b<\/strong><strong><\/strong><\/p>\n\n\n\n<p>python\u4e2d\u6570\u5b57\u6709\u56db\u79cd\u7c7b\u578b\uff1a\u6574\u6570\u3001\u5e03\u5c14\u578b\u3001\u6d6e\u70b9\u6570\u548c\u590d\u6570\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>int<\/strong>\u00a0(\u6574\u6570), \u5982 1, \u53ea\u6709\u4e00\u79cd\u6574\u6570\u7c7b\u578b int\uff0c\u8868\u793a\u4e3a\u957f\u6574\u578b\uff0c\u6ca1\u6709 python2 \u4e2d\u7684 Long\u3002<\/li>\n\n\n\n<li><strong>bool<\/strong>\u00a0(\u5e03\u5c14), \u5982 True\u3002<\/li>\n\n\n\n<li><strong>float<\/strong>\u00a0(\u6d6e\u70b9\u6570), \u5982 1.23\u30013E-2<\/li>\n\n\n\n<li><strong>complex<\/strong>\u00a0(\u590d\u6570), \u5982 1 + 2j\u3001 1.1 + 2.2j<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u5b57\u7b26\u4e32(String)<\/strong><strong><\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python \u4e2d\u5355\u5f15\u53f7\u00a0<strong>&#8216;<\/strong>\u00a0\u548c\u53cc\u5f15\u53f7\u00a0<strong>&#8220;<\/strong>\u00a0\u4f7f\u7528\u5b8c\u5168\u76f8\u540c\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u4e09\u5f15\u53f7(<strong>&#8221;&#8217;<\/strong>\u00a0\u6216\u00a0<strong>&#8220;&#8221;&#8221;<\/strong>)\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u591a\u884c\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li>\u8f6c\u4e49\u7b26\u00a0<strong>\\<\/strong>\u3002<\/li>\n\n\n\n<li>\u53cd\u659c\u6760\u53ef\u4ee5\u7528\u6765\u8f6c\u4e49\uff0c\u4f7f\u7528\u00a0<strong>r<\/strong>\u00a0\u53ef\u4ee5\u8ba9\u53cd\u659c\u6760\u4e0d\u53d1\u751f\u8f6c\u4e49\u3002 \u5982\u00a0<strong>r&#8221;this is a line with \\n&#8221;<\/strong>\u00a0\u5219\u00a0<strong>\\n<\/strong>\u00a0\u4f1a\u663e\u793a\uff0c\u5e76\u4e0d\u662f\u6362\u884c\u3002<\/li>\n\n\n\n<li>\u6309\u5b57\u9762\u610f\u4e49\u7ea7\u8054\u5b57\u7b26\u4e32\uff0c\u5982\u00a0<strong>&#8220;this &#8221; &#8220;is &#8221; &#8220;string&#8221;<\/strong>\u00a0\u4f1a\u88ab\u81ea\u52a8\u8f6c\u6362\u4e3a\u00a0<strong>this is string<\/strong>\u3002<\/li>\n\n\n\n<li>\u5b57\u7b26\u4e32\u53ef\u4ee5\u7528\u00a0<strong>+<\/strong>\u00a0\u8fd0\u7b97\u7b26\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u7528\u00a0<strong>*<\/strong>\u00a0\u8fd0\u7b97\u7b26\u91cd\u590d\u3002<\/li>\n\n\n\n<li>Python \u4e2d\u7684\u5b57\u7b26\u4e32\u6709\u4e24\u79cd\u7d22\u5f15\u65b9\u5f0f\uff0c\u4ece\u5de6\u5f80\u53f3\u4ee5\u00a0<strong>0<\/strong>\u00a0\u5f00\u59cb\uff0c\u4ece\u53f3\u5f80\u5de6\u4ee5\u00a0<strong>-1<\/strong>\u00a0\u5f00\u59cb\u3002<\/li>\n\n\n\n<li>Python \u4e2d\u7684\u5b57\u7b26\u4e32\u4e0d\u80fd\u6539\u53d8\u3002<\/li>\n\n\n\n<li>Python \u6ca1\u6709\u5355\u72ec\u7684\u5b57\u7b26\u7c7b\u578b\uff0c\u4e00\u4e2a\u5b57\u7b26\u5c31\u662f\u957f\u5ea6\u4e3a 1 \u7684\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li>\u5b57\u7b26\u4e32\u7684\u622a\u53d6\u7684\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<strong>\u53d8\u91cf[\u5934\u4e0b\u6807:\u5c3e\u4e0b\u6807:\u6b65\u957f]<\/strong><\/li>\n<\/ul>\n\n\n\n<p>word =&nbsp;&#8216;\u5b57\u7b26\u4e32&#8217;<\/p>\n\n\n\n<p>sentence =&nbsp;&#8220;\u8fd9\u662f\u4e00\u4e2a\u53e5\u5b50\u3002&#8221;<\/p>\n\n\n\n<p>paragraph =&nbsp;&#8220;&#8221;&#8221;\u8fd9\u662f\u4e00\u4e2a\u6bb5\u843d\uff0c<\/p>\n\n\n\n<p>\u53ef\u4ee5\u7531\u591a\u884c\u7ec4\u6210&#8221;&#8221;&#8221;<\/p>\n\n\n\n<p><strong>\u5b9e\u4f8b(Python 3.0+)<\/strong><strong><\/strong><\/p>\n\n\n\n<p>#!\/usr\/bin\/python3&nbsp;<\/p>\n\n\n\n<p>str=&#8217;123456789&#8242;<\/p>\n\n\n\n<p>print(str)&nbsp;# \u8f93\u51fa\u5b57\u7b26\u4e32&nbsp;<\/p>\n\n\n\n<p>print(str[0:-1])&nbsp;# \u8f93\u51fa\u7b2c\u4e00\u4e2a\u5230\u5012\u6570\u7b2c\u4e8c\u4e2a\u7684\u6240\u6709\u5b57\u7b26&nbsp;<\/p>\n\n\n\n<p>print(str[0])&nbsp;# \u8f93\u51fa\u5b57\u7b26\u4e32\u7b2c\u4e00\u4e2a\u5b57\u7b26&nbsp;<\/p>\n\n\n\n<p>print(str[2:5])&nbsp;# \u8f93\u51fa\u4ece\u7b2c\u4e09\u4e2a\u5f00\u59cb\u5230\u7b2c\u4e94\u4e2a\u7684\u5b57\u7b26&nbsp;<\/p>\n\n\n\n<p>print(str[2:])&nbsp;# \u8f93\u51fa\u4ece\u7b2c\u4e09\u4e2a\u5f00\u59cb\u540e\u7684\u6240\u6709\u5b57\u7b26&nbsp;<\/p>\n\n\n\n<p>print(str[1:5:2])&nbsp;# \u8f93\u51fa\u4ece\u7b2c\u4e8c\u4e2a\u5f00\u59cb\u5230\u7b2c\u4e94\u4e2a\u4e14\u6bcf\u9694\u4e00\u4e2a\u7684\u5b57\u7b26\uff08\u6b65\u957f\u4e3a2\uff09&nbsp;<\/p>\n\n\n\n<p>print(str&nbsp;* 2)&nbsp;# \u8f93\u51fa\u5b57\u7b26\u4e32\u4e24\u6b21&nbsp;<\/p>\n\n\n\n<p>print(str&nbsp;+ &#8216;\u4f60\u597d&#8217;)&nbsp;# \u8fde\u63a5\u5b57\u7b26\u4e32&nbsp;<\/p>\n\n\n\n<p>print(&#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8216;)&nbsp;<\/p>\n\n\n\n<p>print(&#8216;hello\\nrunoob&#8217;)&nbsp;# \u4f7f\u7528\u53cd\u659c\u6760(\\)+n\u8f6c\u4e49\u7279\u6b8a\u5b57\u7b26&nbsp;<\/p>\n\n\n\n<p>print(r&#8217;hello\\nrunoob&#8217;)&nbsp;# \u5728\u5b57\u7b26\u4e32\u524d\u9762\u6dfb\u52a0\u4e00\u4e2a r\uff0c\u8868\u793a\u539f\u59cb\u5b57\u7b26\u4e32\uff0c\u4e0d\u4f1a\u53d1\u751f\u8f6c\u4e49<\/p>\n\n\n\n<p>\u8fd9\u91cc\u7684 r \u6307 raw\uff0c\u5373 raw string\uff0c\u4f1a\u81ea\u52a8\u5c06\u53cd\u659c\u6760\u8f6c\u4e49\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<p>&gt;&gt;&gt;&nbsp;print(&#8216;\\n&#8217;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# \u8f93\u51fa\u7a7a\u884c<\/p>\n\n\n\n<p>&gt;&gt;&gt;&nbsp;print(r&#8217;\\n&#8217;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# \u8f93\u51fa \\n<\/p>\n\n\n\n<p>\\n<\/p>\n\n\n\n<p>&gt;&gt;&gt;<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u5b9e\u4f8b\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<p>123456789<\/p>\n\n\n\n<p>12345678<\/p>\n\n\n\n<p>1<\/p>\n\n\n\n<p>345<\/p>\n\n\n\n<p>3456789<\/p>\n\n\n\n<p>24<\/p>\n\n\n\n<p>123456789123456789<\/p>\n\n\n\n<p>123456789\u4f60\u597d<\/p>\n\n\n\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n\n\n\n<p>hello<\/p>\n\n\n\n<p>runoob<\/p>\n\n\n\n<p>hello\\nrunoob<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u7a7a\u884c<\/strong><strong><\/strong><\/p>\n\n\n\n<p>\u51fd\u6570\u4e4b\u95f4\u6216\u7c7b\u7684\u65b9\u6cd5\u4e4b\u95f4\u7528\u7a7a\u884c\u5206\u9694\uff0c\u8868\u793a\u4e00\u6bb5\u65b0\u7684\u4ee3\u7801\u7684\u5f00\u59cb\u3002\u7c7b\u548c\u51fd\u6570\u5165\u53e3\u4e4b\u95f4\u4e5f\u7528\u4e00\u884c\u7a7a\u884c\u5206\u9694\uff0c\u4ee5\u7a81\u51fa\u51fd\u6570\u5165\u53e3\u7684\u5f00\u59cb\u3002<\/p>\n\n\n\n<p>\u7a7a\u884c\u4e0e\u4ee3\u7801\u7f29\u8fdb\u4e0d\u540c\uff0c\u7a7a\u884c\u5e76\u4e0d\u662f Python \u8bed\u6cd5\u7684\u4e00\u90e8\u5206\u3002\u4e66\u5199\u65f6\u4e0d\u63d2\u5165\u7a7a\u884c\uff0cPython \u89e3\u91ca\u5668\u8fd0\u884c\u4e5f\u4e0d\u4f1a\u51fa\u9519\u3002\u4f46\u662f\u7a7a\u884c\u7684\u4f5c\u7528\u5728\u4e8e\u5206\u9694\u4e24\u6bb5\u4e0d\u540c\u529f\u80fd\u6216\u542b\u4e49\u7684\u4ee3\u7801\uff0c\u4fbf\u4e8e\u65e5\u540e\u4ee3\u7801\u7684\u7ef4\u62a4\u6216\u91cd\u6784\u3002<\/p>\n\n\n\n<p><strong>\u8bb0\u4f4f\uff1a<\/strong>\u7a7a\u884c\u4e5f\u662f\u7a0b\u5e8f\u4ee3\u7801\u7684\u4e00\u90e8\u5206\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u7b49\u5f85\u7528\u6237\u8f93\u5165<\/strong><strong><\/strong><\/p>\n\n\n\n<p>\u6267\u884c\u4e0b\u9762\u7684\u7a0b\u5e8f\u5728\u6309\u56de\u8f66\u952e\u540e\u5c31\u4f1a\u7b49\u5f85\u7528\u6237\u8f93\u5165\uff1a<\/p>\n\n\n\n<p><strong>\u5b9e\u4f8b(Python 3.0+)<\/strong><strong><\/strong><\/p>\n\n\n\n<p>#!\/usr\/bin\/python3&nbsp;<\/p>\n\n\n\n<p>input(&#8220;\\n\\n\u6309\u4e0b enter \u952e\u540e\u9000\u51fa\u3002&#8221;)<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d \uff0c<strong>\\n\\n<\/strong>&nbsp;\u5728\u7ed3\u679c\u8f93\u51fa\u524d\u4f1a\u8f93\u51fa\u4e24\u4e2a\u65b0\u7684\u7a7a\u884c\u3002\u4e00\u65e6\u7528\u6237\u6309\u4e0b&nbsp;<strong>enter<\/strong>&nbsp;\u952e\u65f6\uff0c\u7a0b\u5e8f\u5c06\u9000\u51fa\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u540c\u4e00\u884c\u663e\u793a\u591a\u6761\u8bed\u53e5<\/strong><strong><\/strong><\/p>\n\n\n\n<p>Python \u53ef\u4ee5\u5728\u540c\u4e00\u884c\u4e2d\u4f7f\u7528\u591a\u6761\u8bed\u53e5\uff0c\u8bed\u53e5\u4e4b\u95f4\u4f7f\u7528\u5206\u53f7&nbsp;<strong>;<\/strong>&nbsp;\u5206\u5272\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u4f8b\uff1a<\/p>\n\n\n\n<p><strong>\u5b9e\u4f8b(Python 3.0+)<\/strong><strong><\/strong><\/p>\n\n\n\n<p>#!\/usr\/bin\/python3&nbsp;<\/p>\n\n\n\n<p>import&nbsp;sys;<\/p>\n\n\n\n<p>x&nbsp;= &#8216;runoob&#8217;;<\/p>\n\n\n\n<p>sys.stdout.write(x&nbsp;+ &#8216;\\n&#8217;)<\/p>\n\n\n\n<p>\u4f7f\u7528\u811a\u672c\u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<p>runoob<\/p>\n\n\n\n<p>\u4f7f\u7528\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u6267\u884c\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<p>&gt;&gt;&gt;&nbsp;import&nbsp;sys;&nbsp;x =&nbsp;&#8216;runoob&#8217;;&nbsp;sys.stdout.write(x +&nbsp;&#8216;\\n&#8217;)<\/p>\n\n\n\n<p>runoob<\/p>\n\n\n\n<p>7<\/p>\n\n\n\n<p>\u6b64\u5904\u7684 7 \u8868\u793a\u5b57\u7b26\u6570\uff0c<strong>runoob<\/strong>&nbsp;\u6709 6 \u4e2a\u5b57\u7b26\uff0c<strong>\\n<\/strong>&nbsp;\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff0c\u52a0\u8d77\u6765&nbsp;<strong>7<\/strong>&nbsp;\u4e2a\u5b57\u7b26\u3002<\/p>\n\n\n\n<p>&gt;&gt;&gt;&nbsp;import&nbsp;sys<\/p>\n\n\n\n<p>&gt;&gt;&gt;&nbsp;sys.stdout.write(&#8221; hi &#8220;)&nbsp;&nbsp;&nbsp;&nbsp;# hi \u524d\u540e\u5404\u6709 1 \u4e2a\u7a7a\u683c<\/p>\n\n\n\n<p>&nbsp;hi 4<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u591a\u4e2a\u8bed\u53e5\u6784\u6210\u4ee3\u7801\u7ec4<\/strong><strong><\/strong><\/p>\n\n\n\n<p>\u7f29\u8fdb\u76f8\u540c\u7684\u4e00\u7ec4\u8bed\u53e5\u6784\u6210\u4e00\u4e2a\u4ee3\u7801\u5757\uff0c\u6211\u4eec\u79f0\u4e4b\u4ee3\u7801\u7ec4\u3002<\/p>\n\n\n\n<p>\u50cfif\u3001while\u3001def\u548cclass\u8fd9\u6837\u7684\u590d\u5408\u8bed\u53e5\uff0c\u9996\u884c\u4ee5\u5173\u952e\u5b57\u5f00\u59cb\uff0c\u4ee5\u5192\u53f7( : )\u7ed3\u675f\uff0c\u8be5\u884c\u4e4b\u540e\u7684\u4e00\u884c\u6216\u591a\u884c\u4ee3\u7801\u6784\u6210\u4ee3\u7801\u7ec4\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5c06\u9996\u884c\u53ca\u540e\u9762\u7684\u4ee3\u7801\u7ec4\u79f0\u4e3a\u4e00\u4e2a\u5b50\u53e5(clause)\u3002<\/p>\n\n\n\n<p>\u5982\u4e0b\u5b9e\u4f8b\uff1a<\/p>\n\n\n\n<p>if&nbsp;expression :&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;suite<\/p>\n\n\n\n<p>elif&nbsp;expression :&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;suite<\/p>\n\n\n\n<p>else&nbsp;:&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;suite<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>print \u8f93\u51fa<\/strong><strong><\/strong><\/p>\n\n\n\n<p><strong>print<\/strong>&nbsp;\u9ed8\u8ba4\u8f93\u51fa\u662f\u6362\u884c\u7684\uff0c\u5982\u679c\u8981\u5b9e\u73b0\u4e0d\u6362\u884c\u9700\u8981\u5728\u53d8\u91cf\u672b\u5c3e\u52a0\u4e0a&nbsp;<strong>end=&#8221;&#8221;<\/strong>\uff1a<\/p>\n\n\n\n<p><strong>\u5b9e\u4f8b(Python 3.0+)<\/strong><strong><\/strong><\/p>\n\n\n\n<p>#!\/usr\/bin\/python3&nbsp;<\/p>\n\n\n\n<p>x=&#8221;a&#8221;&nbsp;<\/p>\n\n\n\n<p>y=&#8221;b&#8221;&nbsp;<\/p>\n\n\n\n<p># \u6362\u884c\u8f93\u51fa&nbsp;<\/p>\n\n\n\n<p>print(&nbsp;x&nbsp;)&nbsp;<\/p>\n\n\n\n<p>print(&nbsp;y&nbsp;)&nbsp;<\/p>\n\n\n\n<p>print(&#8216;&#8212;&#8212;&#8212;&#8216;)&nbsp;<\/p>\n\n\n\n<p># \u4e0d\u6362\u884c\u8f93\u51fa&nbsp;<\/p>\n\n\n\n<p>print(&nbsp;x, end=&#8221;&nbsp;&#8221;&nbsp;)&nbsp;<\/p>\n\n\n\n<p>print(&nbsp;y, end=&#8221;&nbsp;&#8221;&nbsp;)&nbsp;<\/p>\n\n\n\n<p>print()<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u5b9e\u4f8b\u6267\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<p>a<\/p>\n\n\n\n<p>b<\/p>\n\n\n\n<p>&#8212;&#8212;&#8212;<\/p>\n\n\n\n<p>a b<\/p>\n\n\n\n<p><strong><em><strong><em>\u66f4\u591a\u5185\u5bb9\u53c2\u8003\uff1a<\/em><\/strong><\/em><\/strong><a href=\"https:\/\/www.runoob.com\/w3cnote\/python-print-without-newline.html\"><em><u><em>Python2 \u4e0e Python3 print \u4e0d\u6362\u884c<\/em><\/u><\/em><\/a><em><\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>import \u4e0e from&#8230;import<\/strong><strong><\/strong><\/p>\n\n\n\n<p>\u5728 python \u7528&nbsp;<strong>import<\/strong>&nbsp;\u6216\u8005&nbsp;<strong>from&#8230;import<\/strong>&nbsp;\u6765\u5bfc\u5165\u76f8\u5e94\u7684\u6a21\u5757\u3002<\/p>\n\n\n\n<p>\u5c06\u6574\u4e2a\u6a21\u5757(somemodule)\u5bfc\u5165\uff0c\u683c\u5f0f\u4e3a\uff1a&nbsp;<strong>import somemodule<\/strong><\/p>\n\n\n\n<p>\u4ece\u67d0\u4e2a\u6a21\u5757\u4e2d\u5bfc\u5165\u67d0\u4e2a\u51fd\u6570,\u683c\u5f0f\u4e3a\uff1a&nbsp;<strong>from somemodule import somefunction<\/strong><\/p>\n\n\n\n<p>\u4ece\u67d0\u4e2a\u6a21\u5757\u4e2d\u5bfc\u5165\u591a\u4e2a\u51fd\u6570,\u683c\u5f0f\u4e3a\uff1a&nbsp;<strong>from somemodule import firstfunc, secondfunc, thirdfunc<\/strong><\/p>\n\n\n\n<p>\u5c06\u67d0\u4e2a\u6a21\u5757\u4e2d\u7684\u5168\u90e8\u51fd\u6570\u5bfc\u5165\uff0c\u683c\u5f0f\u4e3a\uff1a&nbsp;<strong>from somemodule import *<\/strong><\/p>\n\n\n\n<p><strong>\u5bfc\u5165 sys \u6a21\u5757<\/strong><strong><\/strong><\/p>\n\n\n\n<p>import&nbsp;sys&nbsp;<\/p>\n\n\n\n<p>print(&#8216;================Python import mode==========================&#8217;)&nbsp;<\/p>\n\n\n\n<p>print&nbsp;(&#8216;\u547d\u4ee4\u884c\u53c2\u6570\u4e3a:&#8217;)&nbsp;<\/p>\n\n\n\n<p>for&nbsp;i&nbsp;in&nbsp;sys.argv:<\/p>\n\n\n\n<p>print&nbsp;(i)&nbsp;<\/p>\n\n\n\n<p>print&nbsp;(&#8216;\\n&nbsp;python \u8def\u5f84\u4e3a&#8217;,sys.path)<\/p>\n\n\n\n<p><strong>\u5bfc\u5165 sys \u6a21\u5757\u7684 argv,path \u6210\u5458<\/strong><strong><\/strong><\/p>\n\n\n\n<p>from&nbsp;sys&nbsp;import&nbsp;argv,path&nbsp;# \u5bfc\u5165\u7279\u5b9a\u7684\u6210\u5458&nbsp;&nbsp;<\/p>\n\n\n\n<p>print(&#8216;================python from import===================================&#8217;)&nbsp;<\/p>\n\n\n\n<p>print(&#8216;path:&#8217;,path)&nbsp;# \u56e0\u4e3a\u5df2\u7ecf\u5bfc\u5165path\u6210\u5458\uff0c\u6240\u4ee5\u6b64\u5904\u5f15\u7528\u65f6\u4e0d\u9700\u8981\u52a0sys.path<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u547d\u4ee4\u884c\u53c2\u6570<\/strong><strong><\/strong><\/p>\n\n\n\n<p>\u5f88\u591a\u7a0b\u5e8f\u53ef\u4ee5\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\u6765\u67e5\u770b\u4e00\u4e9b\u57fa\u672c\u4fe1\u606f\uff0cPython\u53ef\u4ee5\u4f7f\u7528-h\u53c2\u6570\u67e5\u770b\u5404\u53c2\u6570\u5e2e\u52a9\u4fe1\u606f\uff1a<\/p>\n\n\n\n<p>$ python -h<\/p>\n\n\n\n<p>usage:&nbsp;python [option]&nbsp;&#8230;&nbsp;[-c cmd |&nbsp;-m mod |&nbsp;file |&nbsp;-]&nbsp;[arg]&nbsp;&#8230;<\/p>\n\n\n\n<p>Options&nbsp;and&nbsp;arguments (and&nbsp;corresponding environment variables):<\/p>\n\n\n\n<p>-c cmd :&nbsp;program passed in&nbsp;as&nbsp;string&nbsp;(terminates option list)<\/p>\n\n\n\n<p>-d &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;debug output from&nbsp;parser (also PYTHONDEBUG=x)<\/p>\n\n\n\n<p>-E &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;ignore environment variables (such as&nbsp;PYTHONPATH)<\/p>\n\n\n\n<p>-h &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;print&nbsp;this&nbsp;help message and&nbsp;exit<\/p>\n\n\n\n<p>[&nbsp;etc.&nbsp;]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f16\u7801 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cPython 3 \u6e90\u7801\u6587\u4ef6\u4ee5&nbsp;UTF-8&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":473,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-556","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-1"],"views":92,"_links":{"self":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts\/556","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/comments?post=556"}],"version-history":[{"count":1,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts\/556\/revisions"}],"predecessor-version":[{"id":557,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts\/556\/revisions\/557"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/media\/473"}],"wp:attachment":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/media?parent=556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/categories?post=556"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/tags?post=556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}