まず結論

andornot は条件を組み合わせるための書き方です。

age = 20
has_ticket = True

if age >= 18 and has_ticket:
    print("入れます")

この例では、年齢条件とチケット条件の両方を見ています。

こんなときに読む記事です

次のようなところで止まった人向けです。

  • 条件が長くなると読めない
  • andor の違いが分からない
  • not が出てくると混乱する

まずは日本語に置き換えると読みやすくなります。

andは両方

and は、両方の条件が True のときだけ True です。

score = 80
attendance = 90

if score >= 60 and attendance >= 80:
    print("合格")

「点数も出席も条件を満たすなら」と読めます。

orはどちらか

or は、どちらかの条件が True なら True です。

is_weekend = True
is_holiday = False

if is_weekend or is_holiday:
    print("休み")

「週末か祝日なら」と読めます。

notは反対

not は、条件の結果を反対にします。

is_raining = False

if not is_raining:
    print("出かける")

is_rainingFalse なので、not is_rainingTrue になります。

かっこを使うと読みやすい

条件が長いときは、かっこを使うと読みやすくなります。

if (score >= 60) and (attendance >= 80):
    print("合格")

読み方のコツ

次のように読むと、条件式が整理できます。

  • and: かつ
  • or: または
  • not: ではない

英語として覚えるより、条件の日本語として読むのが近道です。