{"id":549,"date":"2020-12-22T01:11:41","date_gmt":"2020-12-21T17:11:41","guid":{"rendered":"http:\/\/www.mimiq.com.cn\/?p=549"},"modified":"2020-12-22T01:14:19","modified_gmt":"2020-12-21T17:14:19","slug":"deepops%e7%9a%84python%e5%b0%8f%e7%ac%94%e8%ae%b0-%e5%a4%a9%e6%b1%a0%e9%be%99%e7%8f%a0%e8%ae%a1%e5%88%92-python%e8%ae%ad%e7%bb%83%e8%90%a5-task-01day2","status":"publish","type":"post","link":"http:\/\/www.mimiq.com.cn\/?p=549","title":{"rendered":"DeepOps\u7684Python\u5c0f\u7b14\u8bb0-\u5929\u6c60\u9f99\u73e0\u8ba1\u5212-Python\u8bad\u7ec3\u8425-Task 01:DAY2"},"content":{"rendered":"\n<p>\u5f88\u5feb\u5230\u4e86\u7b2c\u4e8c\u5929 \u6761\u4ef6\u8bed\u53e5\u4e0e\u5faa\u73af\u8bed\u53e5<\/p>\n\n\n\n<h2>\u6761\u4ef6\u8bed\u53e5<\/h2>\n\n\n\n<h2>if \u8bed\u53e5<\/h2>\n\n\n\n<ul><li>if &#8211; else \u8bed\u53e5<\/li><li>if &#8211; elif &#8211; else \u8bed\u53e5<\/li><li>assert \u5173\u952e\u8bcd<ul><li>assert len(my_list) &gt; 0<\/li><\/ul><\/li><\/ul>\n\n\n\n<h2>\u5faa\u73af\u8bed\u53e5<\/h2>\n\n\n\n<h2>while \u5faa\u73af<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>- while - else \u5faa\u73af\n\t- \u8fd9\u4e2a\u6211\u5f88\u5c11\u7528\u8fc7\uff0c \u5f53while\u5faa\u73af\u6b63\u5e38\u6267\u884c\u5b8c\u7684\u60c5\u51b5\u4e0b\uff0c\u6267\u884celse\u8f93\u51fa\uff0c\u5982\u679cwhile\u5faa\u73af\u4e2d\u6267\u884c\u4e86\u8df3\u51fa\u5faa\u73af\u7684\u8bed\u53e5\uff0c\u6bd4\u5982 break\uff0c\u5c06\u4e0d\u6267\u884celse\u4ee3\u7801\u5757\u7684\u5185\u5bb9\u3002\nwhile \u5e03\u5c14\u8868\u8fbe\u5f0f:\n\t\u4ee3\u7801\u5757\nelse:\n\t\u4ee3\u7801\u5757<\/code><\/pre>\n\n\n\n<h2>for \u5faa\u73af<\/h2>\n\n\n\n<ul><li>for &#8211; else \u5faa\u73af<\/li><li>\u5f53for\u5faa\u73af\u6b63\u5e38\u6267\u884c\u5b8c\u7684\u60c5\u51b5\u4e0b\uff0c\u6267\u884celse\u8f93\u51fa\uff0c\u5982\u679cfor\u5faa\u73af\u4e2d\u6267\u884c\u4e86\u8df3\u51fa\u5faa\u73af\u7684\u8bed\u53e5\uff0c\u6bd4\u5982 break\uff0c\u5c06\u4e0d\u6267\u884celse\u4ee3\u7801\u5757\u7684\u5185\u5bb9\uff0c\u4e0ewhile &#8211; else\u8bed\u53e5\u4e00\u6837\u3002<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>for \u8fed\u4ee3\u53d8\u91cf in \u53ef\u8fed\u4ee3\u5bf9\u8c61:\n     \u4ee3\u7801\u5757\nelse:\n     \u4ee3\u7801\u5757<\/code><\/pre>\n\n\n\n<h2>range() \u51fd\u6570<\/h2>\n\n\n\n<h2>enumerate()\u51fd\u6570<\/h2>\n\n\n\n<h2>sequence\uff1a\u4e00\u4e2a\u5e8f\u5217\u3001\u8fed\u4ee3\u5668\u6216\u5176\u4ed6\u652f\u6301\u8fed\u4ee3\u5bf9\u8c61\u3002<\/h2>\n\n\n\n<ul><li>seasons = [\u2018Spring\u2019, \u2018Summer\u2019, \u2018Fall\u2019, \u2018Winter\u2019]<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>lst = list(enumerate(seasons))\nprint(lst)\n# &#91;(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]\nlst = list(enumerate(seasons, start=1))  # \u4e0b\u6807\u4ece 1 \u5f00\u59cb\nprint(lst)\n# &#91;(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]<\/code><\/pre>\n\n\n\n<h2>break \u8bed\u53e5<\/h2>\n\n\n\n<ul><li>\u8bed\u53e5\u53ef\u4ee5\u8df3\u51fa\u5f53\u524d\u6240\u5728\u5c42\u7684\u5faa\u73af\u3002<\/li><\/ul>\n\n\n\n<h2>continue \u8bed\u53e5<\/h2>\n\n\n\n<h2>pass \u8bed\u53e5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>- \u8981\u662f\u6211\u65e9\u77e5\u9053\u6709\u8fd9\u4e48\u4e00\u4e2a\u73a9\u610f\u513f\uff0c\u5c31\u4e0d\u7528\u5728if\u8bed\u53e5\u91cc\u9762\u5199\u4e00\u4e2aprint()\n- pass \u8bed\u53e5\u7684\u610f\u601d\u662f\u201c\u4e0d\u505a\u4efb\u4f55\u4e8b\u201d\uff0c\u5982\u679c\u4f60\u5728\u9700\u8981\u6709\u8bed\u53e5\u7684\u5730\u65b9\u4e0d\u5199\u4efb\u4f55\u8bed\u53e5\uff0c\u90a3\u4e48\u89e3\u91ca\u5668\u4f1a\u63d0\u793a\u51fa\u9519\uff0c\u800c pass \u8bed\u53e5\u5c31\u662f\u7528\u6765\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u7684\u3002<\/code><\/pre>\n\n\n\n<h2>\u63a8\u5bfc\u5f0f<\/h2>\n\n\n\n<p><strong>\u8fd9\u4e2a\u4e00\u822c\u6559\u7a0b\u5c11\u89c1\uff0c\u9700\u8981\u91cd\u70b9\u770b\u4e00\u4e0b<\/strong><\/p>\n\n\n\n<h3>\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n\n\n\n<ul><li>[ expr for value in collection [if condition] ]<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>x = &#91;-4, -2, 0, 2, 4]\ny = &#91;a - 2 for a in x]\nprint(y)\n# &#91;-8, -4, 0, 4, 8]<\/code><\/pre>\n\n\n\n<h3>\u5143\u7ec4\u63a8\u5bfc\u5f0f<\/h3>\n\n\n\n<ul><li>( expr for value in collection [if condition] )<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>a = (x for x in range(10))\nprint(a)\n# &lt;generator object &lt;genexpr> at 0x0000025BE511CC48>\nprint(tuple(a))\n# (0, 2, 4, 6, 8, 10, 12, 14, 16, 18)<\/code><\/pre>\n\n\n\n<h3>\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h3>\n\n\n\n<ul><li>{ key_expr: value_expr for value in collection [if condition] }<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>b = {i: i % 2 == 0 for i in range(20) if i % 3 == 0}\nprint(b)\n{0: True, 3: False, 6: True, 9: False, 12: True, 15: False, 18: True}<\/code><\/pre>\n\n\n\n<h3>\u96c6\u5408\u63a8\u5bfc\u5f0f<\/h3>\n\n\n\n<ul><li>{ expr for value in collection [if condition] }<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>c = {i for i in &#91;1, 2, 3, 4, 5, 5, 6, 4, 3, 2, 1]}\nprint(c)\n# {1, 2, 3, 4, 5, 6}<\/code><\/pre>\n\n\n\n<h3>\u5176\u5b83<\/h3>\n\n\n\n<ul><li>next(iterator[, default])<\/li><li>Return the next item from the iterator. If default is given and the iterator is exhausted, it is returned instead of raising StopIteration.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>e = (i for i in range(10))\nprint(e)\n# &lt;generator object &lt;genexpr> at 0x0000007A0B8D01B0>\nprint(next(e))  # 0\nprint(next(e))  # 1\nfor each in e:\n     print(each, end=' ')\n# 2 3 4 5 6 7 8 9`<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f88\u5feb\u5230\u4e86\u7b2c\u4e8c\u5929 \u6761\u4ef6\u8bed\u53e5\u4e0e\u5faa\u73af\u8bed\u53e5 \u6761\u4ef6\u8bed\u53e5 if \u8bed\u53e5 if &#8211; else \u8bed\u53e5 if &#8211; elif &#8211; else \u8bed\u53e5 assert \u5173\u952e\u8bcd assert len(my_list) &gt; 0 \u5faa\u73af\u8bed\u53e5 while \u5faa\u73af for \u5faa\u73af for &#8211; else \u5faa\u73af \u5f53for\u5faa\u73af\u6b63\u5e38\u6267\u884c\u5b8c\u7684\u60c5\u51b5\u4e0b\uff0c\u6267\u884celse\u8f93\u51fa\uff0c\u5982\u679cfor\u5faa\u73af\u4e2d\u6267\u884c\u4e86\u8df3\u51fa\u5faa\u73af\u7684\u8bed\u53e5\uff0c\u6bd4\u5982 break\uff0c\u5c06\u4e0d\u6267\u884celse\u4ee3\u7801\u5757\u7684\u5185\u5bb9\uff0c\u4e0ewhile &#8211; else\u8bed\u53e5\u4e00\u6837\u3002 range() \u51fd\u6570 enumerate()\u51fd\u6570 sequence\uff1a\u4e00\u4e2a\u5e8f\u5217\u3001\u8fed\u4ee3\u5668\u6216\u5176\u4ed6\u652f\u6301\u8fed\u4ee3\u5bf9\u8c61\u3002 seasons = [\u2018Spring\u2019, \u2018Summer\u2019, \u2018Fall\u2019, \u2018Winter\u2019] break \u8bed\u53e5 \u8bed\u53e5\u53ef\u4ee5\u8df3\u51fa\u5f53\u524d\u6240\u5728\u5c42\u7684\u5faa\u73af\u3002 continue \u8bed\u53e5 pass \u8bed\u53e5 \u63a8\u5bfc\u5f0f \u8fd9\u4e2a\u4e00\u822c\u6559\u7a0b\u5c11\u89c1\uff0c\u9700\u8981\u91cd\u70b9\u770b\u4e00\u4e0b \u5217\u8868\u63a8\u5bfc\u5f0f [ &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.mimiq.com.cn\/?p=549\"> <span class=\"screen-reader-text\">DeepOps\u7684Python\u5c0f\u7b14\u8bb0-\u5929\u6c60\u9f99\u73e0\u8ba1\u5212-Python\u8bad\u7ec3\u8425-Task 01:DAY2<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[25],"tags":[],"_links":{"self":[{"href":"http:\/\/www.mimiq.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/549"}],"collection":[{"href":"http:\/\/www.mimiq.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mimiq.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mimiq.com.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mimiq.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=549"}],"version-history":[{"count":1,"href":"http:\/\/www.mimiq.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/549\/revisions"}],"predecessor-version":[{"id":550,"href":"http:\/\/www.mimiq.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/549\/revisions\/550"}],"wp:attachment":[{"href":"http:\/\/www.mimiq.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mimiq.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=549"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mimiq.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}