print"\n~~~~~~~ 如何判断python对象的内容和对象的内存地址 ~~~~~~~~~"
t1 = (1,2,3,4)
t2 = (1,2,3,4)
print id(t1);printid(t2)
print t1 == t2 # ==判断内容是否相同
print t1 is t2 #使用is判断是否同一个对象(内存地址)
print "=======比较大小======="
# Return negative if x<y, zero if x==y, positive ifx>y
print cmp(t2[2],t2[1]) #结果是一个正数,是因为前者大于后者. 比较元祖(1,"ok"), (2,"good")时,只比较元祖的首个值
print cmp(t2[2],t2[0])
print "1 < 2吗:\t",True if cmp(1,2)<0 else False
print "\n python中使用if-else语句实现类似java中的三元运算符"
# 三元运算符Reslut ifA-clause else B-clause
print "1 < 2吗 : \t",True if cmp(1,2) <0 else False
# ----------------------
程序运行结果:
~~~~~~~如何判断python对象的内容和对象的内存地址~~~~~~~~~
5133728
5134016
True
False
=======比较大小=======
1
1
1 < 2 吗: True
python中使用if-else语句实现类似java中的三元运算符
1 < 2 吗: True