在python中会用到对象之间的比较,有 == 和 is函数。
is 比较的是两个实例对象是不是完全相同,它们是不是同一个对象,占用的内存地址是否相同。
== 比较的是两个对象的内容是否相等(值是否相同),即内存地址可以不一样,内容一样就可以了
a=10.0
b=5+5
c=5.0+5
print("\na =",a," b =",b," c =",c)
print("a==b ",a==b)
print("a==c ",a==c)
print("\na is b ",a is b)
print("a is c ",a is c)
独立赋值与连续赋值
#独立赋值
a = 2
b = 2
#连续赋值
c = d = 3
如果两个变量值相等,只要是int\float\tuple\str,那么is的结果就是真,即是同一内存(在文件中运行,如果在交互模式下,每一行就是一个编译环境)
只有int\float\tuple\str才能作为字典的key
如果两个变量独立赋值,值相等,只要是list\dict\set,那么is的结果就是假
print("\nint")
a = 2
b = 2
print("a =",a," b =",b)
print ("a is b ",a is b)
print("\nfloat")
a = 3.2
b = 3.2
print("a =",a," b =",b)
print ("a is b ",a is b)
print("\ntuple")
a = (1,2.3)
b = (1,2.3)
print("a =",a," b =",b)
print ("a is b ",a is b)
print("\nstr")
a = "abcde"
b = "abcde"
print("a =",a," b =",b)
print ("a is b ",a is b)
print("\nlist")
a = [3,4,5]
b = [3,4,5]
print("a =",a," b =",b)
print ("a is b ",a is b)
print("\ndict")
a = {1:100,2:200}
b = {1:100,2:200}
print("a =",a," b =",b)
print ("a is b ",a is b)
print("\nset")
a = {100,200}
b = {100,200}
print("a =",a," b =",b)
print ("a is b ",a is b)