Answers for "python oneline if elif else"

1

python if elif else in one line

>>> i=100
>>> a = 1 if i<100 else 2 if i>100 else 0
>>> a
0
>>> i=101
>>> a = 1 if i<100 else 2 if i>100 else 0
>>> a
2
>>> i=99
>>> a = 1 if i<100 else 2 if i>100 else 0
>>> a
1
Posted by: Guest on June-03-2020
0

python in line elif

# You can nest if/else to get the same effect as elif but it's hard to read and not recommended
msg = "Hi " + ("there" if not name else ("Neo" if name == "Anderson" else name))
Posted by: Guest on July-27-2021

Python Answers by Framework

Browse Popular Code Answers by Language