Python does not have the so-called switch syntax. This makes it easier to understand because each evaluation formula is close to each other, but it inevitably becomes redundant.
for a in range(3):
if a == 0:
print "Hello!!"
elif a == 1:
print "How are you?"
elif a == 2:
print "Good Bye :)"
Although it is readable, I think that writing "a =" a ==" would be a hassle. So, when I thought that I wish I could write it a little easier, I thought that there was lambda, so I will use it for a while.
for a in range(3):
case = lambda check_num: a == check_num
if case(0):
print "Hello!!"
elif case(1):
print "How are you?"
elif case(2):
print "Good Bye :)"
Then, it will output the same output as the first one. However, the disadvantage in this example is
However, when comparing the same variables, rather than writing an expression one by one, wrapping it in lambda and using that function made if quite neat and convenient, so make a note here. I will do it.
Recommended Posts