comparison


Introduction
Python Compare the values of the statement and determines whether these are true or not. The determination is predicted on the numeric values. There are number of comparison operators in Python. The sample of these are:
  • x==y,  equal or not
  • x !=y,  equal not
  • x<> y, equal or not
  • x >= y, x is greater or equal
  • x <= y,   x is Smaller or equal
  • x>y, x greater than y
  • x< y,  smaller than x

Example 1
>>> a=21
>>> b=32
>>> c=3
>>> d=66
>>> (a+c) > (b+d)
False

In example 1, the comparative operator returns with the false results after computing the numeric values assigned to each variable.


Example 2
>>> [1,2,3,5] == [1,2,3,4]
 False
 >>> [1,2,3,4,5] is [1,2,3,4,5]
True

In first case, Python returns with false result because two lists are different, while second true.


Example 3
a="Python Lake"
b=input("Enter your ID: ")
if a==b:
print("You're welcome")
else:
print("You're not authorized")
>>> 
Output
Enter your ID: Adil Waseem
You're not authorized

In example 3, Python returns with false returns because the variable a value is different. 

Conclusion
Python has comparative operator for the purpose of evaluating the validity of the values. These return with False or True value or execute the statement according to the object function.



Comments