Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ 比較演算子 | ブール値と比較
Pythonのデータ型

book比較演算子

メニューを表示するにはスワイプしてください

比較は、プログラムが値についてはい/いいえの質問を行うことを可能にします。

  • 等しいか?
  • どちらが大きいか?
  • 値が範囲内にあるか?

比較はブール値True / False)を返し、ifwhileで利用されます。

等価性 ==

2つの値が同じかどうかを確認します。

1234567
saved_pin = 1234 entered_pin = 1234 print(saved_pin == entered_pin) # True stored_email = "support@codefinity.com" input_email = "Support@codefinity.com" print(stored_email == input_email) # False
copy
Note
注意

= - 代入演算子、== - 比較演算子。

不等号 !=

値が異なる場合に真。

1234567
user_id_1 = 105 user_id_2 = 203 print(user_id_1 != user_id_2) # True username_1 = "alex" username_2 = "alex" print(username_1 != username_2) # False
copy

より大きい >

1234567
estimated = 7 actual = 9 print(estimated > actual) # False rating_a = 12 rating_b = 3 print(rating_a > rating_b) # True
copy

より小さい <

12345
user_age = 17 min_age = 18 print(user_age < min_age) # True print("Alice" < "Bob") # True
copy

より大きいまたは等しい >=

123
student_score = 7 passing = 7 print(student_score >= passing) # True
copy

以下または等しい <=

123
order_total = 10 limit = 9 print(order_total <= limit) # False
copy

連鎖比較

Python では自然な範囲指定が可能: 0 < x < 10(0 < x) and (x < 10) と同じ動作。

12345
temperature = 7 print(0 < temperature < 10) # True user_rating = 7 print(5 <= user_rating <= 7) # True
copy

文字列の比較

文字列の比較は大文字と小文字を区別し、辞書式順序で行われる。

12345
saved_password = "Apple" typed_password = "apple" print(saved_password == typed_password) # False print("apple" < "banana") # True
copy

大文字と小文字を区別しない一致の場合:

123
email_stored = "Support@Codefinity.com" email_input = "support@codefinity.COM" print(email_stored.lower() == email_input.lower()) # True
copy
question mark

x1 以上 5 以下であることを(連鎖比較を使って)正しく判定する単一の式はどれですか?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 2.  2

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 2.  2
some-alt