Thursday, March 22, 2012

Comparison Operator


Hi, in this post we will discuss the comparison operator which is used in conditional statements for testing condition.

1. '>'  ---> Checks if something is greater than something

>>> 4 > 5
False
>>> 6>4
True
>>>

2. '<' --> Checks if something is Lower than something

>>> 4 < 9
True
>>> 9 < 4
False
>>>

3. '>=' --> Checks if something is greater than or equal to something.

>>> 6>=6
True
>>> 6>=7
False
>>>

4. '<=' --> Checks if something is lower than or equal to something

>>> 4 <= 4
True
>>> 4 <= 2
False
>>>

5. '==' --> Checks if something is equal to something.

>>> 28 == 28
True
>>> 28 == 45
False
>>>

6. '!=' --> Checks if something is not equal to something.

>>> 28 != 45
True
>>> 28 != 28
False
>>>

7. 'is' --> Checks if a list is exactly equal to another list.

>>> ex1 = [1,2,3]
>>> ex2 = [1,2,3]
>>> ex1 == ex2
True
>>> ex1 is ex2
False
>>>

Its because the only the elements are same, and they are not exactly duplicate.

>>> ex3 = ex4 = [10,20,30]
>>> ex3 == ex4
True
>>> ex3 is ex4
True
>>>

8. 'in' --> Checks if something is in something.

>>> p = 'python'
>>> 'l' in p
False
>>> 'h' in p
True
>>>

Okay, So these are the basic simple things which would be required every now and then in our next posts. Practice them and Make sure that you know when to use what operator. And Dont Forget to Check The next Post.

Thank You!

0 comments:

Post a Comment