Python: what's the difference between 'is' and '=='?

I am trying the following code:

x = 'asd'
y = 'asd'
z = input() #write here string 'asd'. For Python 2.x use raw_input()
x == y # True.
x is y # True.
x == z # True.
x is z # False.

Why is there a lie in the last expression?

+4
source share
2 answers

isverifies identity. a is b Trueiff aand bare the same object (both of them are stored in the same memory address).

==checks equality, which is usually determined by the magic method __eq__- i.e. a == bis Trueif a.__eq__(b)- True.

, Python ( , ). input() , , .

+13

is , , . input() , is .

Python , x y .

+2

Source: https://habr.com/ru/post/1524680/


All Articles