Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python Comparison Operators (Relational Operators)

Comparison operators, also known as relational operators, in Python are used to compare two values and determine their relationship. They return a boolean value, either True or False, depending on whether the comparison is true or false.

In this tutorial, we will explore the different comparison operators in Python and how to use them.

  • Equal to (==):

The equal to operator checks if two values are equal. If they are equal, it returns True; otherwise, it returns False.

a = 10
b = 5
print(a == b)  # Output: False
  • Not equal to (!=):

The not equal to operator checks if two values are not equal. If they are not equal, it returns True; otherwise, it returns False.

a = 10
b = 5
print(a != b)  # Output: True
  • Greater than (>):

The greater than operator checks if the left operand is greater than the right operand. If it is, it returns True; otherwise, it returns False.

a = 10
b = 5
print(a > b)  # Output: True
  • Less than (<):

The less than operator checks if the left operand is less than the right operand. If it is, it returns True; otherwise, it returns False.

a = 10
b = 5
print(a < b)  # Output: False
  • Greater than or equal to (>=):

The greater than or equal to operator checks if the left operand is greater than or equal to the right operand. If it is, it returns True; otherwise, it returns False.

a = 10
b = 5
print(a >= b)  # Output: True
  • Less than or equal to (<=):

The less than or equal to operator checks if the left operand is less than or equal to the right operand. If it is, it returns True; otherwise, it returns False.

a = 10
b = 5
print(a <= b)  # Output: False
  • Chaining comparison operators:

You can chain multiple comparison operators in a single expression. Python will evaluate each comparison and return True only if all comparisons are true.

a = 10
b = 5
c = 15
print(a > b and a < c)  # Output: True
print(b < a < c)        # Equivalent to the above expression

In conclusion, comparison operators in Python are used to compare two values and determine their relationship. They return a boolean value, either True or False, based on the result of the comparison. These operators are essential for making decisions and controlling the flow of your program.

  1. Using == for equality comparison in Python:

    • Description: The == operator is used for equality comparison. It checks if the values on both sides are equal.
    • Code:
    x = 5
    y = 5
    
    result = x == y  # Result: True
    
  2. Inequality comparison with != in Python:

    • Description: The != operator checks if the values on both sides are not equal.
    • Code:
    a = 10
    b = 20
    
    result = a != b  # Result: True
    
  3. Less than (<) and greater than (>) operators in Python:

    • Description: The < operator checks if the left operand is less than the right, and > checks if the left operand is greater than the right.
    • Code:
    x = 15
    y = 20
    
    less_than_result = x < y  # Result: True
    greater_than_result = y > x  # Result: True
    
  4. Less than or equal to (<=) and greater than or equal to (>=) in Python:

    • Description: The <= operator checks if the left operand is less than or equal to the right, and >= checks if the left operand is greater than or equal to the right.
    • Code:
    a = 30
    b = 30
    
    less_than_equal_result = a <= b  # Result: True
    greater_than_equal_result = b >= a  # Result: True
    
  5. Chaining multiple comparison operators in Python:

    • Description: Multiple comparison operators can be chained together to create compound conditions.
    • Code:
    x = 10
    y = 20
    z = 30
    
    chained_result = x < y < z  # Result: True
    
  6. Negating comparisons with not in Python:

    • Description: The not keyword is used to negate the result of a comparison.
    • Code:
    x = 5
    y = 10
    
    not_result = not (x == y)  # Result: True (since x is not equal to y)
    
  7. Comparing strings and other data types in Python:

    • Description: Comparison operators can be used with strings and other data types to check for equality or order.
    • Code:
    string1 = "apple"
    string2 = "banana"
    
    string_comparison = string1 < string2  # Result: True (based on lexicographic order)
    
  8. Boolean results and truthy/falsy values in Python comparisons:

    • Description: Comparison operators return boolean values (True or False). In Python, values are considered truthy or falsy, and these properties can affect the outcome of comparisons.
    • Code:
    x = 0
    y = 10
    
    truthy_result = x < y  # Result: True (0 is falsy, so x < y is True)