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?

よく使う条件式(==, !=, >, <, in)まとめ【Day 9】

0
Last updated at Posted at 2025-12-08

Qiita Advent Calendar 2025 のパイソニスタの一人アドカレ Day9 の記事です。

よく使う条件式(==, !=, >, <, in)まとめ

条件分岐で頻繁に使う「条件式(比較式)」をまとめて理解しましょう。
ここを押さえると if 文が一気に書きやすくなります。

==(等しい)

x = 10
if x == 10:
    print("10です")

値が同じかどうかを比較します。

!=(等しくない)

if x != 10:
    print("10ではありません")

値が違うときに True になります。

> / <(より大きい・より小さい)

n = 7

if num > 3: # True
    print("大きい")

if num < 3: # False
    print("小さい")

数値比較でよく登場します。

>= / <=(以上・以下)

score = 70

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

if score <= 40:
    print("追試")

“以上・以下” の条件を明確にできます。

in(含まれているか)

color = "red"

if color in ["red", "blue", "green"]:
    print("有効な色です")

「リストや文字列の中にあるか」を調べるときに便利です。

文字列でも使えます。

if "a" in "apple":
    print("含まれています")

条件式どう選ぶ?

やりたいこと 条件式
値が等しいか調べたい ==
値が違うか調べたい !=
大小比較をしたい >, <, >=, <=
含まれているか調べたい in

よくある間違い

= と == を間違える

if x = 10:  # エラー

= は「代入」
== は「比較」

まとめ

  • 条件式は if 文の「核」
  • ==, !=, >, <, >=, <=, in を使えると一気に表現が広がる
  • 比較は True / False を返す式になっていればOK
0
0
0

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?