{"id":3204,"date":"2022-11-12T20:16:43","date_gmt":"2022-11-12T20:16:43","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3204"},"modified":"2022-11-12T20:16:45","modified_gmt":"2022-11-12T20:16:45","slug":"python-walrus-operator","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-walrus-operator\/","title":{"rendered":"ASSIGNMENT EXPRESSION (WALRUS OPERATOR) IN PYTHON"},"content":{"rendered":"<p style=\"text-align: center;\">ASSIGNMENT EXPRESSION &#8211; Python Walrus Operator<\/p>\n<p>Now, we are going to have a look at a very interesting feature in python, which is assignment expression and is also called a walrus operator (maybe you will soon understand why is it called a walrus operator).<\/p>\n<p>So, what is an assignment expression, or walrus operator? Let\u2019s have a look at this first.<\/p>\n<p>Basically, the walrus operator allows us to assign the value to some variable, as a part of a larger expression. Simply put, it is a way to assign values to variables within an expression. When we will have some examples, things would get clearer.<\/p>\n<p>But even before we get into the examples, Let\u2019s have a look at what the walrus operator looks like. It basically is a colon, followed by an equals sign (:=).<\/p>\n<h2 style=\"text-align: center;\">Python Walrus Operator<\/h2>\n<p>Now, Let\u2019s have a look at a program, which would not use the walrus operator here. In the below program, we are not doing much. We are just checking whether or not the given number is even or odd. So, Let\u2019s have a look at this now &#8211;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3205 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ASSIGNMENT-EXPRESSION-WALRUS-OPERATOR-IN-PYTHON.jpg\" alt=\"\" width=\"895\" height=\"172\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ASSIGNMENT-EXPRESSION-WALRUS-OPERATOR-IN-PYTHON.jpg 895w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ASSIGNMENT-EXPRESSION-WALRUS-OPERATOR-IN-PYTHON-300x58.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ASSIGNMENT-EXPRESSION-WALRUS-OPERATOR-IN-PYTHON-768x148.jpg 768w\" sizes=\"auto, (max-width: 895px) 100vw, 895px\" \/><\/p>\n<p>In the above program, as you can see, we have a number, and we are checking whether the number is even or odd. In our case, the number is even. But here, if you observe, we are assigning the value to the variable first, and then we are checking the condition, and then we are giving the output accordingly. What if we could make it a little bit shorter? What if we could assign the value to the variable within the expression where we are checking if the number is divisible by 2? Well, I mean something like this &#8211;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3206 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ASSIGNMENT-EXPRESSION-WALRUS-OPERATOR-IN-PYTHON-1.jpg\" alt=\"\" width=\"1069\" height=\"175\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ASSIGNMENT-EXPRESSION-WALRUS-OPERATOR-IN-PYTHON-1.jpg 1069w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ASSIGNMENT-EXPRESSION-WALRUS-OPERATOR-IN-PYTHON-1-300x49.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ASSIGNMENT-EXPRESSION-WALRUS-OPERATOR-IN-PYTHON-1-1024x168.jpg 1024w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ASSIGNMENT-EXPRESSION-WALRUS-OPERATOR-IN-PYTHON-1-768x126.jpg 768w\" sizes=\"auto, (max-width: 1069px) 100vw, 1069px\" \/><br \/>\nHere, as a part of the expression, we are also assigning the value to the variable. But here, we are getting an error, because we have not used the walrus operator. Just in the place of that equal to sign, we have to use the walrus operator, and we are done. Let\u2019s have a look &#8211;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3207 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ASSIGNMENT-EXPRESSION-WALRUS-OPERATOR-IN-PYTHON-2.jpg\" alt=\"\" width=\"928\" height=\"183\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ASSIGNMENT-EXPRESSION-WALRUS-OPERATOR-IN-PYTHON-2.jpg 928w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ASSIGNMENT-EXPRESSION-WALRUS-OPERATOR-IN-PYTHON-2-300x59.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ASSIGNMENT-EXPRESSION-WALRUS-OPERATOR-IN-PYTHON-2-768x151.jpg 768w\" sizes=\"auto, (max-width: 928px) 100vw, 928px\" \/><\/p>\n<p>Now, as you can see, we got the walrus operator used here. What is happening here, takes us back to the definition of the assignment expression, or the walrus operator, that \u2013 it allows us to assign value to the variable, within an expression. So, again if you try to run the above program, you can simply see the correct output here.<\/p>\n<p>So, we can make use of the walrus operator as and when required. It is a very useful feature at times. Remember that the walrus operator allows us to assign value to some variable, as a part of larger expression.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ASSIGNMENT EXPRESSION &#8211; Python Walrus Operator Now, we are going to have a look at a very interesting feature in python, which is assignment expression and is also called a walrus operator (maybe you will soon understand why is it called a walrus operator). So, what is an assignment expression, or walrus operator? Let\u2019s have [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3204","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3204"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3204\/revisions"}],"predecessor-version":[{"id":4055,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3204\/revisions\/4055"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}