Python Not Equal Tutorial

Introduction In this article, we are going to cover the “not equal” operator (!=) in Python. The != operator, which stands for “not equal to,” is crucial for checking if two values or variables are different. It returns True if the values on either side of the != operator are…

Want to improve your IT skillset? Start with a free account and get access to our IT labs!

Table of Contents

    Add a header to begin generating the table of contents

    Related Courses

    Introduction

    In this article, we are going to cover the “not equal” operator (!=) in Python. The != operator, which stands for “not equal to,” is crucial for checking if two values or variables are different. It returns True if the values on either side of the != operator are not equal and False if they are equal.

    We’ll explain how the != operator works, compare it to the equal operator (==), and provide practical examples to enhance your Python coding skills.

    In short, you can use the syntax below use the Python Not Equal operator:

    if 1 != 5:
      print("True")

    Interactive Python Fiddle

    Below is an interactive Python that you can use to test your Python code.

    Python Not Equal Operator Syntax

    The “not equal” operator in Python, denoted by != in Python 2 and Python 3, is a powerful tool for comparing two values or variables to see if they are different.

    This operator inverts the result of an equality check, providing a straightforward way to determine inequality.

    If you want to improve your Python skills, then consider creating a free account and completing our free Python course:

    Free Course: Python 3 for Beginners

    Python is one of the most used programming languages in the world. Both the popularity and the deman…

    50 Lessons
    1 Quizzes
    1 Labs
    5 Hr

    Example 1: Comparing Numbers with the Not Equal operator

    Let’s start with a simple example:

    print(5 != 3)  # Outputs: True (because 5 is not equal to 3)
    print(5 != 5)  # Outputs: False (because 5 is equal to 5)

    In contrast, the equal operator == checks if two values are the same:

    print(5 == 3)  # Outputs: False (because 5 is not equal to 3)
    print(5 == 5)  # Outputs: True (because 5 is equal to 5)

    These operators are essential for controlling the flow of your program, especially in conditional statements and loops. For instance, using != in a condition:

    Example 2: Not Equal Operator with Variables

    a = 5
    b = 3
    if a != b:
        print("a and b are not equal")

    And using == in a condition:

    a = 5
    b = 5
    if a == b:
        print("a and b are equal")

    The != operator is particularly useful when you need to execute code only when two values are different. By effectively leveraging != and ==, you can write more precise and efficient Python code. Remember, the exclamation mark in != is what inverts the result, providing a simple yet powerful way to handle inequality checks in your programs.

    Example 3: Comparing Lists with the Not Equal Operator

    Comparing lists in Python using the “not equal” operator (!=) is straightforward. This operator can help determine if two lists are different. Here’s an example:

    list1 = [1, 2, 3]
    list2 = [1, 2, 4]
    
    # Check if the two lists are not equal
    if list1 != list2:
        print("The lists are not equal.")
    else:
        print("The lists are equal.")

    In this example, list1 and list2 are compared using the != operator. Since list1 contains [1, 2, 3] and list2 contains [1, 2, 4], the != operator evaluates to True, and the output will be:

    The lists are not equal.

    If the lists were the same:

    list1 = [1, 2, 3]
    list2 = [1, 2, 3]
    
    # Check if the two lists are not equal
    if list1 != list2:
        print("The lists are not equal.")
    else:
        print("The lists are equal.")

    In this case, the != operator evaluates to False, and the output will be:

    The lists are equal.

    This demonstrates how the != operator can be used to compare lists and determine if they are different.

    Example 4: Comparing Different Data Types with the Not Equal Operator

    Let’s examine how the != operator behaves when comparing different data types: an integer, a float, and a string.

    Comparing Integer and Float

    When you compare an integer with a float, Python automatically handles the type conversion. Here’s an example:

    A = 2       # Integer
    B = 2.0     # Float
    
    # Check if the integer and float are not equal
    if A != B:
        print("A and B are not equal.")
    else:
        print("A and B are equal.")

    In this case, A is 2 and B is 2.0. Python considers these values equal because it converts the integer to a float before comparing. The output will be:

    A and B are equal.

    Comparing Integer and String

    Comparing an integer and a string is different because Python does not convert types for this comparison. Let’s see how it works:

    A = 2       # Integer
    C = "2"     # String
    
    # Check if the integer and string are not equal
    if A != C:
        print("A and C are not equal.")
    else:
        print("A and C are equal.")

    Here, A is 2 (integer) and C is "2" (string). Since they are different types, they are not equal. The output will be:

    A and C are not equal.

    Comparing Float and String

    Similarly, comparing a float and a string involves strict type checking. Here’s an example:

    B = 2.0     # Float
    C = "2"     # String
    
    # Check if the float and string are not equal
    if B != C:
        print("B and C are not equal.")
    else:
        print("B and C are equal.")

    In this example, B is 2.0 (float) and C is "2" (string). Since they are of different types, they are not equal. The output will be:

    B and C are not equal.

    Conclusion

    In this article, we’ve delved into the “not equal” operator (!=) in Python, understanding how it operates across different data types such as integers, floats, and strings. We’ve seen how Python manages type conversion between numeric types and enforces strict type checking when it comes to strings. By mastering the != and == operators, you can write more precise and efficient code, ensuring accurate comparisons and better control over your programs.

    Take our FREE Python course!

    Ready to take your Python skills to the next level? Join us at ServerAcademy! Create a free account and enroll in our free Python course. Gain access to comprehensive lessons, practical examples, and expert guidance to enhance your programming knowledge.

    CREATE YOUR FREE ACCOUNT & GET OUR

    FREE IT LABS

    profile avatar

    Paul Hill

    Paul Hill is the founder of ServerAcademy.com and IT instructor to over 500,000 students online!