THE WALRUS OPERATOR IN PYTHON 3.8

THE WALRUS OPERATOR IN PYTHON 3.8

Date: 29/01/2020

Introduction

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

Conclusion

Thank You for using pheonixsolutions.
If you like the blog share it with your friends and others also.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Comment

Scroll to Top