# 条件式(三項演算子)の基本パターン x = 101 s = 'odd' if x % 2 else 'even' print(s) # odd # 条件式のネスト s = 'positive' if x > 0 else 'negative' if x < 0 else 'zero' print(s) # positive # 内包表記と条件式 l1 = [3, 4, 6, 0, 1] l2 = ['odd' if x % 2 else 'even' for x in l1] print(l2) # ['odd', 'even', 'even', 'even', 'odd'] # ラムダ式と条件式 l1 = [3, 4, 6, 0, 1] it = map(lambda x: 'odd' if x % 2 else 'even', l1) l2 = list(it) print(
![[解決!Python]条件式(三項演算子)を使ってif文を1行で書くには](https://cdn-ak-scissors.b.st-hatena.com/image/square/6bfa235cd109a82a6d2ff346889a5fc959d84a49/height=288;version=1;width=512/https%3A%2F%2Fimage.itmedia.co.jp%2Fait%2Farticles%2F2104%2F02%2Fcover_news016.png)