3️⃣

3.3 Comparison Operators

Comparison Operators

Comparison operators are ways to compare data

  • ">" means "greater than"
  • ">=" means "at least"
  • "<" means "less than"
  • "<=" means "no more than"
  • "==" means "equal to"
  • 💡

    Don't confuse "==" with "=". "=" is for assignment while "==" is for checking equality

  • "!=" means "not equal to"

Example Code

# Comparison Operators

print(5 > 9)  # False
print(10 >= 9)  # True
print(5 < 9)  # True
print(10 <= 9)  # False

print(9 == 9)  # True
print(10 == 9)  # False

print(9 != 9)  # False
print(10 != 9)  # True

View code on GitHub.

Practice

Money

In Python syntax, what would you write to say you have at least $4.50?

Compare X

You start with x = 10. Compare x with another number using a comparison operator such that if you print x (comparison operator) (another number), it prints False.

Even

Write a program that takes an integer as input and displays True if the integer is even. Otherwise, it displays False.

No Greater Than

Create a program that takes a POSITIVE integer as an input and checks if it is NO GREATER than 100. Print True if it is and False if it isn't. YOU MAY NOT USE GREATER THAN or LESS THAN OPERATORS (>, <, >=, or <=). Find a way to do this problem using only the == operator and any math operators you want

Previous Section

Next Section

⚖️

Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.