比較演算子
メニューを表示するにはスワイプしてください
比較は、プログラムが値についてはい/いいえの質問を行うことを可能にします。
- 等しいか?
- どちらが大きいか?
- 値が範囲内にあるか?
比較はブール値(True / False)を返し、ifやwhileで利用されます。
等価性 ==
2つの値が同じかどうかを確認します。
1234567saved_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
注意
= - 代入演算子、== - 比較演算子。
不等号 !=
値が異なる場合に真。
1234567user_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
より大きい >
1234567estimated = 7 actual = 9 print(estimated > actual) # False rating_a = 12 rating_b = 3 print(rating_a > rating_b) # True
より小さい <
12345user_age = 17 min_age = 18 print(user_age < min_age) # True print("Alice" < "Bob") # True
より大きいまたは等しい >=
123student_score = 7 passing = 7 print(student_score >= passing) # True
以下または等しい <=
123order_total = 10 limit = 9 print(order_total <= limit) # False
連鎖比較
Python では自然な範囲指定が可能:
0 < x < 10 は (0 < x) and (x < 10) と同じ動作。
12345temperature = 7 print(0 < temperature < 10) # True user_rating = 7 print(5 <= user_rating <= 7) # True
文字列の比較
文字列の比較は大文字と小文字を区別し、辞書式順序で行われる。
12345saved_password = "Apple" typed_password = "apple" print(saved_password == typed_password) # False print("apple" < "banana") # True
大文字と小文字を区別しない一致の場合:
123email_stored = "Support@Codefinity.com" email_input = "support@codefinity.COM" print(email_stored.lower() == email_input.lower()) # True
すべて明確でしたか?
フィードバックありがとうございます!
セクション 2. 章 2
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください
セクション 2. 章 2