Help us understand the problem. What is going on with this article?

三項演算子(Python)

More than 1 year has passed since last update.

 三項演算子を使うことで代入の条件分岐を一行でスマートに記述することができます。

用法

(変数) = (条件がTrueのときの値) if (条件) else (条件がFalseのときの値)

 例えば次のような条件によってxの値を分けたいとき

python
if n == 10:
    x = "OK"
else :
    x = "NG"

 
 これを三項演算子を用いて一行で表現すると次のようになります。

python
x = "OK" if n == 10 else "NG"
howmuch515
There is nothing that I want to make.
https://howmuch515.github.io/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした