Day #86 - Walrus Operator in Python

Day #86 - Walrus Operator in Python

Introduction

Welcome to my 86th blog post on the Python journey. On day 86, I learned about a special operator in Python. This is called 'Walrus' operator. It helps you to shorten the code while maintaining the code readability and program efficiency. Let's dive into more details and understand 'Walrus' Operators in Python.

So let's get started......

'Walrus' Operator

  • It is a new addition to Python 3.8

  • It allows us to assign a value to a variable within an expression.

  • Walrus Operator can be used in situation when we need to use a value multiple times in a loop, but we don't want to repeat the calculation.

  • It can be used in while loops and if statements.

  • It is represented by the :=  syntax

Using 'Walrus' Operator in 'while' loop

Example -

In the below example, the length of the numbers list is assigned to the variable 'n' using the Walrus Operator. Variable 'n' is then used in the while condition, to execute the while loop.

numbers = [1, 2, 3, 4, 5]

while (n := len(numbers)) > 0:
    print(numbers.pop())

Using 'Walrus' Operator in 'if' statement

Example -

In the below example, the user input is assigned to the variable 'name' using the Walrus Operator.

names = ["John", "Jane", "Jim"]

if (name := input("Enter a name: ")) in names:
    print(f"Hello, {name}!")
else:
    print("Name not found.")

Examples

Example 1 -

# walrus operator :=

# new to Python 3.8
# assignment expression aka walrus operator
# assigns values to variables as part of a larger expression

#Without Walrus Operator
# happy = True
# print(happy)

#With Walrus Operator
 print(happy := True)

Example 2 -

#Without Walrus Operator
# foods = list()
# while True:
#   food = input("What food do you like?: ")
#       if food == "quit":
#           break
#   foods.append(food)

#With Walrus Operator
foods = list()
while (food := input("What food do you like?: ")) != "quit":
    foods.append(food)

Note - It should be kept in mind that usage of 'Walrus' Operator should be made sparingly as the code will become less readable if overused.

Resources Used

You can watch the video of Day#86 by clicking on the below link 👇👇👇👇👇

Conclusion

Thanks, guys for going through this blog post. On day 86, I learned about 'Walrus' operator that can be used with 'while' loops and 'if' statements to assign value to a variable within an expression.

Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #86

See you in the next one.....


About Me

Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.

I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.

So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.