{"id":52144,"date":"2021-01-05T00:28:47","date_gmt":"2021-01-04T16:28:47","guid":{"rendered":"http:\/\/www.zhushiyao.com\/?p=52144"},"modified":"2021-01-26T14:52:35","modified_gmt":"2021-01-26T06:52:35","slug":"%e4%bb%8epython2-7%e5%90%91-python3-%e8%bd%ac%e6%8d%a2%ef%bc%88by%e3%80%8a%e7%ac%a8%e5%8a%9e%e6%b3%95%e5%ad%a6python3%e3%80%8b%ef%bc%89%e4%b9%8b%e4%b8%80","status":"publish","type":"post","link":"http:\/\/www.zhushiyao.com\/?p=52144","title":{"rendered":"\u4ecepython2.7\u5411 python3 \u8f6c\u6362\uff08by\u300a\u7b28\u529e\u6cd5\u5b66python3\u300b\uff09"},"content":{"rendered":"\n<p>\u5927\u560e\u597d\uff0c\u534a\u540a\u5b50\u7a0b\u5e8f\u5458\u4ece\u7f51\u7ebf\u7684\u8fd9\u4e00\u7aef\u5411\u60a8\u95ee\u597d\u3002<\/p>\n\n\n\n<p>\u8fc7\u53bb\u7684\u8fd8\u662f\u4f1a\u8fc7\u53bb\uff0c\u5c31\u50cf\u65f6\u95f4\uff0c\u5c31\u50cf\u6d41\u6c34\uff0c\u5c31\u50cfpython 2.7<\/p>\n\n\n\n<p>\u7ec8\u4e8e\u8fd8\u662f\u8981\u5411python3\u5207\u6362<\/p>\n\n\n\n<p>\u7f51\u4e0a\u641c\u7d22\u4e86\u4e00\u4e0b\uff0c\u8fd8\u662f\u7528\u300a\u7b28\u529e\u6cd5\u5b66python3\u300b\u8fd9\u4e2a\u6587\u6863\u5f00\u59cb\u5427\u3002\u5730\u5740\u5728\uff1a<a href=\"https:\/\/www.bookstack.cn\/read\/LearnPython3TheHardWay\/spilt.1.learn-py3.md\">https:\/\/www.bookstack.cn\/read\/LearnPython3TheHardWay\/spilt.1.learn-py3.md<\/a><\/p>\n\n\n\n<p>&#8212;&#8212;&#8212;&#8212;\u4ee5\u4e0b\u662f\u4e66\u91cc\u7684\u6458\u5f55\u90e8\u5206\u7684\u5206\u5272\u7ebf&#8212;&#8212;&#8212;-<\/p>\n\n\n\n<p>\n\n\u7a0b\u5e8f\u5458\u603b\u662f\u81ea\u8be9\u4e3a\u6570\u5b66\u5929\u624d\uff0c\u5176\u5b9e\u4e8b\u5b9e\u5e76\u975e\u5982\u6b64\u3002\u5982\u679c\u4ed6\u4eec\u662f\u6570\u5b66\u5929\u624d\uff0c\u4ed6\u4eec\u5c31\u4f1a\u53bb\u7814\u7a76\u6570\u5b66\uff0c\u800c\u4e0d\u662f\u53bb\u5199\u90a3\u4e9b bug \u8fde\u7bc7\u7684\u7f51\u7ad9\u6846\u67b6\u4ee5\u4fbf\u80fd\u5f00\u4e0a\u8c6a\u8f66\u3002\n\n<\/p>\n\n\n\n<p>\u6253\u5370\uff1a print(&#8220;xxxxxxxxx&#8221;)<\/p>\n\n\n\n<p>\n\nprint(&#8220;Hens&#8221;, 25 + 30 \/ 6)\n\n<\/p>\n\n\n\n<p> \u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u53ea\u9700\u8981\u5728\u201c\u201d\u524d\u52a0\u4e0af\uff0c\u8868\u793a format <\/p>\n\n\n\n<p><code>somear = \"world\" <\/code><\/p>\n\n\n\n<p><code>print( f\"Hello, {somevar}\")<\/code><\/p>\n\n\n\n<p>.format()\u4e5f\u53ef\u4ee5\u683c\u5f0f\u5316<\/p>\n\n\n\n<p> joke_evaluation = &#8220;Isn&#8217;t that joke so funny?! {}&#8221; <\/p>\n\n\n\n<p>\n\nprint(joke_evaluation.format(hilarious))\n\n<\/p>\n\n\n\n<p>print(&#8220;How old are you?&#8221;, end=&#8217; &#8216;)   <\/p>\n\n\n\n<p>age = input() <\/p>\n\n\n\n<p># \u6211\u4eec\u5728\u6bcf\u4e00\u4e2a\u6253\u5370\u884c\u672b\u5c3e\u653e\u4e00\u4e2a&nbsp;<code>end=' '<\/code>&nbsp;\uff0c\u662f\u4e3a\u4e86\u544a\u8bc9 print \u4e0d\u8981\u53e6\u8d77\u4e00\u884c\u3002 <\/p>\n\n\n\n<p><code>y = input(\"Name?\")<\/code>  \u63d0\u793a\u4fe1\u606f\u5728\uff08\uff09\u5185<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>age = input(\"How old are you? \")\n\n\u547d\u4ee4\u884c\u53c2\u6570\n\nfrom sys import argv\nscript, filename = argv<\/code><\/pre>\n\n\n\n<p><strong><code>argv<\/code>&nbsp;\u548c&nbsp;<code>input()<\/code>&nbsp;\u4e4b\u95f4\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f<\/strong>&nbsp;\u533a\u522b\u53d6\u51b3\u4e8e\u7528\u6237\u5728\u54ea\u88ab\u8981\u6c42\u8f93\u5165\uff0c\u5982\u679c\u662f\u5728\u547d\u4ee4\u884c\uff0c\u5c31\u7528 argv\u3002\u5982\u679c\u4f60\u60f3\u8ba9\u5b83\u4eec\u5728\u7a0b\u5e8f\u5df2\u7ecf\u8fd0\u884c\u7684\u60c5\u51b5\u4e0b\u7528\u952e\u76d8\u8f93\u5165\uff0c\u90a3\u5c31\u7528&nbsp;<code>input()<\/code><\/p>\n\n\n\n<p>\u8bfb\u53d6\u6587\u4ef6<\/p>\n\n\n\n<p> txt_again = open(file_again) <\/p>\n\n\n\n<p>\n\nprint(txt_again.read())\n\n<\/p>\n\n\n\n<p><strong><code>txt = open(filename)<\/code>&nbsp;\u4f1a\u8fd4\u56de\u6587\u4ef6\u7684\u5185\u5bb9\u5417\uff1f<\/strong>&nbsp;\u4e0d\u4f1a\u3002\u5b83\u5176\u5b9e\u662f\u521b\u5efa\u4e86\u4e00\u4e2a\u53eb\u505a\u201c\u6587\u4ef6\u5bf9\u8c61\u201d\uff08file object\uff09\u7684\u4e1c\u897f <\/p>\n\n\n\n<p><code>exists<\/code>\u3002\u5b83\u4f1a\u57fa\u4e8e\u4e00\u4e2a\u5b57\u7b26\u4e32\u91cc\u9762\u7684\u53d8\u91cf\u6587\u4ef6\u540d\u6765\u5224\u65ad\uff0c\u5982\u679c\u4e00\u4e2a\u6587\u4ef6\u5b58\u5728\uff0c\u5b83\u5c31\u4f1a\u8fd4\u56de&nbsp;<code>True<\/code>\uff0c\u4e0d\u5b58\u5728\u5c31\u4f1a\u8fd4\u56de&nbsp;<code>False<\/code>\uff0c  <\/p>\n\n\n\n<p>from os.path import exists  <\/p>\n\n\n\n<p> exists(to_file) <\/p>\n\n\n\n<ul><li><strong>close<\/strong>&nbsp;&#8211; \u5173\u95ed\u6587\u4ef6\uff0c\u5c31\u50cf\u7f16\u8f91\u5668\u4e2d\u7684 \u201c\u6587\u4ef6-&gt;\u53e6\u5b58\u4e3a\u201d\u4e00\u6837\u3002<\/li><li><strong>read<\/strong>&nbsp;&#8211; \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u4f60\u53ef\u4ee5\u628a\u8bfb\u53d6\u7ed3\u679c\u8d4b\u7ed9\u4e00\u4e2a\u53d8\u91cf\u3002<\/li><li><strong>readline<\/strong>&nbsp;&#8211; \u53ea\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u7684\u4e00\u884c\u5185\u5bb9\u3002<\/li><li><strong>truncate<\/strong>&nbsp;&#8211; \u6e05\u7a7a\u6587\u4ef6\u3002\u6e05\u7a7a\u7684\u65f6\u5019\u8981\u5f53\u5fc3\u3002<\/li><li><strong>write(&#8216;stuff&#8217;)<\/strong>&nbsp;&#8211; \u7ed9\u6587\u4ef6\u5199\u5165\u4e00\u4e9b\u201c\u4e1c\u897f\u201d\u3002<\/li><li><strong>seek(0)<\/strong>&nbsp;&#8211; \u628a\u8bfb\/\u5199\u7684\u4f4d\u7f6e\u79fb\u5230\u6587\u4ef6\u6700\u5f00\u5934\u3002<\/li><li><\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p> \u7528 32 \u4e2a\u6bd4\u7279\u6765\u7f16\u7801\u4e00\u4e2a Unicode \u5b57\u7b26 \uff0c \u4e00\u4e2a 32 \u4f4d\u6bd4\u7279\u7684\u6570\u5b57\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u5b58\u50a8 4,294,967,295 \u4e2a\u5b57\u7b26\uff082^32\uff09, \u8fd9\u5bf9\u4efb\u4f55\u4e00\u79cd\u4eba\u7c7b\u8bed\u8a00\uff0c\u751a\u81f3\u5916\u661f\u8bed\u8a00\u6765\u8bf4\uff0c\u90fd\u591f\u7528\u4e86 <\/p>\n\n\n\n<p> next_lang = line.strip() <\/p>\n\n\n\n<p> raw_bytes = next_lang.encode(encoding, errors=errors) <\/p>\n\n\n\n<p>\n\ncooked_string = raw_bytes.decode(encoding, errors=errors)\n\n<\/p>\n\n\n\n<p> DBES\u201d\u8fd9\u4e2a\u8f85\u52a9\u8bb0\u5fc6\u8bcd\u5417\uff1f\u201cDecode Bytes, Encode Strings\u201d\uff0c\u89e3\u7801\u5b57\u8282==\u300b\u5b57\u7b26\u4e32\uff0c\u7f16\u7801\u5b57\u7b26\u4e32==\u300b\u5b57\u8282\uff08 \u6709\u5b57\u8282\u8981\u5b57\u7b26\u4e32\uff0c\u89e3\u7801\u5b57\u8282\uff0c\u6709\u5b57\u7b26\u4e32\u8981\u5b57\u8282\uff0c\u7f16\u7801\u5b57\u7b26\u4e32 \uff09\u3002 <\/p>\n\n\n\n<p> \u4f60\u9700\u8981\u505a\u7684\u5c31\u662f\u8bb0\u4f4f\u5982\u679c\u4f60\u6709\u539f\u59cb\u5b57\u8282\uff0c\u90a3\u4f60\u5fc5\u987b\u7528\u00a0<code>.decode()<\/code>\u00a0\u6765\u83b7\u53d6\u5b57\u7b26\u4e32\u3002 <\/p>\n\n\n\n<p>\u5173\u4e8ewhile\u8bed\u53e5<\/p>\n\n\n\n<ul><li>\u4fdd\u5b88\u4f7f\u7528 while-loop\uff0c\u901a\u5e38\u7528 for-loop \u66f4\u597d\u4e00\u4e9b\u3002<\/li><li>\u68c0\u67e5\u4e00\u4e0b\u4f60\u7684 while \u8bed\u53e5\uff0c\u786e\u4fdd\u5e03\u5c14\u6d4b\u8bd5\u6700\u7ec8\u4f1a\u5728\u67d0\u4e2a\u70b9\u7ed3\u679c\u4e3a False\u3002<\/li><li>\u5f53\u9047\u5230\u95ee\u9898\u7684\u65f6\u5019\uff0c\u628a\u4f60\u7684 while-loop \u5f00\u5934\u548c\u7ed3\u5c3e\u7684\u6d4b\u8bd5\u53d8\u91cf\u6253\u5370\u51fa\u6765\uff0c\u770b\u770b\u5b83\u4eec\u5728\u505a\u4ec0\u4e48\u3002\u5728\u8fd9\u4e2a\u7ec3\u4e60\u4e2d\uff0c\u4f60\u8981\u901a\u8fc7\u4ee5\u4e0b\u4e09\u4e2a\u68c0\u67e5\u6765\u5b66\u4e60 while-loop\uff1a<\/li><\/ul>\n\n\n\n<p>if\u8bed\u53e5\u7684\u7f16\u7a0b\u4e60\u60ef\uff0c<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3fcza0\">if \u8bed\u53e5\u7684\u89c4\u5219\uff1a<\/h2>\n\n\n\n<ul><li>\u6bcf\u4e00\u4e2a\u201cif \u8bed\u53e5\u201d\u5fc5\u987b\u5305\u542b\u4e00\u4e2a else\u3002<\/li><li>\u5982\u679c\u8fd9\u4e2a else \u6c38\u8fdc\u90fd\u4e0d\u5e94\u8be5\u88ab\u6267\u884c\u5230\uff0c\u56e0\u4e3a\u5b83\u672c\u8eab\u6ca1\u6709\u4efb\u4f55\u610f\u4e49\uff0c\u90a3\u4f60\u5fc5\u987b\u5728 else \u8bed\u53e5\u540e\u9762\u4f7f\u7528\u4e00\u4e2a\u53eb\u505a die \u7684\u51fd\u6570\uff0c\u8ba9\u5b83\u6253\u5370\u51fa\u9519\u8bef\u4fe1\u606f\u5e76\u4e14\u6b7b\u7ed9\u4f60\u770b\uff0c\u5c31\u50cf\u6211\u4eec\u4e0a\u8282\u8bfe\u505a\u7684\u90a3\u6837\uff0c\u6309\u7167\u8fd9\u4e2a\u601d\u8def\u4f60\u53ef\u4ee5\u627e\u5230\u5f88\u591a\u9519\u8bef\u3002<\/li><li>\u201cif \u8bed\u53e5\u201d\u7684\u5d4c\u5957\u4e0d\u8981\u8d85\u8fc7 2 \u5c42\uff0c\u6700\u597d\u5c3d\u91cf\u4fdd\u6301\u53ea\u6709 1 \u5c42\u3002<\/li><li>\u628a\u201cif \u8bed\u53e5\u201d\u5f53\u505a\u6bb5\u843d\u6765\u5bf9\u5f85\uff0c\u5176\u4e2d\u7684\u6bcf\u4e00\u4e2a&nbsp;<code>if, elif, else<\/code>&nbsp;\u5c31\u8ddf\u6bb5\u843d\u4e2d\u7684\u53e5\u5b50\u4e00\u6837\u3002\u5728\u6bcf\u53e5\u524d\u540e\u7559\u4e00\u4e2a\u7a7a\u884c\u4ee5\u4f5c\u533a\u5206\u3002<\/li><li>\u4f60\u7684\u5e03\u5c14\u6d4b\u8bd5\u5e94\u8be5\u5f88\u7b80\u5355\uff0c\u5982\u679c\u5b83\u4eec\u5f88\u590d\u6742\u7684\u8bdd\uff0c\u4f60\u9700\u8981\u5c06\u5b83\u4eec\u7684\u8fd0\u7b97\u4e8b\u5148\u653e\u5230\u4e00\u4e2a\u53d8\u91cf\u91cc\uff0c\u5e76\u4e14\u4e3a\u53d8\u91cf\u53d6\u4e00\u4e2a\u597d\u540d\u5b57\u3002<\/li><\/ul>\n\n\n\n<p>\u5982\u679c\u4f60\u9075\u5faa\u4e0a\u9762\u7684\u89c4\u5219\uff0c\u4f60\u5c31\u4f1a\u5199\u51fa\u6bd4\u5927\u591a\u6570\u7a0b\u5e8f\u5458\u90fd\u597d\u7684\u4ee3\u7801\u6765\u3002<\/p>\n\n\n\n<p>python\u7684\u6570\u636e\u7c7b\u578b<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" loading=\"lazy\" width=\"631\" height=\"458\" src=\"http:\/\/www.zhushiyao.com\/wp-content\/uploads\/2021\/01\/\u5fae\u4fe1\u622a\u56fe_20210121165224.png\" alt=\"\" class=\"wp-image-53986\"\/><\/figure>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>\/\/<\/td><td>\u5730\u677f\u9664\u6cd5\uff08\u5546\u5411\u4e0b\u53d6\u6574\uff09<\/td><td>2 \/\/ 4 = 0<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>\n\n\u7ee7\u627f\u7684\u5927\u591a\u6570\u7528\u6cd5\u90fd\u53ef\u4ee5\u7528\u7ec4\u5408\uff08composition\uff09\u6765\u7b80\u5316\u6216\u66ff\u6362\u3002\u5e76\u4e14\u65e0\u8bba\u5982\u4f55\u90fd\u8981\u907f\u514d\u591a\u91cd\u7ee7\u627f\n\n<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u4e09\u4e2a\u5173\u952e\u8bcd\u9700\u8981\u52a0\u5f3a\u4e00\u4e0b<\/p>\n\n\n\n<p><em>with-as<\/em>\u00a0\u8bed\u53e5<\/p>\n\n\n\n<p> lambda  \u521b\u5efa\u4e00\u4e2a\u77ed\u7684\u533f\u540d\u51fd\u6570 <\/p>\n\n\n\n<p> yield <\/p>\n\n\n\n<p>&#8212;&#8211;\u518d\u7ed3\u5408\u603b\u7ed3\u4e00\u4e0b&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n\n\n\n<p>1\uff1aprint() \u53d8\u6210\u4e86\u51fd\u6570<\/p>\n\n\n\n<p>2:  \u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u4e0d\u9700\u8981 u&#8221;xxxx&#8221; \u8fd9\u6837\u5b50\u4e86<\/p>\n\n\n\n<p>3\uff1a\u589e\u52a0\u4e00\u4e2a\u5730\u677f\u9664\u8fd0\u7b97\u7b26 \/\/ \uff0c \u5982 9\/\/4 = 2<\/p>\n\n\n\n<p>4\uff1a\u5f02\u5e38\u5904\u7406 \uff0c \u53ea\u6709\u7ee7\u627f BaseException \u7684\u5bf9\u8c61\u624d\u53ef\u4ee5\u88ab\u629b\u51fa <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n  raise TypeError(\"xxx\")\nexcpt TypeError as err:\n  print(err)<\/code><\/pre>\n\n\n\n<p>5\uff1a\u4e0d\u7b49\u4e8e\u8fd0\u7b97\u7b26\u53ea\u80fd\u4f7f\u7528 !=<\/p>\n\n\n\n<p>6\uff1a\u589e\u52a0bytes\u7c7b\u578b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>b=b'\u4e2d\u56fd'\ntype(b)\ns = b.decode()\nb2 = s.encode()<\/code><\/pre>\n\n\n\n<p>\u5176\u4ed6\u53ea\u6709\u7b49\u5b9e\u9645\u4e2d\u906d\u9047\u5230\u518d\u8be6\u7ec6\u4e86\u89e3\uff0c\u76ee\u524d\u611f\u89c9python2\u5347\u5230python3\u5e76\u4e0d\u662f\u592a\u5927\u7684\u53d8\u66f4<\/p>\n\n\n\n<p>\u5e94\u8be5\u95ee\u9898\u4e0d\u5927\u7684<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u560e\u597d\uff0c\u534a\u540a\u5b50\u7a0b\u5e8f\u5458\u4ece\u7f51\u7ebf\u7684\u8fd9\u4e00\u7aef\u5411\u60a8\u95ee\u597d\u3002 \u8fc7\u53bb\u7684\u8fd8\u662f\u4f1a\u8fc7\u53bb\uff0c\u5c31\u50cf\u65f6\u95f4\uff0c\u5c31\u50cf\u6d41\u6c34\uff0c\u5c31\u50cfpython 2.7  &hellip; <a href=\"http:\/\/www.zhushiyao.com\/?p=52144\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u4ecepython2.7\u5411 python3 \u8f6c\u6362\uff08by\u300a\u7b28\u529e\u6cd5\u5b66python3\u300b\uff09\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[8],"_links":{"self":[{"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=\/wp\/v2\/posts\/52144"}],"collection":[{"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=52144"}],"version-history":[{"count":20,"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=\/wp\/v2\/posts\/52144\/revisions"}],"predecessor-version":[{"id":54662,"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=\/wp\/v2\/posts\/52144\/revisions\/54662"}],"wp:attachment":[{"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=52144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=52144"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zhushiyao.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=52144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}