Given one pair of integers a = 1 and b = 1 when compared with ‘is’ would return True.
Given a second pair of integers x = 576 and y = 576 when compared with ‘is’ might return False.
Why do we get different results with the "is" operator?
is
and is not
are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the memory.
Two variables that are equal does not imply that they are identical.
"is" operator compares the address of the variables where they are stored. It returns True if the operands are identical (refer to the same object).
a and b are stored in the same location. It can be rephrased as that a and b are pointing to the same location where "1" is stored. As a result, the output is True.
x and y are stored in different locations for some reason (related to python internal memory allocation). That's why the output might be False.
"==" operator compares the values stored in the variables.
To compare values stored by the variables, use "==" operator.
To learn more about Python operators, visit this link.
Comments
0 comments
Article is closed for comments.