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 the same?
  • Is this bigger?
  • Does this number fall inside a range?

A comparison returns a Boolean (True or False) and is the backbone of if/while logic.

The essentials

Python provides six comparison operators (==, !=, <, <=, >, >=) to test equality and ordering between values; each comparison evaluates to True or False.

Equality β€” ==

Checks whether two values are the same.

12
print(5 == 5) # True print("apple" == "Apple") # False (case matters)
copy
Note
Note

= assigns a value to a variable, while == compares two values.

Inequality β€” !=

Checks whether two values are different.

12
print(5 != 3) # True print("cat" != "cat") # False
copy

Greater Than β€” >

True if the left value is strictly larger than the right.

12
print(7 > 9) # False print(12 > 3) # True
copy

Less Than β€” <

True if the left value is strictly smaller than the right.

12
print(2 < 10) # True print("a" < "b") # True (lexicographic order)
copy

Greater Than or Equal β€” >=

True if the left value is larger or equal to the right.

12
print(7 >= 7) # True print(4 >= 9) # False
copy

Less Than or Equal β€” <=

True if the left value is smaller or equal to the right.

12
print(10 <= 9) # False print(5 <= 5) # True
copy

Chained comparisons

Python lets you write ranges naturally: 0 < x < 10 means "x is greater than 0 and less than 10". Under the hood it behaves like (0 < x) and (x < 10).

123
x = 7 print(0 < x < 10) # True print(5 <= x <= 7) # True
copy

This reads cleanly and avoids repeating x.

Floating-point nuance (tiny rounding errors)

Some decimals (like 0.1) cannot be represented exactly in binary. That's why strict equality on floats can surprise you.

1
print(0.1 + 0.2 == 0.3) # False in many environments
copy

When comparing floats for "equality", prefer a tolerance check.

12
import math print(math.isclose(0.1 + 0.2, 0.3, rel_tol=1e-9)) # True
copy

You're not saying "exactly equal", you're saying "close enough".

Comparing strings (case and order)

String comparisons are case-sensitive and lexicographic (character-by-character in Unicode order).

12
print("apple" == "Apple") # False (case matters) print("apple" < "banana") # True ("a" comes before "b")
copy

For case-insensitive checks, normalize both sides first.

12
s1, s2 = "Hello", "heLLo" print(s1.lower() == s2.lower()) # True
copy

1. Fill in the blanks with True or False:

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

3. Which string comparison is True?

question-icon

Fill in the blanks with True or False:

5 == 5 β†’
3 < 2 β†’

9 >= 9 β†’

"A" == "a" β†’

0 < 7 <= 7 β†’

Click or drag`n`drop items and fill in the blanks

question mark

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

Select the correct answer

question mark

Which string comparison is True?

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 5

bookComparison Operators

Swipe to show menu

Comparisons let your program ask yes/no questions about values:

  • Are these the same?
  • Is this bigger?
  • Does this number fall inside a range?

A comparison returns a Boolean (True or False) and is the backbone of if/while logic.

The essentials

Python provides six comparison operators (==, !=, <, <=, >, >=) to test equality and ordering between values; each comparison evaluates to True or False.

Equality β€” ==

Checks whether two values are the same.

12
print(5 == 5) # True print("apple" == "Apple") # False (case matters)
copy
Note
Note

= assigns a value to a variable, while == compares two values.

Inequality β€” !=

Checks whether two values are different.

12
print(5 != 3) # True print("cat" != "cat") # False
copy

Greater Than β€” >

True if the left value is strictly larger than the right.

12
print(7 > 9) # False print(12 > 3) # True
copy

Less Than β€” <

True if the left value is strictly smaller than the right.

12
print(2 < 10) # True print("a" < "b") # True (lexicographic order)
copy

Greater Than or Equal β€” >=

True if the left value is larger or equal to the right.

12
print(7 >= 7) # True print(4 >= 9) # False
copy

Less Than or Equal β€” <=

True if the left value is smaller or equal to the right.

12
print(10 <= 9) # False print(5 <= 5) # True
copy

Chained comparisons

Python lets you write ranges naturally: 0 < x < 10 means "x is greater than 0 and less than 10". Under the hood it behaves like (0 < x) and (x < 10).

123
x = 7 print(0 < x < 10) # True print(5 <= x <= 7) # True
copy

This reads cleanly and avoids repeating x.

Floating-point nuance (tiny rounding errors)

Some decimals (like 0.1) cannot be represented exactly in binary. That's why strict equality on floats can surprise you.

1
print(0.1 + 0.2 == 0.3) # False in many environments
copy

When comparing floats for "equality", prefer a tolerance check.

12
import math print(math.isclose(0.1 + 0.2, 0.3, rel_tol=1e-9)) # True
copy

You're not saying "exactly equal", you're saying "close enough".

Comparing strings (case and order)

String comparisons are case-sensitive and lexicographic (character-by-character in Unicode order).

12
print("apple" == "Apple") # False (case matters) print("apple" < "banana") # True ("a" comes before "b")
copy

For case-insensitive checks, normalize both sides first.

12
s1, s2 = "Hello", "heLLo" print(s1.lower() == s2.lower()) # True
copy

1. Fill in the blanks with True or False:

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

3. Which string comparison is True?

question-icon

Fill in the blanks with True or False:

5 == 5 β†’
3 < 2 β†’

9 >= 9 β†’

"A" == "a" β†’

0 < 7 <= 7 β†’

Click or drag`n`drop items and fill in the blanks

question mark

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

Select the correct answer

question mark

Which string comparison is True?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2
some-alt