Comparison and judgment code example in Python 2.7

print "\n~~~~~~~  How to judge the content of the python object and the memory address of the object  ~~~~~~~~~"
t1 = ( 1 , 2 , 3 , 4 )
t2 = ( 1 , 2 , 3 , 4 )
print  id (t1); print id (t2)
print  t1 == t2   # == Determine whether the content is the same
print  t1  is  t2 #Use   is to determine whether it is the same object ( memory address )


print  "====== == Compare Sizes ======="
# Return negative if x<y, zero if x==y, positive ifx>y
print  cmp (t2[ 2 ],t2[ 1 ])   #The result is a positive number because the former is greater than the latter Compare the tuple (1 ,"ok"), (2,"good") , only compare the first value of the tuple
print  cmp (t2[ 2 ],t2[ 0 ])
print  " Is 1 < 2 :\t" , True  if  cmp ( 1 , 2 )<else  False


print  "\n Use if-else statement in python to implement ternary operator similar to java "
Ternary operator Reslut ifA-clause else B-clause
print  " Is  1 < 2 : \t" , True  if  cmp ( 1 , 2 ) <else  False

 

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

Program running result:

~~~~~~~How to judge the content of python object and the memory address of the object~~~~~~~~~

5133728

5134016

True

False

======= Compare Sizes =======

1

1

Is 1 < 2: True

 

Using the if-else statement in python to implement a ternary operator similar to that in java

Is 1 < 2: True

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324762300&siteId=291194637