Equality Operators Python

Equality Operators

The equality operators in python are used to compare the values on both sides and return true if both the values are equal or false if both the values are unequal.

  • It is very commonly used in the integer type to check for equality.

Example:>>>100==10
Output:False

In the example above we have two numbers 100 and 10. Since hundred is not equal to 10 the output is false.

Example:>>>100!=10
Output:True

Similar to the earlier example we use the not equal to sign in this one. Here the relation becomes true since hundred is not equal to 10. Hence the output is true.

  • We can also use the equality operator in bool and int type as well.

Example:>>>100==True
Output:False

In the above example we have an integer with value hundred and boolean type value true. This gives an output as false since the value of true is one is 1 and one is not equal to hundred.

  • It can come handy while comparing two Boolean operators at once. If both the values are same then the output will be true otherwise false.

Example:>>>False==False
Output:True

In the above code, we see that false is equal to false therefore the output is true.

  • The equality operator is also used to compare two string literals. If the two stranger trials are equal in every perspective then the output is true otherwise False.

Example:>>>’mew==‘mew’
Output:True

Here the first string is exactly the same as the second string so the output is true.


Equality Operators with String:

  • We can also compare a string to an integer type using the equality operator.

Example:>>>’mew’==10
Output:False

  • The chaining concept is also applicable to the Equality operators. The output would be true if the condition for all the operands satisfies. Even if one of the conditions in the chain is not true then the output would be false.

Example:>>>1+1==2==2
Output:True

In the example above all the three values are equal that is 1+1 equals 2 and two equals to 2 thus the output is true.

Example:>>>1==2==3==4
Output:False

In this example, all the four values are different therefore the output is false.

  • The equality datatype can also be used along with complex data type.

 Example:>>> (20+3j) == (20+3j)

Output:True

Here in this example the two complex numbers i.e. 20+3j are equal to each other hence the output is true.

In Python we can use the equality data type with almost every other data type. If both are of different types then it returns false.

Example:>>> (20+3j) == ‘mew’
Output:False

In the above example we have a complex number compared to a string literal and since these are two different datatypes the answer is false.


Difference between = and ==

The = sign is used to assign values to the variable whereas the == sign is used while comparing.

Whenever we compare two numbers having different datatypes the lower the number with the lower datatype gets converted to a higher higher

Example:>>>20 == 20.0
Output:True

In the above example, the first number that is 20 is of int type it as the second number is of float datatype. The number has a lower data type that is 20 is converted to float.

Leave a Comment

Your email address will not be published. Required fields are marked *