Difference between ‘is’ and ‘==’ operators in Python

The ‘is’ and ‘==’ operators are using to compare the variables and we can know more about these two operators in this tutorial.

‘is’ and ‘==’ operators in Python:

‘is’ operator in Python:

Syntax: variable1 is variable2

  • This is one of the identity operators in Python.
  • It compares the two variables and will give “true” if the two variables are pointing to the same object otherwise it will give false.

Example:

a=10.5
f=type(a) is float
k=type(a) is int
print(f)
print(k)
Output: 

true

False

Explanation: From the above example we assigned the float value to the variable ‘a’.

Then we checked the data type of ‘a’ and compared with float and assigned to variable’f’.

So the output came as true when we print the ‘f’ value.

And output came as false when we print the ‘k’ value because we compared the ‘a’ value with ‘int’.

‘==’ operator in Python:

Syntax: variable1 == variable2

  • This is one of the comparison operators in Python.
  • It compares the two values of the variables and will give ‘true’  if two variable values are the same. otherwise, it will give false.

Example:

a=10
b=10
c=5
k=(a == b)
f=(b == c)
print(k)
print(f)
Output:

true

False

Explanation: From the above example we assigned some values to each variable a,b,c.

And we compared ‘a’ and ‘b’ and then ‘b’ and ‘c’.

When we compared ‘a’ and ‘b’ the output came as ‘true’ because the values present in both variables are equal.

But when compared ‘b’ and ‘c’ the output came as ‘false’ because the values present in both variables are not equal.

Observation:

If we observe that the above two operators are similar to each other,

But the only difference is ‘==’ operator compares only the contents which are present in the variables,

But ‘is’ operator used to compare that the two variables are pointing to the same object or not.

And if you want to know about yield keyword in Python you have to go through this link yield keyword in Python.

Leave a Reply