Syncing account progress…

Back

Beginner · 10 min

Comparing values

Use comparisons to ask questions with true-or-false answers.

Compare numbers

Comparisons produce the Boolean values True or False. Use > and < for greater and less than, or >= and <= to include equality.

age = 18
print(age >= 18)
print(age < 18)

Output

True
False

Check equality

Use == to compare values and != to check that they differ. A single = assigns a value instead. String comparisons are case-sensitive.

status = "open"
print(status == "open")
print(status != "open")

Output

True
False

Comparing values

  1. Read each comparison as a yes-or-no question.
  2. Compare equality with assignment.

Try it yourself

Code runs on this device. When you are signed in, drafts sync to your account.

Check stock

Keep stock as 5. Display whether it is at least 5, then whether it equals 0, on separate lines.

Practice