{"id":33529,"date":"2020-03-17T13:57:47","date_gmt":"2020-03-17T08:27:47","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=33529"},"modified":"2020-03-17T13:57:47","modified_gmt":"2020-03-17T08:27:47","slug":"walrus-operator-in-python-3-8","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/walrus-operator-in-python-3-8\/","title":{"rendered":"Walrus Operator in Python 3.8"},"content":{"rendered":"<p>Python 3.8 remains in development. But many alpha versions are released. one among the newest features in Python 3.8 is that the Walrus Operator. during this article, we\u2019re getting to discuss the Walrus operator and explain it with an example.<\/p>\n<p><strong>Introduction<\/strong><\/p>\n<p>Walrus-operator is another name for assignment expressions. consistent with the official documentation, it&#8217;s how to assign to variables within an expression using the notation NAME := expr. The Assignment expressions allow a worth to be assigned to a variable, even a variable that doesn\u2019t exist yet, within the context of expression instead of as a stand-alone statement.<\/p>\n<h3 id=\"code\"><strong>Code :<\/strong><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">a = [1, 2, 3, 4] <br\/>if (n := len(a)) &gt; 3: <br\/>    print(f&quot;List is too long ({n} elements, expected &lt;= 3)&quot;) <\/code><\/pre> <\/div>\n<h3 id=\"output\"><strong>OUTPUT:<\/strong><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">List is too long (4 elements, expected &lt;=3)<\/code><\/pre> <\/div>\n<p><strong>Example \u2013<\/strong><\/p>\n<p>Let\u2019s plan to understand Assignment Expressions more clearly with the help of an example using both Python 3.7 and Python 3.8. Here we&#8217;ve a listing of dictionaries is named \u201csample_data\u201d, which contains the user Id, name and a Boolean called is completed.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">sample_data = [ <br\/>    {&quot;userId&quot;: 1,  &quot;name&quot;: &quot;rahul&quot;, &quot;completed&quot;: False}, <br\/>    {&quot;userId&quot;: 1, &quot;name&quot;: &quot;rohit&quot;, &quot;completed&quot;: False}, <br\/>    {&quot;userId&quot;: 1,  &quot;name&quot;: &quot;ram&quot;, &quot;completed&quot;: False}, <br\/>    {&quot;userId&quot;: 1,  &quot;name&quot;: &quot;ravan&quot;, &quot;completed&quot;: True} <br\/>] <br\/>  <br\/>print(&quot;With Python 3.8 Walrus Operator:&quot;)  <br\/>for entry in sample_data:  <br\/>    if name := entry.get(&quot;name&quot;): <br\/>        print(f&#039;Found name: &quot;{name}&quot;&#039;) <br\/>  <br\/>print(&quot;Without Walrus operator:&quot;) <br\/>for entry in sample_data: <br\/>    name = entry.get(&quot;name&quot;) <br\/>    if name: <br\/>        print(f&#039;Found name: &quot;{name}&quot;&#039;) <\/code><\/pre> <\/div>\n<h3 id=\"output-2\"><strong>OUTPUT:<\/strong><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">With python 3.8 walrus operators :<br\/>Found name: \u201crahul\u201d<br\/>Found name: \u201crohit\u201d<br\/>Found name: \u201cram\u201d<br\/>Found name: \u201cravan\u201d<br\/><br\/>Without Walrus operators :<br\/>Found name: \u201crahul\u201d<br\/>Found name: \u201crohit\u201d<br\/>Found name: \u201cram\u201d<br\/>Found name: \u201cravan\u201d<br\/>\u2003<\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python 3.8 remains in development. But many alpha versions are released. one among the newest features in Python 3.8 is that the Walrus Operator. during this article, we\u2019re getting to discuss the Walrus operator and explain it with an example. Introduction Walrus-operator is another name for assignment expressions. consistent with the official documentation, it&#8217;s how [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":33606,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[86908,86909,86907],"class_list":["post-33529","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding","tag-operators-expression-in-python","tag-operators-in-python-walrus","tag-walrus-operators"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/33529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=33529"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/33529\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/33606"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=33529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=33529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=33529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}