Assignment Expressions:- The Walrus Operator in python 3.8

Posted on by Sumit Kumar

Dear Reader,

In this blog we will learn about Walrus operator in python.
This is the newly added feature in python 3.8.
So please install python 3.8 or higher version otherwise walrus operator will not be available for you.
We understand that no body like change but change is something that eventualy we have to adopt and as the time passes we started loving it.
When for loop, while loop feature has introduced at the begining it was though for the developer to use it but now a days this become a common practice.
Like wise Warlus Operator is also introduce and currently many developer are not using it but in future it may use by developer very widely.

What is the Walrus Operator:-

Walrus operator is the Fancy name given to Assignment Expressions(:=) as syntax (:=) resemblance to the eyes and tusks of walrus.

 

Assignment Expressions or Walrus operator is not only assignment expressions but it is a new way of thinking that any value can be assign to variable and can be use that variable  at the same time.

Lets try to understand this with an example.
Surly I will not directly write Walrus operator it will be be confusing at the begining.
I will write example first without Walrus operator and later I will convert the same example with walrus operator.

Examples:-

1)

with the above example we are just saving one line of code.

2) Lets use walrus operator with while loop:-

In this example we will take input from user and keep it into list continusly untill user press ‘q’.

Now we will do same exercise with the help of walrus operator.

Hope these examples help you to understand the walrus Operator.
Thank You.

Reference:-

https://docs.python.org/3/whatsnew/3.8.html

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*