#Day10 — Walrus Operator in Python

Photo by NOAA on Unsplash

In today’s article, we will talk about the Walrus Operator := in Python. The walrus operator is an assignment operator which was introduced in Python 3.8. But it is not just an assignment operator, it also returns the value after the assignment.

This is the general syntax of the walrus operator

(var := expression/ function)

Although the parenthesis is not necessary, it is a good practice

So what does the Walrus operator do?

  • The value of the expression is assigned to the variable var
  • After assignment, the value is also returned.

Before Python 3.8, if you wanted to assign the value returned by an expression/function to a variable and print the value, below is how you would do it

var = expression/function print(var)

But using the walrus operator, this can be done in a single line

print(var := expression/function)

Let’s compare some code snippets with and without the walrus operator

Print statements

Python 3.7

def func(x):
return x+1
var = 0
var = func(var)
print(var)

Python 3.8

def func(x):
return x+1
var = 0
print( var := func(var) )

The value returned by the function func is first assigned to the variable var. After the assignment, the value is returned and the print statement displays it on the command line.

If statements

Python 3.7

lst = [1,2,3]
n = len(lst)
if n < 10:
print("It is a small list")
print(n)

Python 3.8

lst = [1,2,3]
if (n:= len(lst)) < 10:
print("It is a small list")
print(n)

while loop

Python 3.7

n = 0
while(n < 10):
print(n)
n +=1

Python 3.8

n = 0
while(n := n +1) < 10:
print(n)

Lists

Python 3.7

def func(x):
return x + 1
y = func(2)
lst = [y , y*y , y*y*y]
print(lst)

Python 3.8

def func(x):
return x + 1
lst = [y:=func(2),y*y,y*y*y]
print(lst)

List Comprehension

Python 3.7

'''
Imagine this is some
computatationally expensive function
'''
def computationally_expensive_function(x):
return 2
nums = [1,2,3]lst = [computationally_expensive_function(num) for num in nums if computationally_expensive_function(num) > 0]
print(lst)

Python 3.8

'''
Imagine this is some
computatationally expensive function
'''
def computationally_expensive_function(x):
return 2
nums = [1,2,3]lst = [var for num in nums if (var := computationally_expensive_function(num))> 0]
print(lst)

Instead of calling the computation expensive function twice, we only call it once when using the walrus operator.

Originally published at https://www.realpythonproject.com on March 31, 2021.

Programming Tips

Everything from SQL to Python

Rahul Banerjee 👨‍💻

Written by

Articles related to Python, Data Science, APIs |SDE Intern @Amazon | Comp Eng Student @uoft

Programming Tips

Content related to programming: tips, code-snippets, concepts. Follow along https://linktr.ee/rahul2699

Rahul Banerjee 👨‍💻

Written by

Articles related to Python, Data Science, APIs |SDE Intern @Amazon | Comp Eng Student @uoft

Programming Tips

Content related to programming: tips, code-snippets, concepts. Follow along https://linktr.ee/rahul2699

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store