โ† Back to Python Course
๐Ÿค” Making Decisionsยท20 minยทBeginner
โš–๏ธ

Comparisons

Bigger, smaller, or equal?

Learn how to compare values in Python โ€” is this bigger than that? Are they equal? Let Python judge!

๐Ÿ“–

What we are learning

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.

๐ŸŒŸ

Why it matters

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:

  • โœ“Thermostats check if the temperature is above or below a target
  • โœ“Games check if your score is greater than the high score
  • โœ“Elevators check if the current floor equals the requested floor
๐ŸŽฏ

By the end, you can:

  • 1.Use ==, !=, <, >, <=, >= to compare values
  • 2.Combine comparisons with and, or, not
  • 3.Use comparisons in if statements
๐Ÿ”ค

New words

equalnot equalgreater thanless thanoperator
๐Ÿ
Is your age greater than 10? Is it less than 15? Is it exactly 12? Write down the answers as True or False.
๐Ÿค“

Fun Fact!

In Python, == checks if two things are equal, but = assigns a value. Mixing them up is the #1 beginner mistake!

โš–๏ธ Comparing Values

Comparison operators let Python compare two values. The result is always a boolean:Trueor False.

๐Ÿ”ง Comparison Operators

==
Equal to
5 == 5 โ†’ True
!=
Not equal
5 != 3 โ†’ True
>
Greater than
10 > 5 โ†’ True
<
Less than
3 < 7 โ†’ True
>=
Greater or equal
5 >= 5 โ†’ True
<=
Less or equal
3 <= 5 โ†’ True
๐ŸTry comparison operators
main.py
โ— Output
Click โ–ถ Run to see the output!

โš ๏ธ == vs =

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 5

๐ŸŽฏ Comparisons in if Statements

Comparisons are most useful inside if statements!

๐ŸComparisons + if statements
main.py
โ— Output
Click โ–ถ Run to see the output!
๐Ÿ
Try this: write a program that checks if a variable 'age' is greater than or equal to 13. Print 'Teenager!' if it is.
๐Ÿง 

Quick Quiz!

+10 XP

What is the difference between = and == in Python?

๐Ÿ
Finished the lesson? Click the button to mark it complete and earn XP!