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