Codementor Events

Introduction to Python 3.8 new feature — “The Walrus Operator”

Published May 27, 2019Last updated Nov 22, 2019


Image credits: geekboots.com

You can see my post here on medium.

Python 3.8 is in the development phase (currently in alpha phase) and it’s expected to be released in September 2019. You can read the full documentation here. As expected, it has included a lot of features — assignment expressions, position only parameters, a lot of additions to builtin modules (improved modules), etc. You can download the alpha version for fedora 29:

sudo dnf install python38

The download files for python 3.8 are available here. This article (a small introductory tutorial) talks about an important addition to the new python 3.8 version —  the assignment expressions.

The Walrus Operator (:=)

According to the python 3.8 documentation, A new feature called assignment expression has been added to the new version of python. A new syntactical operator— “The Walrus Operator” is the part of it. “The walrus operator” is a new method, using which you can assign values to variable as a part of an expression. This can be illustrated using the following example.

Consider the following code example:

a = 11

if (b := a) > 10:
    print(f"The value of b is {b} and is greater than 10.")

Explanation:

The value of variable a is assigned to 11. The assignment expression b := a assigns variable b to the value of a which is 11 (in the above case), and checks if the value is greater than 10 (it acts like a normal if loop condition).

prints:

The value of b is 11 and is greater than 10.

It is as simple as that! You can give it a try by downloading the alpha version today. If you want to read the whole thing, you can read it here. It is a very simple feature, and to me it also improves readability and avoids some extra work. The similar idea is already implemented in the languages such as Java. I think this feature is a great new addition to python!

. . . .

Buy me a ☕️ on paypal, if you like this small introductory post 😃.

Discover and read more posts from Ruturaj Kiran Vaidya
get started
post commentsBe the first to share your opinion
robin deatherage
5 years ago

This " := " is called a “collator”. collate means=1. to gather or 2.to verify the arrangement of 3.arrange in their proper sequence. Python should not change this to Walrus. Leave it what it is.

mo ali
5 years ago

UESLESS BUT THNX

ZJ
5 years ago

Long live Pascal

Show more replies