Not equal sign python

Python Not Equal

Python Not Equal is a Comparison Operator used to check if two values are not equal.

The symbol used for Python Not Equal operator is !=. There should be no separator between exclamatory symbol and equal symbol. ! is referred to as not.

Not Equal Operator is mostly used in boolean expressions of conditional statements like If, If-Else, Elif, While, etc.

Syntax

Following is the syntax of Python Not Equal comparison operator.

result = operand_1 != operand_2

where operand_1 and operand_2 are values of any datatype.

Not Equal operator returns a boolean value. The operator returns True if operand_1 and operand_2 are not equal values, else it returns False.

Example 1: Not Equal Comparison Operator

In this example, we shall take two integers, and check if they are not equal using !=.

Читайте также:  background-position

Python Program

a = 10 b = 12 c = 12 print(a != b) print(b != c)

a and b are not equal and therefore a != b returned True.

a and c are equal and therefore a != b returned False.

Example 2: Not Equal Operator with IF Statement

We already know that not equal operator returns a boolean value. Therefore, this can be used in conditions of decision making statements.

In the following example, we shall use not equal operator in IF statement condition.

Python Program

a = 11 if a%2 != 0 : print(a, "is not even number.")

a%2 != 0 returns True for a=11. And therefore, if block is executed.

Example 3: Not Equal Operator with Strings

In this example, we shall use Not Equal operator to check if two strings are not equal.

Python Program

a = "python" b = "javascript" if a != b : print(a, 'and', b, 'are different.')

Clearly, the two strings are not equal and the result of a != b is True. So, Python executes the if block code.

Example 4: Not Equal Operator in While Condition

You can use not equal operator in while loop condition.

Python Program

a = 4 while a != 0 : print("hello") a -= 1

Example 5: Not Equal Operator in Compound Condition

not equal operator can be used to combine simple conditions and form compound conditions or boolean expressions.

Python Program

a = 4 b = 5 if (a == 1) != (b == 5): print('Hello')

(a == 1) and (b == 5) two simple conditions and we have use not equal operator to join them and form a compound condition.

Summary

In this tutorial of Python Examples, we learned what Python Not Equal Comparison Operator is, how to use it to find if two values are not equal, with the help of well detailed example programs.

Источник

Python Not Equal – Does Not Equal Operator Tutorial

Ihechikara Vincent Abba

Ihechikara Vincent Abba

Python Not Equal – Does Not Equal Operator Tutorial

When you’re learning the basics of most programming languages, you are bound to come across operators.

In this tutorial, we will talk about the not equal operator in Python and also see a few examples of how it works.

Operators and Operands in Python

Before talking about the not equal operator, let’s understand what operators and operands are in general.

Operators are symbols that denote a certain type of action or process. They carry out specific operations on certain values or variables. These values or variables are known as the operands of the the operator so the operator performs its operation on them and returns a value.

Here are a few examples of operators and how they interact with operands:

Addition operator ( + )

a = 10 b = 10 print(a + b) # returns 20 

The operator here is the + symbol which adds the value of a and b which are the operands.

Multiplication operator ( * )

c = 10 d = 10 print(a * b) # returns 100

Similar to the last example, * is the operator while c and d are the operands.

Not equal operator ( != )

firstNumber = 10 secondNumber = 20 print(firstNumber != secondNumber) # returns True

Again, the operator is the != symbol and the operands are firstNumber and secondNumber .

There are many other operators in Python which are divided into groups but in this tutorial we will be focusing on the not equal operator ( != ).

Not Equal Operator in Python

The not equal operator is a relational or comparison operator that compares two or more values (operands). It returns either true or false depending on the result of the operation.

If the values compared are equal, then a value of true is returned. If the values compared are not equal, then a value of false is returned.

!= is the symbol we use for the not equal operator.

Let’s see a few examples of how it works.

How to compare numeric values using the != operator in Python

Here, we will define two variables and then compare their values.

a = 600 b = 300 print(a != b) # True 

As expected, the above operation returns true because the value of a is not equal to the value of b . If you still find this hard to grasp, then I will represent the code above using plain English to rewrite each line below:

