News, Programming, Python, Tutorials

Walrus Operator in Python

In the python 3.8 release, a new assignment operator was introduced which became wildly famous because of its being controversial and Guido van Rossum (creator of python) stepping down as BDFL (Benevolent dictator for life) “key visionary” “final decider” of the python language updates.

The Walrus operator :=

The name “walrus” because it resembles the eyes and tusk of a walrus.

Walrus operator :=

The update introduces a new syntax := to assign value to a variable as a part of a larger expression.

Let’s understand what does a “Walrus operator” do with an example.

Make sure your python version is updated to version 3.8 or above. [jump to code implementation]

Traditional way before python version 3.8

In the above code, a list is created first and then it is passed as an argument to the print function. If a function with

Walrus way after python version 3.8

By using the walrus operator you can avoid initializing a variable before passing it as an argument but instead, we can initialize it using the walrus operator right in the argument.

Additionally, the variable initialized by the walrus operator can be reused further in the program.

Walrus operator in while loops

To demonstrate how walrus operator can be used in while loop we will write a program which stores all the input by the user until the user enters quit .

Traditional way

Walrus way

By using the walrus operator we have reduced the usage of writing the input function twice.


Showcase your project and insights with our audience.

Email us at electroic321 [at] gmail [dot] com


Thank you for reading, Happy Learning, drop your suggestion in the comments.

Feel free to follow us on Youtube , Linked In , Instagram

0 0 vote
Article Rating