Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Comparison Operators | Booleans and Comparisons
Data Types in Python

bookComparison 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.

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
Note

= - assigns, == - compares.

Inequality !=

True when values differ.

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

Greater Than >

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

Less Than <

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

Greater or Equal >=

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

Less or Equal <=

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

Chained Comparisons

Python supports natural ranges: 0 < x < 10 works like (0 < x) and (x < 10).

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

Comparing Strings

String comparisons are case-sensitive and lexicographic.

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

For case-insensitive matching:

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

Which single expression correctly checks that x is between 1 and 5 inclusive (using chaining)?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 3.45

bookComparison 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.

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
Note

= - assigns, == - compares.

Inequality !=

True when values differ.

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

Greater Than >

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

Less Than <

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

Greater or Equal >=

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

Less or Equal <=

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

Chained Comparisons

Python supports natural ranges: 0 < x < 10 works like (0 < x) and (x < 10).

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

Comparing Strings

String comparisons are case-sensitive and lexicographic.

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

For case-insensitive matching:

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

Which single expression correctly checks that x is between 1 and 5 inclusive (using chaining)?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2
some-alt