はじめに
登場人物
青山 千尋(あおやま ちひろ)先生:
学校の先生でプログラミングに詳しい人物。絵里と優奈が通う学校のプログラミング部の顧問。
藤崎 絵里(ふじさき えり):
青山先生が顧問をしているプログラミング部の部員。プログラミングは未経験。
朝倉 優奈(あさくら ゆな):
青山先生が顧問をしているプログラミング部の部員。絵里の親友。プログラミングは未経験。
月島 結翔(つきしま ゆいと)ナレーター:
本ブログでナレーションを担当。普段は、地元のTV番組やラジオ番組でナレーターとして活動。
第7章 論理演算子を知ろう
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-28-00.53.03-Create-an-illustration-of-a-young-male-teacher-in-an-anime-style.-He-has-a-professional-but-friendly-demeanor-with-short-neatly-styled-hair-and-glas-150x150.webp)
この章では論理演算子について一緒に学んでいきましょう。
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
ちひろ先生、論理演算子について教えてください。
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
もちろんよ。論理演算子とは、真偽値(TrueかFalse)を操作するために使われる特別な演算子のことよ。Pythonでは、and
、or
、not
という3つの論理演算子があるわ。
念のため、真はTrue、偽はFalseのことよ。
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.41.35-Create-an-illustration-of-a-young-girl-with-black-short-hair-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.41.35-Create-an-illustration-of-a-young-girl-with-black-short-hair-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.41.35-Create-an-illustration-of-a-young-girl-with-black-short-hair-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp--150x150.webp)
それぞれの演算子はどんな意味があるんですか?
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
まずand
演算子はね、二つの条件がともにTrueである場合にのみTrueを返すの。たとえば、True and True
はTrueになるけれど、True and False
はFalseになるのよ。
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
へえ、じゃあor
演算子はどうなんですか?
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
or
演算子は、条件のうち少なくとも一方がTrueであればTrueを返すのよ。だから、True or False
はTrueになるし、False or False
の場合だけがFalseになるわ。
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
そうなんだぁ、なんかややこしいですね。。。
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.41.35-Create-an-illustration-of-a-young-girl-with-black-short-hair-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.41.35-Create-an-illustration-of-a-young-girl-with-black-short-hair-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.41.35-Create-an-illustration-of-a-young-girl-with-black-short-hair-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp--150x150.webp)
not
演算子はどんな役割があるんですか?
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
not
演算子はね、条件の真偽値を逆転させるわ。つまり、TrueをFalseに、そしてFalseをTrueに変えるの。not True
はFalseになり、not False
はTrueになるわよ。
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
それぞれの演算子を一緒に使うこともできますか?
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
もちろんよ。複雑な条件を評価する時には、これらの演算子を組み合わせて使うことができるわ。ただ、複数の論理演算子を組み合わせる場合には、操作の順番を明確にするために括弧を使った方がいいわね。
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
括弧を使わない場合は、どうなるんですか?
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
Pythonにおける論理演算子の優先順位は、以下の順番になっているの。括弧の中の計算
not
and
or
だから、括弧をつけていないと、この優先順位を意識して解読する必要があり、すごく理解し難くなるのよ。
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
なるほど、だから括弧をつけた方がいいんですね。
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
その通りよ。
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.41.35-Create-an-illustration-of-a-young-girl-with-black-short-hair-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.41.35-Create-an-illustration-of-a-young-girl-with-black-short-hair-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.41.35-Create-an-illustration-of-a-young-girl-with-black-short-hair-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp--150x150.webp)
ちひろ先生、論理演算子を組み合わせた具体的な例を教えていただけますか?
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
もちろんよ。例えば、ある試験に合格するためには、成績が70点以上成績 >= 70
であるか、特別な推薦が必要推薦 == True
だとしましょう。この条件をPythonで表現すると、(成績 >= 70) or (推薦 == True)
となるわ。このようにして、and
、or
、not
を上手に使いこなすことで、複雑な条件も簡単に表現できるのよ。
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.41.35-Create-an-illustration-of-a-young-girl-with-black-short-hair-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.41.35-Create-an-illustration-of-a-young-girl-with-black-short-hair-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.41.35-Create-an-illustration-of-a-young-girl-with-black-short-hair-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp--150x150.webp)
とってもわかりやすかったです!ありがとうございます。
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-26-22.36.07-Create-an-illustration-of-a-young-girl-with-a-ponytail-in-an-anime-style.-She-should-have-large-expressive-almond-shaped-eyes-to-give-a-sharp-look--150x150.webp)
私も理解できました!ちひろ先生、ありがとうございます。
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
![](https://botdevstudio.com/wp-content/uploads/2024/02/青山先生-150x150.png)
それでは、次の章では、代入演算子(=
)を勉強しましょう。
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-28-00.53.03-Create-an-illustration-of-a-young-male-teacher-in-an-anime-style.-He-has-a-professional-but-friendly-demeanor-with-short-neatly-styled-hair-and-glas-150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-28-00.53.03-Create-an-illustration-of-a-young-male-teacher-in-an-anime-style.-He-has-a-professional-but-friendly-demeanor-with-short-neatly-styled-hair-and-glas-150x150.webp)
![](https://botdevstudio.com/wp-content/uploads/2024/02/DALL·E-2024-02-28-00.53.03-Create-an-illustration-of-a-young-male-teacher-in-an-anime-style.-He-has-a-professional-but-friendly-demeanor-with-short-neatly-styled-hair-and-glas-150x150.webp)
「本章のまとめ」を確認後、第8章へ進んでください。
本章のまとめ
- 論理演算子は真偽値(TrueかFalse)を操作するための演算子
- Pythonでは、and、or、notの3つの論理演算子がある
- and演算子は二つの条件がともにTrueの場合のみTrueを返す
- or演算子は条件の少なくとも一方がTrueであればTrueを返す
- not演算子は条件の真偽値を逆転させる
- 複雑な条件評価にはこれらの演算子を組み合わせて使用する
- 論理演算子を組み合わせる際は括弧を使うと良い
- Pythonにおける論理演算子の優先順位は「括弧内の計算 > not > and > or」となる