Comparison Operators

  • Comparison operators in Python are used to compare two values. They return True or False and are mainly used in conditions like if statements and loops to control program flow.
  • A comparison operator is a symbol used to compare two values. It returns True if the comparison is correct, otherwise False.

    Examples: == (equal), != (not equal), > (greater than), < (less than), >= (greater or equal), <= (less or equal).

    Comparison operators in Python are used to compare two values. The result of a comparison is always a Boolean value: True or False.

    Operator

    Description

    Example

    ==

    Equal to

    5 == 5 → True

    !=

    Not equal to

    5 != 3 → True

    >

    Greater than

    5 > 3 → True

    <

    Less than

    5 < 3 → False

    >=

    Greater than or equal to

    5 >= 5 → True

    <=

    Less than or equal to

    3 <= 5 → True

    Comparison operators are very useful in decision-making, loops, and conditional statements in Python.

Comparison Operators Example

Demonstrates the use of all comparison operators with two numbers.

a = 10
b = 5

# Equal to
print("a == b:", a == b)

# Not equal to
print("a != b:", a != b)

# Greater than
print("a > b:", a > b)

# Less than
print("a < b:", a < b)

# Greater than or equal to
print("a >= b:", a >= b)

# Less than or equal to
print("a <= b:", a <= b)