{"id":2192,"date":"2017-05-09T11:32:00","date_gmt":"2017-05-09T03:32:00","guid":{"rendered":"http:\/\/cn.hostease.com\/xueyuan\/?p=2192"},"modified":"2017-05-09T11:32:39","modified_gmt":"2017-05-09T03:32:39","slug":"python%e5%9f%ba%e7%a1%8005-%e7%bc%a9%e8%bf%9b%e5%92%8c%e9%80%89%e6%8b%a9","status":"publish","type":"post","link":"https:\/\/cn.hostease.com\/xueyuan\/jishu\/python\/python%e5%9f%ba%e7%a1%8005-%e7%bc%a9%e8%bf%9b%e5%92%8c%e9%80%89%e6%8b%a9\/","title":{"rendered":"Python\u57fa\u784005 \u7f29\u8fdb\u548c\u9009\u62e9"},"content":{"rendered":"<p>Python\u6700\u5177\u7279\u8272\u7684\u662f\u7528\u7f29\u8fdb\u6765\u6807\u660e\u6210\u5757\u7684\u4ee3\u7801\u3002\u6211\u4e0b\u9762\u4ee5if\u9009\u62e9\u7ed3\u6784\u6765\u4e3e\u4f8b\u3002if\u540e\u9762\u8ddf\u968f\u6761\u4ef6\uff0c\u5982\u679c\u6761\u4ef6\u6210\u7acb\uff0c\u5219\u6267\u884c\u5f52\u5c5e\u4e8eif\u7684\u4e00\u4e2a\u4ee3\u7801\u5757\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u5148\u770bC\u8bed\u8a00\u7684\u8868\u8fbe\u65b9\u5f0f\uff08\u6ce8\u610f\uff0c\u8fd9\u662fC\uff0c\u4e0d\u662fPython!\uff09<\/p>\n<div class=\"cnblogs_code\">\n<pre>if ( i &gt; 0 )\r\n{\r\n    x = 1;\r\n    y = 2;\r\n}<\/pre>\n<\/div>\n<p>\u5982\u679ci &gt; 0\u7684\u8bdd\uff0c\u6211\u4eec\u5c06\u8fdb\u884c\u62ec\u53f7\u4e2d\u6240\u5305\u62ec\u7684\u4e24\u4e2a\u8d4b\u503c\u64cd\u4f5c\u3002\u62ec\u53f7\u4e2d\u5305\u542b\u7684\u5c31\u662f\u5757\u64cd\u4f5c\uff0c\u5b83\u96b6\u5c5e\u4e8eif\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u5728Python\u4e2d\uff0c\u540c\u6837\u7684\u76ee\u7684\uff0c\u8fd9\u6bb5\u8bdd\u662f\u8fd9\u6837\u7684<\/p>\n<div class=\"cnblogs_code\">\n<pre>if i &gt; 0:\r\n    x = 1\r\n    y = 2<\/pre>\n<\/div>\n<p>\u5728Python\u4e2d\uff0c \u53bb\u6389\u4e86i &gt; 0\u5468\u56f4\u7684\u62ec\u53f7\uff0c\u53bb\u9664\u4e86\u6bcf\u4e2a\u8bed\u53e5\u53e5\u5c3e\u7684\u5206\u53f7\uff0c\u8868\u793a\u5757\u7684\u82b1\u62ec\u53f7\u4e5f\u6d88\u5931\u4e86\u3002<\/p>\n<p>\u591a\u51fa\u6765\u4e86if &#8230;\u4e4b\u540e\u7684:(\u5192\u53f7), \u8fd8\u6709\u5c31\u662fx = 1 \u548c y =2\u524d\u9762\u6709\u56db\u4e2a\u7a7a\u683c\u7684\u7f29\u8fdb\u3002\u901a\u8fc7\u7f29\u8fdb\uff0cPython\u8bc6\u522b\u51fa\u8fd9\u4e24\u4e2a\u8bed\u53e5\u662f\u96b6\u5c5e\u4e8eif\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>Python\u8fd9\u6837\u8bbe\u8ba1\u7684\u7406\u7531\u7eaf\u7cb9\u662f\u4e3a\u4e86\u7a0b\u5e8f\u597d\u770b\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>if\u8bed\u53e5<\/h3>\n<p>\u5199\u4e00\u4e2a\u5b8c\u6574\u7684\u7a0b\u5e8f\uff0c\u547d\u540d\u4e3aifDemo.py\u3002\u8fd9\u4e2a\u7a0b\u5e8f\u7528\u4e8e\u5b9e\u73b0if\u7ed3\u6784\u3002<\/p>\n<div class=\"cnblogs_code\">\n<pre>i = 1\r\nx = 1\r\nif i &gt; 0:\r\n    x = x+1\r\nprint x<\/pre>\n<\/div>\n<p>$python ifDemo.py\u00a0 # \u8fd0\u884c<\/p>\n<p>\u7a0b\u5e8f\u8fd0\u884c\u5230if\u7684\u65f6\u5019\uff0c\u6761\u4ef6\u4e3aTrue\uff0c\u56e0\u6b64\u6267\u884cx = x+1,\u3002<\/p>\n<p>print x\u8bed\u53e5\u6ca1\u6709\u7f29\u8fdb\uff0c\u90a3\u4e48\u5c31\u662fif\u4e4b\u5916\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u5982\u679c\u5c06\u7b2c\u4e00\u53e5\u6539\u6210i = -1\uff0c\u90a3\u4e48if\u9047\u5230\u5047\u503c (False), x = x+1\u96b6\u5c5e\u4e8eif, \u8fd9\u4e00\u53e5\u8df3\u8fc7\u3002 print x\u6ca1\u6709\u7f29\u8fdb\uff0c\u662fif\u4e4b\u5916\uff0c\u4e0d\u8df3\u8fc7\uff0c\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u8fd9\u79cd\u4ee5\u56db\u4e2a\u7a7a\u683c\u7684\u7f29\u8fdb\u6765\u8868\u793a\u96b6\u5c5e\u5173\u7cfb\u7684\u4e66\u5199\u65b9\u5f0f\uff0c\u4ee5\u540e\u8fd8\u4f1a\u770b\u5230\u3002\u5f3a\u5236\u7f29\u8fdb\u589e\u5f3a\u4e86\u7a0b\u5e8f\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u590d\u6742\u4e00\u4e9b\u7684if\u9009\u62e9\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<pre>i = 1\r\nif i &gt; 0:\r\n\u00a0 \u00a0 print 'positive i'\r\n    i = i + 1\r\nelif i == 0:\r\n\u00a0 \u00a0 print 'i is 0'\r\n    i = i * 10\r\nelse:\r\n\u00a0 \u00a0 print 'negative i'\r\n    i = i - 1\r\nprint 'new i:',i<\/pre>\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<\/div>\n<p>\u8fd9\u91cc\u6709\u4e09\u4e2a\u5757\uff0c\u5206\u522b\u5c5e\u4e8eif, elif, else\u5f15\u9886\u3002<br \/>\nPython\u68c0\u6d4b\u6761\u4ef6\uff0c\u5982\u679c\u53d1\u73b0if\u7684\u6761\u4ef6\u4e3a\u5047\uff0c\u90a3\u4e48\u8df3\u8fc7\u540e\u9762\u7d27\u8ddf\u7684\u5757\uff0c\u68c0\u6d4b\u4e0b\u4e00\u4e2aelif\u7684\u6761\u4ef6\uff1b \u5982\u679c\u8fd8\u662f\u5047\uff0c\u90a3\u4e48\u6267\u884celse\u5757\u3002<br \/>\n\u901a\u8fc7\u4e0a\u9762\u7684\u7ed3\u6784\u5c06\u7a0b\u5e8f\u5206\u51fa\u4e09\u4e2a\u5206\u652f\u3002\u7a0b\u5e8f\u6839\u636e\u6761\u4ef6\uff0c\u53ea\u6267\u884c\u4e09\u4e2a\u5206\u652f\u4e2d\u7684\u4e00\u4e2a\u3002<\/p>\n<p>\u6574\u4e2aif\u53ef\u4ee5\u653e\u5728\u53e6\u4e00\u4e2aif\u8bed\u53e5\u4e2d\uff0c\u4e5f\u5c31\u662fif\u7ed3\u6784\u7684\u5d4c\u5957\u4f7f\u7528\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<pre>i  = 5\r\nif i &gt; 1:\r\n    print 'i bigger than 1'\r\n    print 'good'\r\n    if i &gt; 2:\r\n        print 'i bigger than 2'\r\n        print 'even better'<\/pre>\n<div class=\"cnblogs_code_toolbar\"><\/div>\n<\/div>\n<p>if i &gt; 2 \u540e\u9762\u7684\u5757\u76f8\u5bf9\u4e8e\u8be5if\u7f29\u8fdb\u4e86\u56db\u4e2a\u7a7a\u683c\uff0c\u4ee5\u8868\u660e\u5176\u96b6\u5c5e\u4e8e\u8be5if\uff0c\u800c\u4e0d\u662f\u5916\u5c42\u7684if\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>if\u8bed\u53e5\u4e4b\u540e\u7684\u5192\u53f7<\/p>\n<p>\u4ee5\u56db\u4e2a\u7a7a\u683c\u7684\u7f29\u8fdb\u6765\u8868\u793a\u96b6\u5c5e\u5173\u7cfb, Python\u4e2d\u4e0d\u80fd\u968f\u610f\u7f29\u8fdb<\/p>\n<p>if\u00a0 &lt;\u6761\u4ef61&gt;:<\/p>\n<p>statement<\/p>\n<p>elif &lt;\u6761\u4ef62&gt;:<\/p>\n<p>statement<\/p>\n<p>elif &lt;\u6761\u4ef63&gt;\uff1a<\/p>\n<p>statement<\/p>\n<p>else:<\/p>\n<p>statement<\/p>\n<p>&nbsp;<\/p>\n<p>\u4f5c\u8005\uff1aVamei \u51fa\u5904\uff1ahttps:\/\/www.cnblogs.com\/vamei<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u6700\u5177\u7279\u8272\u7684\u662f\u7528\u7f29\u8fdb\u6765\u6807\u660e\u6210\u5757\u7684\u4ee3\u7801\u3002\u6211\u4e0b\u9762\u4ee5if\u9009\u62e9\u7ed3\u6784\u6765\u4e3e\u4f8b\u3002if\u540e\u9762\u8ddf\u968f\u6761\u4ef6\uff0c\u5982\u679c\u6761\u4ef6\u6210\u7acb\uff0c\u5219 &#8230; <a title=\"Python\u57fa\u784005 \u7f29\u8fdb\u548c\u9009\u62e9\" class=\"read-more\" href=\"https:\/\/cn.hostease.com\/xueyuan\/jishu\/python\/python%e5%9f%ba%e7%a1%8005-%e7%bc%a9%e8%bf%9b%e5%92%8c%e9%80%89%e6%8b%a9\/\" aria-label=\"\u9605\u8bfb Python\u57fa\u784005 \u7f29\u8fdb\u548c\u9009\u62e9\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[654],"tags":[657],"class_list":["post-2192","post","type-post","status-publish","format-standard","hentry","category-python","tag-python"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/2192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/comments?post=2192"}],"version-history":[{"count":2,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/2192\/revisions"}],"predecessor-version":[{"id":2194,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/2192\/revisions\/2194"}],"wp:attachment":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/media?parent=2192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/categories?post=2192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/tags?post=2192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}