Search Here

Python Walrus Operator - Assignment Expression Operator with Examples

Python Walrus Operator | Assignment Expression Operator with Examples

Table of Contents

What is the Walrus operator in Python?

Python has released its new version 3.8 with features such as Assignment Expressions, Positional-only parameters, new f-string support, and many more.
The new update that baffled many of python developers is an introduction to Walrus Operator also known as Assignment Expression operator.

Using this operator we can assign value to other variables inside expression and during conditional checking. Let us look at its syntax and then go further in detail.

Syntax

new_variable := old_variable

The operator is represented using symbol colon followed by equal to sign :=. The value stored in old_variable is assigned to new_variable.

Examples

It would be more convenient if we learn through the examples provided below.

num = 15

(age:=num)

print("Ryan's age is {}".format(age))

#PYTHON OUTPUT
Ryan's age is 15

The variable num value is assigned to a variable age using :=.

Assign variable value during conditional check using Walrus Operator

nums = [60, 50]
if (largest_num := nums[0]) > nums[1]:
    print("The largest number is : {}".format(largest_num))

#PYTHON OUTPUT
The largest number is : 60

The value in the array nums[0] is assigned to a variable largest_num irrespective of whether it is greater or lesser than other values.

Let us recreate the above example without using a walrus operator.

nums = [60, 50]
if nums[0] > nums[1]:
    largest_num = nums[0]
    print("The largest number is : {}".format(largest_num))

#PYTHON OUTPUT
The largest number is : 60

And both the output are the same. Let us look at some more examples related to the walrus operator.

Bubble Sort without using Walrus Operator

nums = [32, 60, 50, 35, 12, 15, 1]

n = len(nums)

for index in range(n):

    for j in range(0,n-1):

        if nums[index] < nums[j]:

            temp = nums[index]

            nums[index] = nums[j]

            nums[j] = temp

print(nums)

#PYTHON OUTPUT
[1, 12, 15, 32, 35, 50, 60]

Bubble Sort with using Walrus Operator

nums = [32, 60, 50, 35, 12, 15, 1]

n = len(nums)

for index in range(n):

    for j in range(0,n-1):
        
        if (temp := nums[index]) < nums[j]:

            nums[index] = nums[j]

            nums[j] = temp

print(nums)

#PYTHON OUTPUT
[1, 12, 15, 32, 35, 50, 60]

Both the above two bubble sort examples return the same output but the difference is that the first one does not use a walrus operator and the second one uses it.

Example: Display students who have secured more than 85% using Walrus Operator

students = [
    {"name" : "Ravi", "marks" : 569},
    {"name" : "Sumit", "marks" : 464},
    {"name" : "Anil", "marks" : 357},
    {"name" : "Yuvraj", "marks" : 402},
    {"name" : "Kiran", "marks" : 602},
    {"name" : "Vikram", "marks" : 615},
]

for student in students:

    if (percentage := ((student["marks"]/700)*100)) > 85:

        print("Student {} has scored {:0.02f} % ".format(student["name"], percentage)) 

#PYTHON OUTPUT
Student Kiran has scored 86.00 %
Student Vikram has scored 87.86 %

{:0.02f} are used to display only precise decimal point numbers.

Example: Walrus Operator with Looping over a list

nums = [32, 60, 50, 35, 12, 15, 1]

print([ temp for x in nums if (temp:=x) > 25 ])

#PYTHON OUTPUT
[32, 60, 50, 35]

The output displays only those numbers which are greater than 25.

Conclusion

We have come to the final part of our post, on Python Walrus Operator | Assignment Expressions with Examples. If you like this share this post and provide your suggestions in comments.

Recent Posts

Summary
Review Date
Reviewed Item
Python Walrus Operator | Assignment Expression Operator with Examples
Author Rating
51star1star1star1star1star
Software Name
Python Programming Language
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Programming Language