This question already has answers here:
Understanding the "is" operator [duplicate]
(11 answers)
Two variables in Python have same id, but not lists or tuples
(5 answers)
Closed 5 years ago.
I understand that the "is" operator checks identity between two objects, but what is this?
a=25
b=25
a is b
True
Why is this true if
a = [1,2,3]
b = [1,2,3]
a is b
False
And from this post, https://www.quora.com/Python-is-operator, it says all immutable objects, like integers from the first example, will have the first example's result, True, but tuples are immutable and this happens:
a = (1,2)
b = (1,2)
a is b
False
Can someone explain please?
Thanks.
Related
This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Why does comparing strings using either '==' or 'is' sometimes produce a different result?
(15 answers)
Closed 3 years ago.
Here is my Python2 script, test.py:
x = sys.argv[1]
y = 'foo'
print(x)
print(y)
print(x is y)
I then call my script with python test.py 'foo'. This prints out:
foo
foo
False
But both x and y appear to be the same value, 'foo'. So why is this equivalence test returning False?
You need to use ==
is will return True if two variables point to the same object, == if the objects referred to by the variables are equal.
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 4 years ago.
image=[[1,1,1],[1,1,0],[1,0,1]]
visited = [[False] * len(image[0])] * len(image)
visited[0][0] = True
print((visited[1][0]))
Shouldn't the above python code print False? Why is the entire column being assigned as True?
The reference to [False] * len(image [0]) is repeated len(image) times, so modifying the first element of visited affects the second (and third).
This question already has answers here:
Concatenating two lists - difference between '+=' and extend()
(11 answers)
Python append() vs. + operator on lists, why do these give different results?
(7 answers)
Closed 6 years ago.
Python lists have a += operator as well as append and extend methods.
If l is a list, is l += ... equivalent to l.append(...), l.extend(...), both, or neither?
In python += on a list is equivalent to extend method on that list.
This question already has answers here:
Understanding the "is" operator [duplicate]
(11 answers)
Closed 8 years ago.
I tried this operation in python interactive mode :
>>> (1*1) is 1
True
>>> (377*35) is 13195
False
>>> 377*35
13195
>>> 377*35 is 377*35
False
>>> 1*1 is 1
True
Could anybody explain why ' (377*35) is 13195 ' is false?
Thanks in advance!
A is B checks that A and B refer to the same object. It does not check whether A equals B numerically.
The reason for the different behaviour in your examples is that ints with small values (typically between -1 and 99
inclusive) are "interned" by the interpreter -- whenever a result has such a value,
an existing short int with the same value is returned.
This explains why is returns True for your examples involving small numbers but not for those involving large numbers.
This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 9 years ago.
This might be a stupid question, but what exactly is the is function, and when would one use it?
From the context, i guess i could infer that it's equivalent to ==; but if that's the case, why have both? The Built-in Functions Reference shows nothing, and help(is) returns a SyntaxError.
is checks if the objects have the same identity. == only checks if they are equal.
>>> L1 = [1,2,3]
>>> L2 = [1,2,3]
>>> L1 is L2
False
>>> L1 == L2
True