You Won’t See This Python Operator Very Often in Your Life!!! | by Jagajith | CodeX | Oct, 2021

I will just add colon “:” before the assignment operator.And boom I get 10 printed on my screen. This basically returns and assigns at the same time.

The walrus operator is called so because it kind of looks like the eyes and tusks of a walrus.

Walrus — Source

I’d like to give you some general advice about the mindset you should have when new features are introduced into a language, framework, or whatever. Avoid this type of black-and-white thinking where you essentially say I’m always going to use that or I’m never going to use that. Think about the feature, look at the advantages, and disadvantages.

You must now be aware of the fact that this feature was introduced in Python 3.8, so if you use it for some reason, it will not be compatible with Python 3.7 and below, and of course, a lot of programmers, especially beginners or intermediates who aren’t really into every little feature of the python language, may not understand your code or what exactly you’re doing, so for some people, readability will be a problem when you use the walrus operator because it’s not the most popular and the most widely used operators so this is something to keep in mind.

It doesn’t add anything that you couldn’t accomplish without it in some manner, so it’s not anything you have to do. It’s just a new method to do things, and it can help you reduce your code.

Let’s look at the first example where the walrus operator can be quite useful. Structure you’ll often encounter in command line based applications where we have some dynamic user input and we need to process it, such as in number guessing games, menus, or hangman games, or basically in applications where we have something that is executed over and over again where we have user input that we’re constantly associating with.

Lets look at the following program without walrus operator: