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.
Related
This question already has an answer here:
python is operator behaviour with string [duplicate]
(1 answer)
Closed 4 months ago.
I am filtering a list of strings according to the following condition.
list_of_strings = ["foo-bar", "foo", "bar"]
print(list(filter(lambda x: x is not "foo-bar", list_of_strings)))
>>> ["foo-bar", "foo", "bar"] # This is output. But I expected "foo-bar" to be removed
But this is working fine with !=
How is this happening?
The problem is in the filtering condition. Should be:
lambda x: x != "foo-bar"
To compare two strings use != and == (equality test).
is is a test for an identical object.
>>> "A" is "A"
True
This is an efect of an optimisation called interning. If a constant is used several times, the Python interpreter tries to save only one copy of it. You cannot rely on that.
>>> "a".upper() is "A"
False
>>> "a".upper() == "A"
True
This question already has answers here:
How to find list intersection?
(16 answers)
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 2 years ago.
My if statement is returning the wrong values:
for i in range (3325 , 3325+1):
FSBRID = []
for j in range(93, 94):
a = ws1.cell(row=i, column=j)
print(a.value)
if 'SOIL' or 'soil' in a.value:
print('wrong')
The returned values:
TISSUE
wrong
Process finished with exit code 0
The construct
if 'SOIL' or 'soil' in a.value:
is parsed as though it were
if ('SOIL') or ('soil' in a.value):
and non-empty strings are true, making it equivalent to
if True or ('soil' in a.value):
One solution is to split it into two “in” operators, the results of which is combined.
if 'SOIL' in a.value or 'soil' in a.value:
This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
"is" operator behaves unexpectedly with integers
(11 answers)
Closed 3 years ago.
I've been working in python for a while now but just now I've encountered an error that I can't explain.
def b():
return -6
a = b()
if a is -6:
print("Hi")
Whenever I run this I don't get into the if function and nothing gets printed.
but if I make the number bigger than -6 meaning -5, -4 and etc this works properly.
def b():
return -5
a = b()
if a is -5:
print("Hi")
can someone explain to me why is this happening? the == operator work as it should but if is it not.
== checks if the two values are equal, is checks if the two objects are the same object. So when you create an integer object from your function you need to use ==. Using is here is checking if the function object -6 is the reference to another object -6.
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:
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