メインコンテンツへスキップ
Lesson4 / 9

ブール型(bool)と真偽値

目次

このレッスンで学ぶこと

  • ブール型とは何か
  • 真(True)と偽(False)の使い方
  • 比較演算子による真偽値の生成
  • 論理演算子(and, or, not)の使い方

ブール型とは

**ブール型(bool)**は、「真」か「偽」かの2つの値だけを持つデータ型です。

プログラムでは、「条件が成立しているか」「データが存在するか」など、Yes/No、正しい/間違っている、といった2択の判断が必要な場面が頻繁にあります。この判断結果を表すのがブール型です。

ブール型の2つの値

Pythonのブール型には、以下の2つの値しかありません:

意味読み方
True真(正しい、Yes、成立)トゥルー
False偽(間違い、No、不成立)フォルス
Python
# ブール型の値
is_student = True   # 学生である(真)
is_adult = False    # 成人ではない(偽)

print(type(True))   # <class 'bool'>
print(type(False))  # <class 'bool'>

重要: TrueFalseは最初の文字が大文字です。truefalse(小文字)はエラーになります。

ブール型の使用例

Python
# ログイン状態
is_logged_in = True

# 会員かどうか
is_member = False

# 在庫があるか
has_stock = True

💡 豆知識: ブール型の名前は、19世紀のイギリスの数学者ジョージ・ブール(George Boole)に由来します。彼が考案したブール代数は、現代のコンピュータの基礎となっています。


なぜブール型が必要なのか?

プログラムは常に「判断」を行います。ブール型は、その判断結果を明確に表現するために不可欠です。

ブール型を使う場面

Python
age = 20

# 成人かどうかを判定
if age >= 18:
    print("成人です")
else:
    print("未成年です")

この例で、age >= 18 の結果は:

  • 成立する場合 → True(真)
  • 成立しない場合 → False(偽)

ブール型の利点

  1. 条件分岐: if文でプログラムの流れを制御
  2. ループ制御: while文で繰り返しの継続を判断
  3. フラグ管理: 状態を表す変数として使用
  4. データ検証: 入力が正しいかどうかをチェック
Python
# 状態管理の例
is_running = True

while is_running:
    command = input("コマンド: ")
    if command == "quit":
        is_running = False  # ループを終了

ブール型の基本

TrueとFalse

Python
# ブール型の値は2つだけ
is_student = True
is_adult = False

print(is_student)  # True
print(is_adult)    # False
print(type(True))  # <class 'bool'>

ポイント: 先頭は大文字(True, False)です。小文字(true, false)はエラーになります。


比較演算子

等しい・等しくない

機能: 2つの値が等しいか、等しくないかを比較します。

Python
# == : 等しい
print(10 == 10)   # True
print(10 == 20)   # False

# != : 等しくない
print(10 != 20)   # True
print(10 != 10)   # False

# 文字列の比較
print("Python" == "Python")  # True
print("Python" == "Java")    # False

用途: 値の一致判定、条件分岐

注意: 等号は == (2つ)です。= は代入演算子なので混同しないように注意してください。


大小の比較

機能: 数値の大小を比較します。

Python
# > : より大きい
print(10 > 5)   # True
print(10 > 20)  # False

# < : より小さい
print(10 < 20)  # True
print(10 < 5)   # False

# >= : 以上(等しいを含む)
print(10 >= 10)  # True
print(10 >= 5)   # True
print(10 >= 20)  # False

# <= : 以下(等しいを含む)
print(10 <= 10)  # True
print(10 <= 20)  # True
print(10 <= 5)   # False

用途: 数値の範囲チェック、年齢判定、点数評価


論理演算子

and - 両方が真

機能: 両方の条件が真(True)の場合のみ、結果が真になります。

Python
# 両方Trueなら結果はTrue
print(True and True)    # True
print(True and False)   # False
print(False and False)  # False

# 実用例
age = 20
has_license = True

# 18歳以上かつ免許を持っている
can_drive = age >= 18 and has_license
print(can_drive)  # True

用途: 複数の条件を同時に満たす必要がある場合


or - どちらかが真

機能: どちらか一方でも真(True)なら、結果が真になります。

Python
# どちらかがTrueなら結果はTrue
print(True or False)   # True
print(False or True)   # True
print(False or False)  # False

# 実用例
is_holiday = False
is_weekend = True

# 祝日または週末なら休み
is_day_off = is_holiday or is_weekend
print(is_day_off)  # True

用途: 複数の条件のうち、どれか1つでも満たせば良い場合


not - 否定

機能: 真偽値を反転します(True → False、False → True)。

Python
print(not True)   # False
print(not False)  # True

# 実用例
is_raining = False
can_go_out = not is_raining
print(can_go_out)  # True

用途: 条件の反転、「〜でない」の表現


具体例

例1: 年齢による判定

Python
age = 25

# 成人判定
is_adult = age >= 18
print(f"成人: {is_adult}")  # True

# 高齢者判定
is_senior = age >= 65
print(f"高齢者: {is_senior}")  # False

# 成人かつ高齢者でない
is_working_age = is_adult and not is_senior
print(f"労働年齢: {is_working_age}")  # True

例2: パスワードの検証

Python
password = "MyPass123"

# 8文字以上
is_long_enough = len(password) >= 8
print(f"長さOK: {is_long_enough}")  # True

# 数字を含む
has_number = any(c.isdigit() for c in password)
print(f"数字あり: {has_number}")  # True

# 両方の条件を満たす
is_valid = is_long_enough and has_number
print(f"有効なパスワード: {is_valid}")  # True

例3: 割引条件の判定

Python
age = 15
is_student = True

