For all built-in Python objects (like strings, lists, dicts, functions, etc.), if x is y, then x==y is also True.
Not always. NaN is a counterexample. But usually, identity (is) implies equality (==). The converse is not true: Two distinct objects can have the same value.
Also, is it generally considered better to just use '==' by default, even when comparing int or Boolean values?
You use == when comparing values and is when comparing identities.
When comparing ints (or immutable types in general), you pretty much always want the former. There's an optimization that allows small integers to be compared with is, but don't rely on it.
For boolean values, you shouldn't be doing comparisons at all. Instead of:
if x == True:
# do something
write:
if x:
# do something
For comparing against None, is None is preferred over == None.
I've always liked to use 'is' because I find it more aesthetically pleasing and pythonic (which is how I fell into this trap...), but I wonder if it's intended to just be reserved for when you care about finding two objects with the same id.
Yes, that's exactly what it's for.
Answer from dan04 on Stack OverflowFor all built-in Python objects (like strings, lists, dicts, functions, etc.), if x is y, then x==y is also True.
Not always. NaN is a counterexample. But usually, identity (is) implies equality (==). The converse is not true: Two distinct objects can have the same value.
Also, is it generally considered better to just use '==' by default, even when comparing int or Boolean values?
You use == when comparing values and is when comparing identities.
When comparing ints (or immutable types in general), you pretty much always want the former. There's an optimization that allows small integers to be compared with is, but don't rely on it.
For boolean values, you shouldn't be doing comparisons at all. Instead of:
if x == True:
# do something
write:
if x:
# do something
For comparing against None, is None is preferred over == None.
I've always liked to use 'is' because I find it more aesthetically pleasing and pythonic (which is how I fell into this trap...), but I wonder if it's intended to just be reserved for when you care about finding two objects with the same id.
Yes, that's exactly what it's for.
I would like to show a little example on how is and == are involved in immutable types. Try that:
a = 19998989890
b = 19998989889 +1
>>> a is b
False
>>> a == b
True
is compares two objects in memory, == compares their values. For example, you can see that small integers are cached by Python:
c = 1
b = 1
>>> b is c
True
You should use == when comparing values and is when comparing identities. (Also, from an English point of view, "equals" is different from "is".)
Videos
I thought that Python was different from other OOP languages like Java and C++ in that it implements equality of strings by comparing their value. But I've been reading that Python actually compares strings by their address, so the == shouldn't be used for value comparison. But from my own testing, == seems to work just fine for value comparison...
So does == compare value or address (or some other property of objects, like ID or something) of two objects?
x = "45"
z ="100"
if x > y.....I understand you can type cast with int() but I'm trying not to use it or eval().
when I compare two strings together that contains a substring it returns true. Is there a function or a way to check if they are identical?
free, freedom > False;
I want:
free, freedom = False
Hi everyone,
I'm stuck on the pset 2 substitution. And I'm not gonna lie i'm struggling big time. So I have divided the problem in small parts.
First, I'm trying to create a function to verify the validity of the key. For doing so i want to compare the user input key to an arbitrary constant named KEY (bb steps) and printing a 1 if the input correspond to the constant or a 0 if the input doesn't correspond.
i'm not using bool for simplicity
Here is my code :
The only result that i have is 0, even when I type the correct key which is hello here. But why? my syntax seems good.
Thank for your responses
P.S. i will gladly use a more efficient way to share my code if you have one