--Make a note of what you thought was python.
――I like ternary operators (conditional operators) ~ (I was shocked when new employees didn't understand ...)
>>> a = True
>>> b = a ? "OK" : "NG"
File "<stdin>", line 1
b = a ? "OK" : "NG"
^
SyntaxError: invalid syntax
--An error occurred ...
>>> b = "OK" if a else "NG"
>>> print(b)
OK
――I think this is more natural in English. (I'm not used to it yet) --Although there is a way to write ``` $ b =" OK "if (a);` `` as a postfix if in perl.
-(I'm told that I don't understand because I write it like this ...) ――It is also possible to connect conditions.
sh-3.2$ perl -le '$a=0;$b=($a==0)?"ZERO":($a==1)?"ONE":($a==2)?"TWO":"TAKUSAN!";print $b'
ZERO
sh-3.2$ perl -le '$a=1;$b=($a==0)?"ZERO":($a==1)?"ONE":($a==2)?"TWO":"TAKUSAN!";print $b'
ONE
sh-3.2$ perl -le '$a=2;$b=($a==0)?"ZERO":($a==1)?"ONE":($a==2)?"TWO":"TAKUSAN!";print $b'
TWO
sh-3.2$ perl -le '$a=3;$b=($a==0)?"ZERO":($a==1)?"ONE":($a==2)?"TWO":"TAKUSAN!";print $b'
TAKUSAN!
――What happens if you write this like python?
>>> a=0
>>> b = "ZERO" if a == 0 else "ONE" if a == 1 else "TWO" if a == 2 else "TAKUSAN!"
>>> b
'ZERO'
>>> a=1
>>> b = "ZERO" if a == 0 else "ONE" if a == 1 else "TWO" if a == 2 else "TAKUSAN!"
>>> b
'ONE'
>>> a=2
>>> b = "ZERO" if a == 0 else "ONE" if a == 1 else "TWO" if a == 2 else "TAKUSAN!"
>>> b
'TWO'
>>> a=3
>>> b = "ZERO" if a == 0 else "ONE" if a == 1 else "TWO" if a == 2 else "TAKUSAN!"
>>> b
'TAKUSAN!'
――It may be rather difficult to understand if it is continuous ...
――It is sometimes said that it is difficult to understand, but it is better to use it ~ ――Is it against the design concept of python?
Recommended Posts