# 18歳未満または学生なら割引
gets_discount = age < 18 or is_student
print(f"割引対象: {gets_discount}")  # True(年齢が18歳未満)

# 65歳以上も割引
age2 = 70
is_student2 = False
gets_discount2 = age2 < 18 or age2 >= 65 or is_student2
print(f"割引対象: {gets_discount2}")  # True(65歳以上)

例4: 範囲チェック

Python
score = 85

# 0〜100の範囲内か
is_valid_score = score >= 0 and score <= 100
print(f"有効な点数: {is_valid_score}")  # True

# 別の書き方(チェーン比較)
is_valid_score2 = 0 <= score <= 100
print(f"有効な点数: {is_valid_score2}")  # True

例5: 値の真偽判定

Python
# bool()で真偽値に変換
print(bool(1))      # True(0以外の数値はTrue)
print(bool(0))      # False(0はFalse)
print(bool(""))     # False(空文字列はFalse)
print(bool("text")) # True(空でない文字列はTrue)
print(bool([]))     # False(空リストはFalse)
print(bool([1, 2])) # True(空でないリストはTrue)

# 条件分岐で利用
name = ""
if name:
    print(f"こんにちは、{name}さん")
else:
    print("名前が入力されていません")

例6: 複数条件の組み合わせ

Python
temperature = 28
humidity = 75

# 暑い日の判定(気温25度以上または湿度70%以上)
is_hot = temperature >= 25 or humidity >= 70
print(f"暑い: {is_hot}")  # True

# 熱中症注意(気温30度以上かつ湿度70%以上)
is_heat_stroke_risk = temperature >= 30 and humidity >= 70
print(f"熱中症注意: {is_heat_stroke_risk}")  # False

よくある間違い

間違い1: 代入演算子と比較演算子の混同

Python
x = 10

# 間違い: = は代入
if x = 10:  # SyntaxError
    print("10です")

正しい方法:

Python
x = 10

# 正しい: == は比較
if x == 10:
    print("10です")

間違い2: TrueとFalseの大文字小文字

Python
# 間違い: 小文字は変数として扱われる
result = true  # NameError: name 'true' is not defined

正しい方法:

Python
# 正しい: 先頭は大文字
result = True

間違い3: 論理演算子の優先順位

Python
# andはorより優先される
result = True or False and False
print(result)  # True(True or (False and False) と解釈される)

明示的に:

Python
# 括弧で明示する
result1 = (True or False) and False
print(result1)  # False

result2 = True or (False and False)
print(result2)  # True

間違い4: 文字列との比較

Python
# "True"という文字列はbool型のTrueではない
text = "True"
print(text == True)   # False(型が違う)
print(bool(text))     # True(空でない文字列はTrue)

正しい方法:

Python
text = "True"

# 文字列として比較
if text == "True":
    print("文字列がTrueです")

# bool型に変換(文字列が空かどうか)
if bool(text):
    print("空でない文字列です")

間違い5: 範囲チェックの書き方

Python
x = 50

# 間違い: これは動くが意図と違う
if x > 0 or x < 100:  # 常にTrue(すべての数値はどちらかを満たす)
    print("範囲内")

正しい方法:

Python
x = 50

# 正しい: andを使う
if x > 0 and x < 100:
    print("範囲内")

# またはチェーン比較
if 0 < x < 100:
    print("範囲内")

練習問題

問題1(基礎)⭐☆☆

2つの数値 a = 15, b = 20 があります。「aはbより小さい」という条件をブール型で表現し、結果を表示してください。

💡 ヒント

< 演算子を使います。

✅ 解答例
Python
a = 15
b = 20

result = a < b
print(result)  # True
print(f"aはbより小さい: {result}")

実行結果:

True
aはbより小さい: True

解説: a < b は「aがbより小さい」という条件を表し、結果は真偽値(True/False)になります。


問題2(基礎)⭐☆☆

年齢 age = 16 に対して、「18歳以上かつ65歳未満」という条件を判定してください。

💡 ヒント

and 演算子を使います。age >= 18 and age < 65

✅ 解答例
Python
age = 16

is_working_age = age >= 18 and age < 65
print(f"労働年齢: {is_working_age}")

# またはチェーン比較
is_working_age2 = 18 <= age < 65
print(f"労働年齢: {is_working_age2}")

実行結果:

労働年齢: False
労働年齢: False

解説: 16歳は18歳未満なので、条件を満たさず False になります。


問題3(応用)⭐⭐☆

テストの点数 score = 85 と出席率 attendance = 0.9(90%)があります。以下の条件で合格判定をしてください:

  • 点数が60点以上、かつ出席率が80%以上なら合格
💡 ヒント
  1. score >= 60 で点数をチェック
  2. attendance >= 0.8 で出席率をチェック
  3. and で両方の条件を結合
✅ 解答例
Python
score = 85
attendance = 0.9

# 合格条件
is_passing = score >= 60 and attendance >= 0.8

if is_passing:
    print("合格")
else:
    print("不合格")

print(f"点数: {score}点")
print(f"出席率: {attendance * 100}%")
print(f"結果: {'合格' if is_passing else '不合格'}")

実行結果:

合格
点数: 85点
出席率: 90.0%
結果: 合格

解説: 点数85点(60点以上)、出席率90%(80%以上)なので、両方の条件を満たし合格となります。


まとめ

このレッスンでは、ブール型と条件判定の基礎を学びました。

  • ブール型は TrueFalse の2値で、条件分岐の基盤になります。
  • 比較演算子を使うことで、値の関係を真偽値として判定できます。
  • and, or, not を使って、複数条件を組み合わせた判定ができます。
  • bool() を使うと、他の型を条件判定に使える形へ変換できます。
  • True / False の表記や === の使い分けを正確に書くことが重要です。