Python Comparison Operators to Know for Intro to Python Programming

Comparison operators in Python help you evaluate relationships between values. They are essential for making decisions in your code, like checking equality or determining which value is larger. Understanding these operators is key to effective programming.

  1. Equal to (==)

    • Compares two values to check if they are the same.
    • Returns True if the values are equal, otherwise returns False.
    • Can be used with numbers, strings, lists, and other data types.
    • Important for conditional statements and loops in programming.
    • Example: 5 == 5 evaluates to True.
  2. Not equal to (!=)

    • Checks if two values are not the same.
    • Returns True if the values are different, otherwise returns False.
    • Useful for filtering data and controlling program flow.
    • Can be applied to all data types, similar to the equal operator.
    • Example: 5 != 3 evaluates to True.
  3. Greater than (>)

    • Compares two values to determine if the first is larger than the second.
    • Returns True if the first value is greater, otherwise returns False.
    • Commonly used in numerical comparisons and sorting algorithms.
    • Can be used with integers, floats, and other comparable types.
    • Example: 10 > 5 evaluates to True.
  4. Less than (<)

    • Checks if the first value is smaller than the second.
    • Returns True if the first value is less, otherwise returns False.
    • Frequently used in loops and conditional statements.
    • Applicable to various data types that support comparison.
    • Example: 3 < 7 evaluates to True.
  5. Greater than or equal to (>=)

    • Combines the greater than and equal to comparisons.
    • Returns True if the first value is greater than or equal to the second.
    • Useful for inclusive comparisons in algorithms and conditions.
    • Can be used with all comparable data types.
    • Example: 5 >= 5 evaluates to True.
  6. Less than or equal to (<=)

    • Combines the less than and equal to comparisons.
    • Returns True if the first value is less than or equal to the second.
    • Important for inclusive conditions in programming logic.
    • Works with all data types that support comparison.
    • Example: 4 <= 6 evaluates to True.
  7. Identity operator (is)

    • Checks if two variables point to the same object in memory.
    • Returns True if both variables refer to the same object, otherwise False.
    • Useful for comparing objects, especially in mutable data types.
    • Distinct from equality; two objects can be equal but not identical.
    • Example: a is b checks if a and b are the same object.
  8. Negated identity operator (is not)

    • Checks if two variables do not point to the same object in memory.
    • Returns True if the variables refer to different objects, otherwise False.
    • Important for ensuring distinct object references in your code.
    • Similar to the identity operator but in the opposite context.
    • Example: a is not b checks if a and b are different objects.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.