问题描述:

I declared two variables as follows (one of which is the reverse of the other):

a = "test"

b = "tset" # reverse of a

I then ran this into the interpretor which returned the below value:

>>> b[::-1]

'test' # value returned

As you can see, it reversed the string which made it exactly like the first variable a.

However, when I execute this statement, the results are not quite the same:

a is b[::-1]

False # returns false after executing above statement

a is "test", so is b[::-1]. So why is it that the condition does not evaluate to True?

网友答案:

To check for "values" equality you should use the operator ==:

>>> a == b[::-1]
True

So why is it that the condition does not evaluate to True?

The is keyword is used to compare objects identity; in other words, if the objects are the same.

相关阅读:
Top