This question already has answers here:
Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?
(3 answers)
Closed 2 years ago.
I have this example:
a = [-10,10][True]
b = [-10,10][False]
print(a,b)# "10 -10"
Since bool is a subclass of int that's why True acts like 1 and False like 0
This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 3 years ago.
I know that the operators is and is not test for object identity: x is y is true if and only if x and y are the same object
I initialize two variables i and j with the same value 10, and when I am comparing the id of both variable with is operator it gives me a False
even though the id(i) and id(j) are the same.
I need a clarification, Here is my code
>>> i = 10;
>>> j = 10;
>>> print(10==10);
True
>>> print(id(10)==id(10));
True
>>> print(10 is 10);
True
>>> print(5+5 is 10);
True
>>> print(i == j);
True
>>> print(id(i) == id(j));
True
>>> print(i is j);
True
>>> print(id(i) is id(j)); # Why this statment Evaluate to False this is my quetion?
False
>>> id(i);
140718000878704
>>> id(j);
140718000878704
The is check is True if and only if the id()s are equal--if they are the same object in memory. (The id() builtin gets a unique integer identifier for the object, which is based on its memory address in CPython).
It is improper to use an is check on integers to compare their values. CPython does re-use integer objects in a small range instead of making new ones as an optimization, but this is an implementation detail that you should not rely upon.
For integer objects outside of that range, they may be separate objects in memory, even if they are equal in value.
For example,
>>> x = -6
>>> -6 is x
False
>>> x = 257
>>> 257 is x
False
This question already has answers here:
What's the canonical way to check for type in Python?
(15 answers)
Closed 5 years ago.
Why does this print False in Python 3?
>>> 1 == int
False
Because that isn't at all doing what you think it is. You are comparing the integer value 1 with the type int; naturally they are not equal.
If you want to check whether an object is of a certain type, use isinstance:
isinstance(1, int)
I guess, what you want to use is this:
>>> type(1) is int
True
or
>>> type(1) == int
True
This question already has answers here:
Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?
(3 answers)
Closed 6 years ago.
>>> True + 2
3
>>> False + 2
2
I can understand that somehow, True means 1 and False means 0 . So
does it mean, a Boolean and integer operation always gives an integer?
In python bool is a subclass of int, and therefor satisfies the "is-a" relation, meaning a bool is-a int.
To demonstrate:
issubclass(bool, int)
=> True
isinstance(True, int)
=> True
In practice this means that in any operation which works on an int, the int can be substituted with a bool.
This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 1 year ago.
Is it better to use the "is" operator or the "==" operator to compare two numbers in Python?
Examples:
>>> a = 1
>>> a is 1
True
>>> a == 1
True
>>> a is 0
False
>>> a == 0
False
Use ==.
Sometimes, on some python implementations, by coincidence, integers from -5 to 256 will work with is (in CPython implementations for instance). But don't rely on this or use it in real programs.
Others have answered your question, but I'll go into a little bit more detail:
Python's is compares identity - it asks the question "is this one thing actually the same object as this other thing" (similar to == in Java). So, there are some times when using is makes sense - the most common one being checking for None. Eg, foo is None. But, in general, it isn't what you want.
==, on the other hand, asks the question "is this one thing logically equivalent to this other thing". For example:
>>> [1, 2, 3] == [1, 2, 3]
True
>>> [1, 2, 3] is [1, 2, 3]
False
And this is true because classes can define the method they use to test for equality:
>>> class AlwaysEqual(object):
... def __eq__(self, other):
... return True
...
>>> always_equal = AlwaysEqual()
>>> always_equal == 42
True
>>> always_equal == None
True
But they cannot define the method used for testing identity (ie, they can't override is).
>>> a = 255556
>>> a == 255556
True
>>> a is 255556
False
I think that should answer it ;-)
The reason is that some often-used objects, such as the booleans True and False, all 1-letter strings and short numbers are allocated once by the interpreter, and each variable containing that object refers to it. Other numbers and larger strings are allocated on demand. The 255556 for instance is allocated three times, every time a different object is created. And therefore, according to is, they are not the same.
That will only work for small numbers and I'm guessing it's also implementation-dependent. Python uses the same object instance for small numbers (iirc <256), but this changes for bigger numbers.
>>> a = 2104214124
>>> b = 2104214124
>>> a == b
True
>>> a is b
False
So you should always use == to compare numbers.
== is what you want, "is" just happens to work on your examples.
>>> 2 == 2.0
True
>>> 2 is 2.0
False
Use ==