Loading...

Python Comparison

One equals sign stores a value. Two equals signs ask a question. guest = "lida" puts the text under the name. guest == "lida" asks whether the stored text is lida, and the answer is True or False. It does not store anything. print(5 > 2) writes True. print(5 == 2) writes False. print(5 != 2) writes True, because 5 and 2 are not the same.

Larger, the same, or not the same

python
print(5 > 2)
print(5 == 2)
print(5 != 2)

The three lines are True, then False, then True. These are the two values from the page about True and False. A comparison does not write the numbers themselves.

Letter case counts, and text is not a number

print("lida" == "lida") writes True. The same letters in the same case are the same text. print("lida" == "Lida") writes False. A capital L is not a small l. print("2" == 2) writes False. The text 2 and the number 2 are different kinds. The previous page can turn one into the other. This page only asks whether they already match.

Same text, different case, text against a number

python
print("lida" == "lida")
print("lida" == "Lida")
print("2" == 2)

The three lines are True, then False, then False.

5 >= 5 writes True, because "larger or the same" includes the same. 5 > 5 writes False. 5 <= 5 writes True. 5 < 5 writes False. The extra equals sign means "or the same," not a second question.

Six signs, and the question each one asks.

📊 What each sign asks

Sign Question
== Are they the same?
!= Are they not the same?
> Is the first larger?
< Is the first smaller?
>= Is the first larger or the same?
<= Is the first smaller or the same?

The next page joins two answers

This page asks one question at a time. It does not add numbers, choose a branch, or turn text into a number. The next page joins two of these answers with and, or, and not.

Say these yourself

  1. One = stores. Two == ask, and the answer is True or False.
  2. 5 > 2 is True. 5 == 2 is False. 5 != 2 is True.
  3. "lida" == "Lida" is False. "2" == 2 is False.
  4. The next page joins two answers with and, or, and not.

Easy to misread

  • Using one = when you meant to ask
  • Reading 5 == 2 as the number 2
  • Treating "Lida" as the same text as "lida"
  • Treating the text "2" as the number 2
  • Reading >= as two separate questions

Keep

One = stores. Two == ask whether two values are the same. != asks whether they are not.
The answer is True or False, not the number you compared.
5 > 2 is True. 5 == 2 is False. 5 >= 5 is True. 5 > 5 is False.
Letter case counts. The text "2" is not the number 2.
The next page joins two answers with and, or, and not.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top