Python notes using perl-ternary operator

--Make a note of what you thought was python.

Isn't there a ternary operator?

――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.

Complex ternary operator

-(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 ...

I want it after all ~

――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

Python notes using perl-ternary operator
Notes using Python subprocesses
Python notes using perl-special variables
Notes on using MeCab from Python
Notes on installing Python using PyEnv
Notes on using rstrip with python.
Notes for using OpenCV on Windows10 Python 3.8.3.
Python scraping notes
Notes using cChardet and python3-chardet in Python 3.3.1.
Python study notes _000
Python learning notes
Python beginner notes
Python study notes_006
Start using Python
python C ++ notes
Python 3 operator memo
Python study notes _005
Python grammar notes
Python Library notes
Notes for using python (pydev) in eclipse
Ternary operator (Python)
python personal notes
Scraping using Python
Notes on using code formatter in Python
python pandas notes
Python study notes_001
python learning notes
Python3.4 installation notes
Notes on installing Python3 and using pip on Windows7
Notes on using dict in python [Competition Pro]
[Python] Notes on accelerating genetic algorithms using multiprocessing
Operate Redmine using Python Redmine
missingintegers python personal notes
Fibonacci sequence using Python
Data analysis using Python 0
Python package development notes
Data cleaning using Python
python decorator usage notes
Python ipaddress package notes
[Personal notes] Python, Django
Using Python #external packages
Python Pickle format notes
WiringPi-SPI communication using Python
[Python] pytest-mock Usage notes
First Python miscellaneous notes
Age calculation using python
Matlab => Python migration notes
Search Twitter using Python
Notes around Python3 assignments
Name identification using python
Notes on using Alembic
Try using Tweepy [Python2.7]
Python try / except notes
Python operator overload precedence
Python framework bottle notes
Minimum notes when using Python on Mac (Homebrew edition)
Flatten using Python yield from
Scraping using Python 3.5 async / await
Save images using python3 requests
[S3] CRUD with S3 using Python [Python]
O'Reilly python3 Primer Learning Notes