LoginSignup
0
0

More than 1 year has passed since last update.

python if文の条件の判定順

Posted at

すこし気になることがあり、if条件判定順について確認してみた。(個人的メモ)

下のコードのように配列長より長い数値が入りうる変数で配列の要素をチェックする際に、if文を一気に書いた方がインデントが減るので確認してみた。(phpだと定義されている(と認識している)ので、個人的にはよく使っていた)

import numpy as np
array_Test = np.repeat(True,10)
for i in range(0,15):
    if i < len(array_Test) and array_Test[i]==True:#配列の要素をチェックすることを強調
        print(i,"True")
    else:
        print(i,"False")
## 結果
# 0 True
# 1 True
# 2 True
# 3 True
# 4 True
# 5 True
# 6 True
# 7 True
# 8 True
# 9 True
# 10 False
# 11 False
# 12 False
# 13 False
# 14 False

これで、配列より長くないということでエラーは起こさない。
if文の条件の判定順はpythonでも左からとして使えそう。

0
0
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
0
0