Skip to content

Commit 2b747ed

Browse files
:=
1 parent 09589a3 commit 2b747ed

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

python48 (walrus operator).py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# walrus operator :=
2+
3+
# there is a new syntax := that assigns values to variables as part of a larger expression.
4+
# It is known as the walrus operator.
5+
# This is the most controvercial feature of python3.8
6+
7+
# this is desisgned to use inside expressions.
8+
# it actually assigns a variable to some part of the expression.
9+
10+
# Example 1
11+
a = [0,1,2,3,4,5,6]
12+
13+
if (n := len(a)) > 5:
14+
print(f"List is too long ({n} elements, expected <= 5)")
15+
16+
# what it does is that, it sets a variable n as the len(a) so that we can use it later on.
17+
# it helps avoid calling len() function twice.
18+
# But it doesn't effects the expression.
19+
# we must use brakets to distinguish the part of expression that we want to set as a var with walrus.
20+
21+
22+
# Example 2
23+
while (ans := input("Enter a number: ")) != "":
24+
print(ans)
25+
26+
# this operator is also helpful with while-loops that compute a value to test loop termination
27+
# and then that same value again in the body of the loop.
28+
29+
30+
# Example 3
31+
names = ["1. ahammad", "2. cristiano", "3. ramos", "4. zidan"]
32+
allowed_names = ["ahammad", "cristiano", "marcelo"]
33+
34+
walrus_list = [clean_name.title() for name in names if (clean_name := name[3:]) in allowed_names]
35+
print(walrus_list)
36+
37+
# Another motivated use case of walrus operator in list comprehensions
38+
# where a value computed in a filtering condition isalso needed in the expression body.
39+
40+
41+
# Example 4
42+
# we can do all the stuffs without walrus which we can do with walrus.
43+
# the only advantage walrus gives us is to remove some extra line of codes.
44+
45+
# Without Walrus
46+
x = 5
47+
without_walrus = x < 7
48+
print(without_walrus)
49+
50+
# With Walrus
51+
x = 5
52+
print(walrus := x < 7)
53+
# we can use the var later on
54+
print(not walrus)
55+
56+
# Example 5
57+
# this is the best advantage of walrus operators.
58+
# It is simmilar to example 2.
59+
60+
# Without Walrus
61+
nums = []
62+
num = input("Type a number: ")
63+
while num.isdigit():
64+
nums.append(int(num))
65+
num = input("Type a number: ")
66+
print(nums)
67+
68+
# With Walrus
69+
nums = []
70+
while (num := input("Type a number: ")).isdigit():
71+
nums.append(num)
72+
print(nums)
73+
74+
75+
76+
# Example 6
77+
# if helps us avoiding levels of indentation.
78+
79+
# Without Walrus
80+
var = 5
81+
if var == 5:
82+
ans = input("Enter your name: ")
83+
if ans != "":
84+
print("Nice name!")
85+
86+
87+
# With walrus
88+
var = 5
89+
if var == 5 and (ans := input("Enter your name: ")) != "":
90+
print("Nice name")
91+
92+
93+
# Now we know what is walrus and where should we use it.
94+
# It makes the code kind of hard to understand.
95+
# so, if we really want to implement it within our code and make our code DRY, then we can do thaat.
96+
# Otherwise, we can do the exact same things without the walrus operator :=

0 commit comments

Comments
 (0)