Python中的比较和判断代码实例

xiaoxiao2021-02-28  86

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>yprint cmp(t2[2],t2[1])  #结果是一个正数,是因为前者大于后者比较元祖(1,"ok"), (2,"good")时,只比较元祖的首个值print cmp(t2[2],t2[0])print "1 < 2:\t",True if cmp(1,2)<else Falseprint "\n python中使用if-else语句实现类似java中的三元运算符"# 三元运算符Reslut ifA-clause else B-clauseprint "1 < 2吗 : \t",True if cmp(1,2) <else False

 

 # ----------------------

程序运行结果:

~~~~~~~如何判断python对象的内容和对象的内存地址~~~~~~~~~

5133728

5134016

True

False

=======比较大小=======

1

1

1 < 2 吗:     True

 

python中使用if-else语句实现类似java中的三元运算符

1 < 2 吗:     True

 

转载请注明原文地址: https://www.6miu.com/read-34009.html

最新回复(0)