a is equal to 600 b is equal to 300 print(the value of a does not equal the value of b) # True, the value of a is not equal to the value of b

That should probably simplify it.

Next, we will compare more than two values.

a = 600 b = 300 c = 300 print(a != b & c) # True

If you were expecting a value of false then you were probably trying to add some of the values during the comparison.

To make this simpler to understand, the operator is only going look at the values of each operand and then compare all of them without adding one operand to the other.

Imagine a , b and c as triplets and each baby’s face was represented by a number. Now the != operator is saying, «I have made my observations and concluded that the three babies are not identical facially» and that is completely True .

When all the operands are the same and the != is used, then the value returned will be false. That is:

a = 600 b = 600 c = 600 print(a != b & c) # False

Here, the triplets all have the same face but != is saying, «All the babies do not have the same face» and that is false because their faces, represented by numbers, are the same – 600.

How to compare lists in Python using the != operator

In the previous section, we compared the values of numbers. In this section, we will be comparing lists. Lists are used to store more than one item in a single variable.

a = [2, 3] b = [2, 3] print(a != b) # False

Just like we saw in the previous section, the value is False because the two lists are the same. It would be True if both operands were not the same.

To further grasp the idea of True or False being returned when using the != operator, you should always have in mind that the value will be True if the operands are not the same and False if the operands are the same.

The != operator can also be used to compare Strings, Dictionaries, Tuples and Sets.

How to use an if statement with the != operator in Python

In some cases, you might prefer to carry out a certain command only after evaluating two variables. Consider the example below:

a = 21 b = 10 if ( a != b ): print ("True. a is not equal to b") else: print ("False. a is equal to b") # True. a is not equal to b

The if statement checks whether the values of the operands are not the same and then prints a message based on the value returned.

This is a very basic example. As you advance as a Python developer, you’ll find yourself crafting more complex (but not necessarily hard) logic to execute various commands.

Conclusion

This article served as an introduction to using the not equal ( != ) operator in Python and highlighted a few examples to help you understand its application.

If you are a beginner interested in learning Python, freeCodeCamp has a Scientific Computing with Python certificate which is a good place to start.

Источник

Python not equal operator

Python not equal operator

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Python not equal operator returns True if two variables are of same type and have different values, if the values are same then it returns False . Python is dynamic and strongly typed language, so if the two variables have the same values but they are of different type, then not equal operator will return True .

Python not equal operators

Operator Description
!= Not Equal operator, works in both Python 2 and Python 3.
<> Not equal operator in Python 2, deprecated in Python 3.

Python 2 Example

$ python2.7 Python 2.7.10 (default, Aug 17 2018, 19:45:58) [GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 10 <> 20 True >>> 10 <> 10 False >>> 10 != 20 True >>> 10 != 10 False >>> '10' != 10 True >>> 

Python 2 Not Equal Operators

Python 3 Example

$ python3.7 Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 10 <> 20 File "", line 1 10 <> 20 ^ SyntaxError: invalid syntax >>> 10 != 20 True >>> 10 != 10 False >>> '10' != 10 True >>> 

Python Not Equal Operator

We can use Python not equal operator with f-strings too if you are using Python 3.6 or higher version.

x = 10 y = 10 z = 20 print(f'x is not equal to y = ') flag = x != z print(f'x is not equal to z = ') # python is strongly typed language s = '10' print(f'x is not equal to s = ') 
x is not equal to y = False x is not equal to z = True x is not equal to s = True 

Python not equal with custom object

When we use not equal operator, it calls __ne__(self, other) function. So we can define our custom implementation for an object and alter the natural output. Let’s say we have Data class with fields — id and record. When we are using the not-equal operator, we just want to compare it for record value. We can achieve this by implementing our own __ne__() function.

class Data: record = '' def __init__(self, i, s): self.id = i self.record = s def __ne__(self, other): # return true if different types if type(other) != type(self): return True if self.record != other.record: return True else: return False d1 = Data(1, 'Java') d2 = Data(2, 'Java') d3 = Data(3, 'Python') print(d1 != d2) print(d2 != d3) 

Notice that d1 and d2 record values are same but “id” is different. If we remove __ne__() function, then the output will be like this:

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. Learn more about us

Источник

Оцените статью