Bigger, smaller, or equal?
Learn how to compare values in Python โ is this bigger than that? Are they equal? Let Python judge!
Comparison operators let Python compare two values. Is 5 bigger than 3? Is your score equal to the high score? Comparisons always produce a boolean: True or False. You can use them in if statements to make decisions.
๐ก Think of it like this:
Think of a balance scale. You put one thing on each side and the scale tells you which is heavier. Comparison operators do the same thing โ they compare two values and tell you the result.
Comparisons are how programs check rules. Is the password long enough? Is the temperature too hot? Is the player close enough to the goal? All of these use comparisons.
Where you see this in real life:
In Python, == checks if two things are equal, but = assigns a value. Mixing them up is the #1 beginner mistake!
Comparison operators let Python compare two values. The result is always a boolean:Trueor False.
Click โถ Run to see the output!This is the #1 beginner mistake! = and == are different!
= Assignment
Stores a value in a variable
x = 5 # x now holds 5== Comparison
Checks if two values are equal
x == 5 # True if x is 5Comparisons are most useful inside if statements!
Click โถ Run to see the output!What is the difference between = and == in Python?