83
74

More than 5 years have passed since last update.

【Python】一行でif/elseを書く

Posted at

Pythonの基本中の基本だけれど、いっつも忘れるのでメモ

if a > 5:
    i = a
else:
    i = 0

i = a if a > 5 else 0

みたいな感じで書ける


True/Falseの条件式の場合

if string:
    i = "True"
else:
    i = "False"

i = "True" if string else "False"

は同じ

83
74
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
83
74