首页 > 其他分享 >"==" versus "is"

"==" versus "is"

时间:2022-10-09 15:03:56浏览次数:32  
标签:versus compare python variable print True

"=="versus "is"


Problem

The snippet below confuseD me a lot:

import operator as op
a = [1,2]
b = [1,2]
print(a == b,a is b)

c = 1
d = 1
print(c == d,c is d)

And the terminal shell prompts:

True False
True True

== : compare the value of each identifier only
is : compare the physical location of each identifier

How about second scenaro? both are true?

Answer:promoting performance is a important issue in python,So python opts for directly referring to the existing variable for a new variable if their values are same.

标签:versus,compare,python,variable,print,True
From: https://www.cnblogs.com/UQ-44636346/p/16772065.html

相关文章