Nesting ternary operators in python

The ternary operator puts together simple if-else statements on one line, making them easier to read.

if-else statement

For example, if you want to display foo as the value when v is greater than 10, and bar otherwise, write it in an if-else statement and write it in 4 lines.

if-4 lines when written in else statement


In [22]: for v in range(-5, 15):   
    ...:     if v > 10:               
    ...:         p = 'foo'            
    ...:     else:                    
    ...:         p = 'bar'            
    ...:     print(v, p)              
    ...:                              
-5 bar                                
-4 bar                                
-3 bar                                
-2 bar                                
-1 bar                                
0 bar                                 
1 bar                                 
2 bar                                 
3 bar                                 
4 bar                                 
5 bar                                 
6 bar                                 
7 bar                                 
8 bar                                 
9 bar                                 
10 bar                                
11 foo                                
12 foo                                
13 foo                                
14 foo                                

Ternary operator

The grammar of the ternary operator is

(variable) = (Value when the condition is True) if (conditions) else (conditionsがFalseのときの値) 

The ternary operator can be written in one line.

One line when written with a ternary operator


In [26]: for v in range(-5, 15):
    ...:     p = 'foo' if v > 10 else 'bar'
    ...:     print(v, p)
-5 bar
-4 bar
-3 bar
-2 bar
-1 bar
0 bar
1 bar
2 bar
3 bar
4 bar
5 bar
6 bar
7 bar
8 bar
9 bar
10 bar
11 foo
12 foo
13 foo
14 foo

** Here, the if-else statements between the for-print statements are summarized by the ternary operator. ** **

Nest

if-elif-else statement

When adding one condition and outputting'foobar' if it is less than 0, add ʻelif`.

This will output "'foo' if it is greater than 10,'foobar' if it is less than 0, and'bar' otherwise."

if-Two or more else statement conditions


In [36]: for v in range(-5, 15, 1):
    ...:     if v > 10:
    ...:         p = 'foo'
    ...:     elif v < 0:
    ...:         p = 'foobar'
    ...:     else:
    ...:         p = 'bar'
    ...:     print(v, p)
    ...:
-5 foobar
-4 foobar
-3 foobar
-2 foobar
-1 foobar
0 bar
1 bar
2 bar
3 bar
4 bar
5 bar
6 bar
7 bar
8 bar
9 bar
10 bar
11 foo
12 foo
13 foo
14 foo

Nesting ternary operators

The grammar for nesting ternary operators is

(variable) = (Value when the condition is True) if (conditions) else (conditionsがFalseのときの値) if (conditions) else (conditionsがFalseのときの値) ...if-()-else-()Connect infinitely

Nested ternary operator


In [38]: for v in range(-5, 15, 1):
    ...:     p = 'foo' if v > 10 else 'foobar' if v < 0 else 'bar'
    ...:     print(v, p)
-5 foobar
-4 foobar
-3 foobar
-2 foobar
-1 foobar
0 bar
1 bar
2 bar
3 bar
4 bar
5 bar
6 bar
7 bar
8 bar
9 bar
10 bar
11 foo
12 foo
13 foo
14 foo

reference

Qiita --Ternary Operator (Python)

Recommended Posts

Nesting ternary operators in python
Nesting Python ternary operators
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
# 3 [python3] Various operators
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
StepAIC in Python
N-gram in python
Csv in python
Disassemble in Python
Reflection in Python
Ternary operator (Python)
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Daily AtCoder # 36 in Python
Clustering text in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python
Use config.ini in Python
Solve ABC168D in Python
Daily AtCoder # 7 in Python
LU decomposition in Python
One liner in Python
Simple gRPC in Python