Comparison Operators
Comparisons let your program ask yes/no questions about values:
- Are these equal?
- Is one bigger?
- Is a value inside a range?
A comparison returns a Boolean (True / False) and powers if/while.
Equality ==
Checks whether two values are the same.
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
= - assigns, == - compares.
Inequality !=
True when values differ.
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
Greater Than >
1234567estimated = 7 actual = 9 print(estimated > actual) # False rating_a = 12 rating_b = 3 print(rating_a > rating_b) # True
Less Than <
12345user_age = 17 min_age = 18 print(user_age < min_age) # True print("Alice" < "Bob") # True
Greater or Equal >=
123student_score = 7 passing = 7 print(student_score >= passing) # True
Less or Equal <=
123order_total = 10 limit = 9 print(order_total <= limit) # False
Chained Comparisons
Python supports natural ranges:
0 < x < 10 works like (0 < x) and (x < 10).
12345temperature = 7 print(0 < temperature < 10) # True user_rating = 7 print(5 <= user_rating <= 7) # True
Comparing Strings
String comparisons are case-sensitive and lexicographic.
12345saved_password = "Apple" typed_password = "apple" print(saved_password == typed_password) # False print("apple" < "banana") # True
For case-insensitive matching:
123email_stored = "Support@Codefinity.com" email_input = "support@codefinity.COM" print(email_stored.lower() == email_input.lower()) # True
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.45
Comparison Operators
Swipe to show menu
Comparisons let your program ask yes/no questions about values:
- Are these equal?
- Is one bigger?
- Is a value inside a range?
A comparison returns a Boolean (True / False) and powers if/while.
Equality ==
Checks whether two values are the same.
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
= - assigns, == - compares.
Inequality !=
True when values differ.
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
Greater Than >
1234567estimated = 7 actual = 9 print(estimated > actual) # False rating_a = 12 rating_b = 3 print(rating_a > rating_b) # True
Less Than <
12345user_age = 17 min_age = 18 print(user_age < min_age) # True print("Alice" < "Bob") # True
Greater or Equal >=
123student_score = 7 passing = 7 print(student_score >= passing) # True
Less or Equal <=
123order_total = 10 limit = 9 print(order_total <= limit) # False
Chained Comparisons
Python supports natural ranges:
0 < x < 10 works like (0 < x) and (x < 10).
12345temperature = 7 print(0 < temperature < 10) # True user_rating = 7 print(5 <= user_rating <= 7) # True
Comparing Strings
String comparisons are case-sensitive and lexicographic.
12345saved_password = "Apple" typed_password = "apple" print(saved_password == typed_password) # False print("apple" < "banana") # True
For case-insensitive matching:
123email_stored = "Support@Codefinity.com" email_input = "support@codefinity.COM" print(email_stored.lower() == email_input.lower()) # True
Thanks for your feedback!