{"id":5867,"date":"2017-08-28T00:51:17","date_gmt":"2017-08-27T16:51:17","guid":{"rendered":"https:\/\/sdeno.com\/?p=5867"},"modified":"2017-09-06T18:06:23","modified_gmt":"2017-09-06T10:06:23","slug":"python%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=5867","title":{"rendered":"python\u7b14\u8bb0"},"content":{"rendered":"<p><span style=\"color: #ff0000;\">0<\/span>\uff0c\u6267\u884cpython<\/p>\n<pre>python hello.py<\/pre>\n<p>\u6216\u8005<\/p>\n<pre>\/\/hello.py\r\n#!\/usr\/bin\/env python \/\/\u5728\u8981\u6267\u884c\u7684\u6587\u4ef6\u6700\u524d\u9762\u52a0\u4e0a\uff0c\u8fd9\u6837\u53ef\u4ee5\u7701\u53bb\u5199python\r\n# -*- coding: utf-8 -*- \/\/\u4e0d\u5199\u8fd9\u4e2a\u8f93\u51fa\u7684\u4e2d\u6587\u53ef\u80fd\u51fa\u73b0\u4e71\u7801\r\n...\u4ee3\u7801\r\n<\/pre>\n<pre>chmod a+x hello.py \/\/\u7ed9\u6267\u884c\u7684\u6743\u9650\r\n.\/hello.py \/\/\u76f4\u63a5\u6267\u884c\u4e0d\u9700\u8981\u5728\u524d\u9762\u52a0python\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">1\uff0c<\/span>\u53d8\u91cf \u8ddfjs\u4e00\u6837<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">2\uff0c<\/span>\u8fd0\u7b97\u7b26<br \/>\n\u6574\u6570\u548c\u6574\u6570\u8fdb\u884c\u8fd0\u7b97\u65f6\u53ea\u80fd\u53d6\u6574\uff0c\u4f8b\u5982\uff1a<br \/>\n3\/2 = 1<\/p>\n<p>\u5982\u679c\u60f3\u4fdd\u7559\u5c0f\u6570\u90a3\u4e48\u5c31\uff1a<br \/>\n3.0\/2=1.5<br \/>\n3.0\/\/2=1<\/p>\n<p>\u5e73\u65b9<br \/>\n3**2=9<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">3,<\/span>\u7c7b\u578b\uff08python\u53ef\u4ee5\u6839\u636e\u503c\u6765\u5b9a\u4e49\u7c7b\u578b\uff09<br \/>\n\u6574\u6570int -2,147,483,648\u52302,147,483,647<br \/>\n\u4f8b\u5982\uff1anum=123123 \/\/\u9ed8\u8ba4int,\u5982\u679c\u957f\u5ea6\u8d85\u8fc7int,\u7c7b\u578b\u5c31\u4f1a\u81ea\u52a8\u53d8\u4e3along\u7c7b\u578b<\/p>\n<p>&nbsp;<\/p>\n<p>\u6d6e\u70b9\u6570float<br \/>\n\u4f8b\u5982\uff1anum=1.0<\/p>\n<p>&nbsp;<\/p>\n<p>\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982<br \/>\nstr=&#8217;abcde&#8217;<\/p>\n<p>&nbsp;<\/p>\n<p>\u5e03\u5c14\u7c7b\u578b<br \/>\nTrue False (\u5728\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u65f6\uff0c\u9996\u5b57\u6bcd\u4e00\u5b9a\u8981\u5927\u5199)<\/p>\n<p>&nbsp;<\/p>\n<p>\u5b57\u7b26\u4e32\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<br \/>\nstr=&#8217;abcde&#8217;<br \/>\n[] \/\/\u83b7\u53d6\u5b57\u7b26\u4e32\u6307\u5b9a\u5185\u5bb9<br \/>\nlen() \/\/\u8fd4\u56de\u957f\u5ea6<br \/>\n+ \/\/\u62fc\u63a5<br \/>\n* \/\/\u91cd\u590d\u51e0\u6b21<br \/>\nin \/\/\u5224\u65ad\u662f\u5426\u5728\u5b57\u7b26\u4e32\u4e2d\uff0c\u8fd4\u56de\u5e03\u5c14\u503c<br \/>\nmax() \/\/\u8fd4\u56de\u6700\u5927\u503c<br \/>\nmin()<br \/>\ncmp() \/\/\u5b57\u7b26\u4e32\u4e4b\u95f4\u6bd4\u8f83\uff0c\u8fd4\u56de\u503c\u67091\uff0c-1\uff0c0<\/p>\n<p>&nbsp;<\/p>\n<p>\u6ce8\u610f\uff1a\u5f53\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u53d8\u91cfstr=&#8217;123&#8217;\u5373\u4f7f\u6539\u53d8\u4e86\u503c\u4e3astr=&#8217;111&#8242;,\u5176\u5b9e\u4e5f\u662f\u91cd\u65b0\u5f00\u8f9f\u4e86\u4e00\u4e2a\u4eba\u5185\u5b58\u7a7a\u95f4\uff0c\u4e5f\u5c31\u662f\u8bf4<br \/>\n\u6211\u4eec\u5e76\u6ca1\u6709\u4fee\u6539\u4e4b\u524d\u7684\u503c\uff0c\u53ea\u662f\u53e6\u5916\u5f00\u542f\u4e86\u4e00\u4e2a\u65b0\u7684\u5185\u5b58\u7a7a\u95f4\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">4\uff0c<\/span>\u5143\u7ec4\uff08\u7c7b\u4f3c\u6570\u7ec4\uff09<br \/>\nt=() \/\/\u7a7a\u5143\u7ec4<br \/>\nt1=(1,) \/\/\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\u7684\u5143\u7ec4\uff0c\u5fc5\u987b\u52a0\u9017\u53f7<br \/>\nt2=(&#8216;aa&#8217;,123) \/\/\u4e00\u822c\u5143\u7ec4<br \/>\nt2[0] \/\/\u83b7\u53d6\u5143\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20<br \/>\na,b=(&#8216;aa&#8217;,123) \/\/\u5c06\u5143\u7ec4\u5185\u5bb9\u5bf9\u5e94\u58f0\u660e\u7ed9\u4e00\u4e2a\u53d8\u91cf<br \/>\n\u5143\u7ec4\u4e00\u65e6\u521b\u5efa\u4e86\u5c31\u4e0d\u80fd\u4fee\u6539\uff0c\u5426\u5219\u62a5\u9519<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">5\uff0c<\/span>\u5217\u8868 \uff08\u7c7b\u4f3c\u6570\u7ec4\uff09<br \/>\nt1=[1] \/\/\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\u7684\u5143\u7ec4<br \/>\nt1[0] \/\/\u83b7\u53d6\u5217\u8868\u7b2c\u4e00\u4e2a\u5143\u7d20<br \/>\nt1.append(&#8216;hello&#8217;) \/\/\u5728\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20\u4e3ahello<br \/>\nt1.remove(&#8216;hello&#8217;) \/\/\u5220\u9664\u6307\u5b9a\u5185\u5bb9<br \/>\ndel(t1[0]) \/\/\u5220\u9664t1\u7b2c\u4e00\u4e2a\u5143\u7d20<br \/>\n\u5217\u8868\u91cc\u9762\u7684\u5143\u7d20\u662f\u53ef\u4ee5\u4fee\u6539\u7684\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">6\uff0c<\/span>\u5b57\u5178 \uff08\u7c7b\u4f3c\u5bf9\u8c61\uff09<br \/>\nobj={&#8216;name&#8217;:&#8217;chen&#8217;}<br \/>\nobj[&#8216;name&#8217;] \/\/\u53d6\u503c<br \/>\nobj[&#8216;name&#8217;]=&#8217;ge&#8217; \/\/\u4fee\u6539<br \/>\nobj[&#8216;age&#8217;]=11 \/\/\u6dfb\u52a0\u989d\u5916\u7d22\u5f15\u503c(\u4e0d\u4e00\u5b9a\u6dfb\u52a0\u5230\u672b\u5c3e)<br \/>\ndel(obj[&#8216;name&#8217;]) \/\/\u5220\u9664\u6307\u5b9a\u7d22\u5f15<br \/>\nobj.pop(&#8216;name&#8217;) \/\/\u5220\u9664\u6307\u5b9a\u7d22\u5f15\uff0c\u8fd4\u56de\u5220\u9664\u7684\u503c<br \/>\nobj.clear() \/\/\u5220\u9664obj\u91cc\u9762\u6240\u6709\u5185\u5bb9<\/p>\n<p>\u5b57\u5178\uff08\u65e2\u662f\u5bf9\u8c61\uff09\u6709\u81ea\u5df1\u7684\u65b9\u6cd5\uff1a<br \/>\n\/\/\u6e05\u9664\u5b57\u5178\u6240\u6709\u5185\u5bb9<br \/>\nobj.clear()<\/p>\n<p>\/\/\u53d6\u503c\uff0c\u5982\u679c\u503c\u4e0d\u5b58\u5728\u8fd4\u56de\u81ea\u5b9a\u4e49\u7684\u5185\u5bb9<br \/>\nobj.get(5,&#8217;\u81ea\u5b9a\u4e49\u5185\u5bb9&#8217;)<\/p>\n<p>\/\/\u8fd4\u56de\u5b57\u5178\u7684\u5168\u90e8\u7d22\u5f15\u503c\uff0c\u4ee5\u5217\u8868\u7c7b\u578b\u8fd4\u56de<br \/>\nobj.keys()<\/p>\n<p>\/\/\u8fd4\u56de\u5b57\u5178\u6240\u6709\u503c\uff0c\u4ee5\u5217\u8868\u7c7b\u578b\u8fd4\u56de<br \/>\nobj.values()<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">7\uff0c<\/span>\u8bed\u6cd5<\/p>\n<p>*python\u7684\u8bed\u6cd5\u6ca1\u6709\u82b1\u62ec\u53f7{}\u6765\u533a\u5206\u4ee3\u7801\u5757\uff0c\u6240\u4ee5\u7528\u7f29\u8fdb\u7684\u5f62\u5f0f\u8fdb\u884c\u533a\u5206\u3002\u7c7b\u4f3cjade\u6a21\u677f\u4e00\u6837\u3002<br \/>\n\u8fde\u63a5\u7b26\u53f7\u662f+<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">8,<\/span>\u903b\u8f91\u8fd0\u7b97\u7b26<\/p>\n<p>and or not<br \/>\n\u7c7b\u4f3cjs\u7684<br \/>\n&amp;&amp; || !<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">9,<\/span>\u6761\u4ef6\u8bed\u53e5<br \/>\nif else<\/p>\n<p>if True:<br \/>\nprint 1<br \/>\nelse:<br \/>\nprint 2<\/p>\n<p>&nbsp;<\/p>\n<p>if 1==1:<\/p>\n<p>elif 2==2:<\/p>\n<p>else:<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">10,<\/span>\u904d\u5386<\/p>\n<p>for x in &#8220;abcd&#8221;:<br \/>\nprint x+&#8221;1&#8243; \/\/\u5faa\u73af4\u6b21<\/p>\n<p>&nbsp;<\/p>\n<p>for x in [0,1,2,3]:<br \/>\nprint x+&#8221;1&#8243; \/\/\u5faa\u73af4\u6b21<\/p>\n<p>&nbsp;<\/p>\n<p>range(5) \/\/[0,1,2,3,4]<br \/>\nrange(1,6) \/\/[1,2,3,4,5]<br \/>\nrange(1,6,2)\/\/[1,3,5]<\/p>\n<p>&nbsp;<\/p>\n<p>for x in range(100):<br \/>\nprint x+&#8221;1&#8243; \/\/\u5faa\u73af100\u6b21<\/p>\n<p>&nbsp;<\/p>\n<p>(\u7c7b\u4f3cjs\u7684for(){}\u8bed\u6cd5)<br \/>\nx=0<br \/>\nwhile x&lt;10:<br \/>\nprint &#8220;1&#8221;<br \/>\nx+=1<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">11\uff0c<\/span>\u51fd\u6570<\/p>\n<p>def run(a,b):<br \/>\nc=a+b<br \/>\nprint c<\/p>\n<p>run(1,1)<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">12,<\/span>\u4f5c\u7528\u57df(\u8ddfjs\u7684\u7406\u89e3\u4e00\u6837\uff0c\u91cd\u70b9\u662f\u5982\u4f55\u8ba9\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\u53d8\u4e3a\u5168\u5c40\u53d8\u91cf)<\/p>\n<p>\/\/\u58f0\u660e<br \/>\ndef run():<br \/>\nglobal x<br \/>\nx=123<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/\u8c03\u7528<br \/>\nrun()<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">13,<\/span>\u533f\u540d\u51fd\u6570lambda\uff0c\uff08\u4f5c\u7528\u662f\u8ba9\u51fd\u6570\u53d8\u7684\u7b80\u5355\uff0c\u7c7b\u4f3c\u4e8ejs\u7684\u4e09\u5143\u8868\u8fbe\u5f0f\u662fif else\u7684\u7b80\u5199\uff09<\/p>\n<p>g=lambda x:1+x<br \/>\nprint g(3) \/\/ 4<\/p>\n<p>&nbsp;<\/p>\n<p>\u76f8\u5f53\u4e8e<\/p>\n<p>&nbsp;<\/p>\n<p>def g(x):<br \/>\nreturn x+1<br \/>\nprint g(3) \/\/ 4<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">14,<\/span>python\u81ea\u5e26\u51fd\u6570\uff0c\uff08\u6709\u5f88\u591a\uff0c\u8fd9\u91cc\u6211\u4eec\u63a8\u8350\u5e38\u7528\u7684\uff09<\/p>\n<p>http:\/\/www.jb51.net\/article\/60896.htm<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">15\uff0c<\/span>\u6a21\u5757<\/p>\n<p>\/\/\u88ab\u8c03\u7528\u5305 a.py<br \/>\ndef g(x):<br \/>\nreturn x+1<\/p>\n<p>if __name__==&#8217;__main__&#8217;:<br \/>\nprint &#8216;ziji&#8217; \/\/a.py\u8c03\u7528\u81ea\u5df1\u65f6\u89e6\u53d1<br \/>\nelif __name__==&#8217;a&#8217;:<br \/>\nprint &#8216;zhiyou_a&#8217; \/\/\u53ea\u6709\u5f15\u5165a.py\u65f6\u88ab\u89e6\u53d1<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/\u8c03\u7528\u5305 b.py<br \/>\nimport a<br \/>\nprint a.g(3)<\/p>\n<p>&nbsp;<\/p>\n<p>\u5982\u679c\u5e0c\u671b\u88ab\u6307\u5b9a\u7684\u5305\u8c03\u7528\u90a3\u4e48\u5c31\u5229\u7528__name__\u505a\u5224\u65ad<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">16\uff0c<\/span>\u5305\uff0c\u591a\u4e2a\u6a21\u5757\u5b58\u653e\u5728\u4e00\u4e2a\u6587\u4ef6\u5939\u4e0b\u5c31\u7ec4\u6210\u4e00\u4e2a\u5305<\/p>\n<p>\u5728\u4e00\u4e2a\u6587\u4ef6\u5939\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u5b57\u4e3a__init__.py<\/p>\n<p>&nbsp;<\/p>\n<p>1\uff0c\u5728\u4e00\u4e2a\u6587\u4ef6\u5939\u540d\u4e3abao\u4e0b\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u540d\u4e3a__init__.py\u7684\u6587\u4ef6<\/p>\n<p>2\uff0c\u5728bao\u6587\u4ef6\u5939\u4e0b\u521b\u5efa\u4e00\u4e2aa.py\u5185\u5bb9\u662f<br \/>\ndef g(x):<br \/>\nreturn x+1<\/p>\n<p>3\uff0c\u8c03\u7528\u5305\u91cc\u9762\u7684a.py\u91cc\u9762\u7684g\u51fd\u6570<\/p>\n<p>import bao.a \/\/\u5305\u540d\u5c31\u662f\u6587\u4ef6\u5939\u540d<br \/>\nprint bao.a.go(2)<\/p>\n<p>&nbsp;<\/p>\n<p>\u5173\u4e8e\u5bfc\u5165\u5305\u7684\u65b9\u5f0f\uff1a<br \/>\n1\uff0cimport \u5305\/\u6a21\u5757<\/p>\n<p>2, import \u5305\/\u6a21\u5757 as \u81ea\u5b9a\u4e49\u540d<\/p>\n<p>3, from \u5305\/\u6a21\u5757 import \u6a21\u5757\/\u51fd\u6570\u540d \uff08\u63a8\u8350\u4f7f\u7528\uff09<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">17\uff0c<\/span>\u6b63\u5219<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">18\uff0c<\/span>\u672c\u5730\u6587\u4ef6\u8bfb\u3001\u5199\u64cd\u4f5c<\/p>\n<p>r \u53ea\u8bfb<br \/>\nr+ \u8bfb\u5199\uff0c\u76f8\u5f53\u4e8e\u8ffd\u52a0\u5185\u5bb9\u3002\u6ce8\u610f,\u5fc5\u987b\u6253\u5f00\u6587\u4ef6\u540e\u5728\u8bfb\u53d6\u5185\u5bb9\u540e\u5728\u5199\u5165\uff0c\u8fd9\u6837\u624d\u662f\u8ffd\u52a0\u6548\u679c\u3002\u5982\u679c\u6253\u5f00\u540e\u5c31\u76f4\u63a5\u5199\u5165\u5c31\u662f\u8986\u76d6\u539f\u6765\u5185\u5bb9\u6548\u679c\u4e86\u3002<br \/>\nw \u5199\u5165\uff0c\u5148\u5220\u9664\u6587\u4ef6\u540e\u5199\u5165\uff0c\u5982\u679c\u6ca1\u6587\u4ef6\u5c31\u521b\u5efa \uff08\u4f1a\u5220\u9664\u6587\u4ef6\u614e\u7528\uff09<br \/>\nw+ \u8bfb\u5199\uff0c\u5148\u5220\u9664\u6587\u4ef6\u540e\u5199\u5165\uff0c\u5982\u679c\u6ca1\u6587\u4ef6\u5c31\u521b\u5efa\uff08\u4f1a\u5220\u9664\u6587\u4ef6\u614e\u7528\uff09<\/p>\n<p>&nbsp;<\/p>\n<p>fo=open(&#8216;\/root\/1.txt&#8217;,&#8217;r+&#8217;) \/\/\u6253\u5f00\u6587\u4ef6<br \/>\nfo.read() \/\/\u67e5\u770b\u5185\u5bb9<br \/>\nfo.write(&#8220;\/n hahahah&#8221;) \/\/\u8ffd\u52a0\u5185\u5bb9<br \/>\nfo.close() \/\/\u770b\u5b8c\u540e\u5173\u95ed<\/p>\n<p><a href=\"https:\/\/sdeno.com\/wp-content\/uploads\/2017\/08\/python.txt\">python<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>0\uff0c\u6267\u884cpython python hello.py \u6216\u8005 \/\/hello.py #!\/usr\/bin\/env [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[],"class_list":["post-5867","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5867"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5867\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}