The walrus operator in Python

Translation prepared as part of the course Python Developer. Basic“.

We also invite everyone to a two-day online intensive “Developing a Desktop Application Using the Tkinter Library”… On the intensive course, we will get the initial skills of backend development in Python, as well as start developing a desktop application using the Tkinter library.


The walrus operator, introduced in Python 3.8, allows you to solve two problems at once: assign a value to a variable and return that value, so sometimes you can write shorter code and make it more readable, and it can be even more computationally efficient.

Let’s take a look at the walrus operator and give examples of where it can be useful.

Simple assignment operator

We all know how to assign a value to a variable. This is done using a simple assignment operator:

num = 15

And if we want to display the value of this variable using the function printthen pass the variable num we can as follows:

print(num)
# 15

Walrus operator

The walrus operator (: =), added in Python 3.8, is formally known as the expression assignment operator. It makes it possible to assign variables in an expression, including variables that do not yet exist. As mentioned above, using the simple assignment operator (=), we assigned num equal to 15 in the context of a single statement.

The expression is evaluated as a value. And the instruction performs some kind of action.

In other words, the walrus operator allows us to both assign a value to a variable and return that value in the same expression. And it is called so because the symbol (: =) looks like the eyes and tusks of a walrus lying on its side.

Expr is calculated and then assigned to the variable name. This value will be returned. Let’s take a look at some examples of using this operator.

A simple example

The best way to understand how the walrus operator works is with this simple example. As above, we want to assign 15 to a variable numand then output the value num… We can accomplish both of these tasks in one line of code using a new statement like this:

print(num := 15)
# 15

The value 15 is assigned num… Then the same value is returned, which becomes the argument to the function print So 15 is displayed.

If we try to do the same with the usual assignment operator, we get a type error, because num = 15 returns nothing.

print(num = 15)
# TypeError

Another example

Let’s say we want to continue requesting some data from the user. If the user doesn’t enter anything, we can stop asking for additional input. Do it with a loop while as follows:

We ask the user to enter something and assign a value to the input. Then we create a loop whilewhich is executed as long as the entered value is not an empty string. We output “Nice!” If the user has successfully entered something. Why do we ask the user for the next input, assign a value to it and restart the process.

Let’s try to do this with the walrus operator:

We ask the user for input and assign it using the walrus operator. This value is later returned and compared with an empty string. If the comparison results in True (that is, unequally empty string), the code in the loop while runs and displays “Nice!” If comes Falsethen no further code is executed.

List expression example

Another example of how you can improve code readability and computational efficiency is a list expression that filters values.

Let’s say we have a list of numbers num_list, and we want to add a number cube to the list only if this value is less than 20. You can do this as follows:

Please note that we need to call the function cube twice.

The walrus operator will allow us to call the function cube just once in our list expression, as shown below:

Value cube(x) assigned y, then returns and compares to 20. Value y will be added to the list only if it is less than 20. Note that the function cube() called only once, which improves the efficiency of the code. And the increase in efficiency is the more significant, the more complex the function is to calculate.

I hope this guide to using the walrus operator in Python was helpful. Thanks for reading!


Learn more about the course Python Developer. Basic“.

Registration for the two-day online intensive “Developing a Desktop Application Using the Tkinter Library”: day 1, day 2.

Similar Posts

Leave a Reply