THE WALRUS OPERATOR IN PYTHON 3.8
Walrus Operator is another name for Assignment Expressions. I will explain in detail in this article.
Assignment Expressions are written with a new notation which is colon followed by equal to expression ( := ) . It allows you to assign a value to a variable and check conditions for that variable in the same expression.
For Example, Lets find out the given number is odd or even
if((number:=27)%2==0): print("Given number is even") else: print("Given number is odd")
Here is the Output :
Advantages of using the assignment expressions is we can directly assign a value to the variable and checks for the condition is satisfied or not at the same time.
Whereas in previous python versions , we have to assign a value for a variable separately and checks the condition by using that variable.
For Example , In python 3.7
number=27 if(number%2==0): print("Given number is even") else: print("Given number is odd")
Walrus Operator reduces the code lines and making things easier
Thank You for using pheonixsolutions.
If you like the blog share it with your friends and others also.