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".)
Comparing strings in if statements
Comparing Strings
Does the == operator compare strings by value or address?
Python: How can i compare a User-Input-String in an if-Statement? - Stack Overflow
Videos
I'm writing this through my phone, forgive me if the proper indentation is incorrect, as it displays differently in my screen, and auto correct might mess something up. I'm getting the following error: Name error = name 'yes' is not defined. I dug around, and this usually happens when the variable is not defined, or if the value is pulled before the variable being defined. I don't think the issue is with my logic, but moreso with the syntax. I even tried specifying that it was a string value, as that was a problem once, but it doesn't seem to work either. If I could get any help, I'd appreciate it.
Here's the code that's giving the error:
pizza = str(input("Do you like pizza? "))
If pizza == str(yes): print("User Likes Pizza") elif pizza == no: print("user does not like pizza") else print("Please Give Valid response")
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?
Your condition is always true, because only one of the inequalities can be false at the same time.
So false or true or true => true.
You should use and instead of or.
Even better, you could check whether the input is part of a set:
if Benutzerwahl not in {"Schere", "Stein", "Papier"}:
...
use "and" instead of "or" "or" will be true as long as one of your checks is true