0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Python】ブール型の短絡評価

Last updated at Posted at 2025-03-08

ブール演算子

ブール演算子は、真偽値を組み合わせるための演算子です。

演算子 説明
x or y xの真偽値判定がTrueの場合はxを返す。それ以外はyを返す。
x and y xの真偽値判定がFalseの場合はxを返す。それ以外はyを返す。
not x xの真偽値判定がFalseの場合はTrueを返す。それ以外はFalseを返す。
  • orの場合、左項(x)の真偽値判定がTrueであれば、右項(y)は評価しない。
  • andの場合、左項(x)の真偽値判定がFalseであれば、右項(y)は評価しない。

ブール演算 --- and, or, not:

実装例

Print文の後、Trueを返す関数とFalseを返す関数を実装

python
def ret_true():
    print("Trueを返します")
    return True

def ret_false():
    print("Falseを返します")
    return False

1 and演算子で左項がTrueであれば、右項の真偽判定が実行される。

python
ret = True and ret_false()
print(ret)
  • 実行結果

Falseを返します
False

2 and演算子で左項がFalseであれば、右項の真偽判定は実行されない。

python
ret = False and ret_true()
print(ret)
  • 実行結果

False
3 or演算子で左項がTrueであれば、右項の真偽判定は実行されない。

python
ret = True or ret_false()
print(ret)
  • 実行結果

True
4 or演算子で左項がFalseであれば、右項の真偽判定が実行される。

python
ret = False or ret_true()
print(ret)
  • 実行結果

Trueを返します
True

0
0
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?