{"id":6830,"date":"2020-05-28T23:52:38","date_gmt":"2020-05-29T06:52:38","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6830"},"modified":"2020-05-28T23:53:54","modified_gmt":"2020-05-29T06:53:54","slug":"walrus-operator-python-weekly-ep2","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/python\/walrus-operator-python-weekly-ep2\/","title":{"rendered":":= \u6d77\u8c61\u8fd0\u7b97\u7b26 Walrus Operator &#8211; Python Weekly EP2"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\":= \u6d77\u8c61\u8fd0\u7b97\u7b26 Walrus Operator - Python Weekly EP2\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/ojhTG9Ctt0I?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n\/\/ Author: Huahua\nimport re\nimport io\n\n# Example 1:\na = [0] * 100\nif len(a) > 10:\n  print(f\"List is too long ({len(a)} elements, expected <= 10)\")\n\nn = len(a)\nif n > 10:\n  print(f\"List is too long ({n} elements, expected <= 10)\")\n\nif (x := len(a)) > 10:\n  print(f\"List is too long ({x} elements, expected <= 10)\")\n\n# Example 2, bad\nads = \"Now 20% off till 6\/18\"\n\nm1 = re.search(r'(\\d+)% off', ads)\ndiscount1 = float(m1.group(1)) \/ 100 if m1 else 0.0\n\ndiscount2 = float(m2.group(1)) \/ 100 if (m2 := re.search(r'(\\d+)% off', ads)) else 0.0\n\nprint(f'{discount1 = }, {discount2 = }')\n\n# Example 3\nf1 = io.StringIO(\"123456789\")\nx1 = f1.read(4)\nwhile x1 != '':\n  print(x1)\n  x1 = f1.read(4)\n\nf2 = io.StringIO(\"123456789\")\nwhile (x2 := f2.read(4)) != '':\n  print(x2)\n\n# Example 4\nprog_langs = {'c++', 'python', 'java'}\nlangs = ['C++', 'Java', 'PYthon', 'English', '\u4e2d\u6587']\n\nl1 = [lang.lower() for lang in langs if lang.lower() in prog_langs]\nprint(l1)\n\nl2 = [l for lang in langs if (l := lang.lower()) in prog_langs]\nprint(l2)\n\n\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ Author: Huahua import re import io # Example 1: a = [0] * 100 if len(a) > 10: print(f&#8221;List is too long ({len(a)} elements,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[605],"tags":[517,611,610],"class_list":["post-6830","post","type-post","status-publish","format-standard","hentry","category-python","tag-python","tag-python3-8","tag-walrus","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6830","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6830"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6830\/revisions"}],"predecessor-version":[{"id":6832,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6830\/revisions\/6832"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}