if is in python not working on negative number [duplicate] - python

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.

Related

if x or y in LIST is always true even if it is not [duplicate]

This question already has answers here:
How to test the membership of multiple values in a list
(12 answers)
Closed 3 years ago.
I have an if statement which always returns as true even if it is not.
I have a global list :
NUMBERS_LIST = []
Numbers get added to it via an API call, that works fine.
When it does the following:
def func():
if 8 or 9 in NUMBER_LIST:
return true
elif 1 or 2 in NUMBER_LIST:
return true
But for some reason, it always returns true on the first if statement, even if NUMBER_LIST = [1]
I debugged my program and can see that NUMBER_LIST does contain 1, it's type is int.
I tried doing int(8), converting both types to str but that did not fix my issue.
When I debugged and step through the program, it doesn't really tell me much, I am using Pycharm.
or does not distribute. What you have written is not equivalent to
if 8 in NUMBER_LIST or 9 in NUMBER_LIST:
which is what you want, but to
if 8 or (9 in NUMBER_LIST):
Since 8 is a truthy value, the containment operation is never evaluated.

Comparing strings from argv [duplicate]

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.

Python is operator [duplicate]

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.

Does python store one value for an int and many references? [duplicate]

This question already has answers here:
"is" operator behaves unexpectedly with integers
(11 answers)
Closed 7 years ago.
I'm wondering why this evaluates to True.
x = 1
if x is 1:
print "Does x and 1 point to the same object?"
print "Does this mean python doesn't store redundant values?"
It doesn't work for this case as I expect.
x = range(10)
y = range(10)
if not x is y:
print "I expect this"
My understanding is that is checks to see if two names are pointing to the same object. Does this imply that python has a mechanism to avoid creating redundant values?
This is an implementation detail of the CPython interpreter, but integers with small values are "interned" -- whenever the result of an expression falls within a certain range, an existing int object with the same value is returned.
You could check the range that gets interned by using the following code:
interned_range = [i for i in range(-1000, 1000) if i+0 is i]
print min(interned_range), max(interned_range)
My CPython inters integers between -5 and 256 inclusive.
Being an implementation detail, this behaviour should generally not be relied upon.

Strange python 'is' operator in arithmetic [duplicate]

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.

Categories

Resources