Not Equal

 

What Is the Not Equal Operator?

  • The not equal operator != in Python is used to check if two values are different.

How to Use It:

  • The basic way to use the not equal operator is:

    python
    result = value1 != value2
    
    • value1 and value2 are the values you want to compare.
    • result will be True if the values are not equal, and False if they are equal.

Examples and Exercises:

  1. Not Equal Comparison of Numbers:

    python
    a = 10
    b = 12
    c = 12
    print(a != b)  # Output: True
    print(b != c)  # Output: False
    
  2. Using Not Equal Operator in an IF Statement:

    python
    a = 11
    if a % 2 != 0:
        print(a, "is not an even number.")
    
    • Output: 11 is not an even number.
  3. Not Equal Operator with Strings:

    • You can also compare text.
    python
    a = "python"
    b = "javascript"
    if a != b:
        print(a, 'and', b, 'are different.')
    
    • Output: python and javascript are different.
  4. Not Equal Operator in a While Loop:

    • You can use the not equal operator to control loops.
    python
    a = 4
    while a != 0:
        print("hello")
        a -= 1
    
    • Output:

       
      hello
      hello
      hello
      hello
      
  5. Not Equal Operator in Compound Conditions:

    • Combine conditions using the not equal operator.
    python
    a = 4
    b = 5
    if (a == 1) != (b == 5):
        print('Hello')
    
    • Output: Hello

Summary:

  • The not equal operator != in Python is used to check if two values are different. It works with numbers, text, and more. Try out the examples to see how it works!