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
Related
This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Is i = i + n truly the same as i += n? [duplicate]
(3 answers)
Closed 2 years ago.
So this is happening:
a = [1,2]
b = a
b += [3]
print(a)
[1, 2, 3]
It's extremely confusing and it's screwing up my project's test suite, which takes a list of strings and tests that it gets modified in the correct ways by each function.
Did I just find a bug in Python, or is there some convoluted reason for this misbehavior?
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:
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.
This question already has answers here:
Compute list difference [duplicate]
(17 answers)
Closed 7 years ago.
I have two lists and I want to print the difference between them (if there is 1 difference, it should print "1". How can I fix that?
So what I have is:
a= ["1","2","3"]
b= ["1","4","5"]
The answer should be 2.
It depends on what do you mean by difference. If they are equal in length and you want to find out the difference, do:
c = [i for i in a if i not in b]
print len(c)
Use set:
print len(set(L1) - set(L2))
Test:
>>> L1 = [1,2,5]
>>> L2 = [8,1]
>>> len(set(L1) - set(L2))
2
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.