Showing posts with label Comparison Operators. Show all posts
Showing posts with label Comparison Operators. Show all posts

Friday, March 23, 2012

Comparison Operator II


Hi, In this post I will show you some more ways of comparing.


1. '<' --> comes before?

>>> 'd' < 'a'
False
>>> 'a' < 'd'
True
>>> 'a' < 'D'
False
>>> 'a' < 1
False
>>> 'A' < 1
False
>>> 1 < 'a'
True
>>> 1 < 'A'
True
>>> 1<5
True
>>>

When we use '<' for int type data, it uses the regular maths comparison, but when we use with characters, the sequence is

[Numbers < Uppercase Letters < Lowercase Letters]

>>> 'a' < 'A'
False
>>> 'A' < 'a'
True
>>>

[The opposite is for '>' which means 'comes after', I leave this for you to check on your own]


2. 'and' --> executes the body of the conditional statement, if the all the conditions in a statement are True.

>>> python = 'program'
>>> var1 = 'good'
>>> if python=='program' and var1=='good':
    print "You are Good"

You are Good
>>> if python=='program' and var1=='bad':
    print "You are Good"

   
>>>

Okay so see here since one of the condition was not true hence it dint execute the print statement.


3. 'or' --> executes the body of the conditional statement, if one of the condition or all the condition in a statement is True.

>>> python = 'program'
>>> var1 = 'good'
>>> if python=='program' or var1=='bad':
    print "You are Good"

You are Good
>>> if python=='program' or var1=='good':
    print "You are Good"

   
You are Good
>>>

Here in both case the print statement was executed because one or both the condition was true.

Now lets make a small program.

#program to check the usage of and/or operators
ch = raw_input("Enter Name: ")
age = input("Enter You age: ")
code = raw_input("Enter the VIP Code, If you have(Type No, if you dont have): ")
code1 = code.lower()
if age > 20 and age < 35:
    print "You are allowed to enter in the bar"
elif code1=='vip' or code1=='premiummember':
    print "You are allowed to enter in the bar"
else:
    print "You are not allowed"
raw_input("Press Enter to Exit")


Output:

>>>
Enter Name: Whiskey
Enter You age: 25
Enter the VIP Code, If you have(Type No, if you dont have): No
You are allowed to enter in the bar
Press Enter to Exit
>>>
>>>
Enter Name: Whiskey
Enter You age: 40
Enter the VIP Code, If you have(Type No, if you dont have): VIP
You are allowed to enter in the bar
Press Enter to Exit
>>>
Enter Name: Whiskey
Enter You age: 17
Enter the VIP Code, If you have(Type No, if you dont have): PremiumMember
You are allowed to enter in the bar
Press Enter to Exit
>>>
>>>
Enter Name: Whiskey
Enter You age: 17
Enter the VIP Code, If you have(Type No, if you dont have): no
You are not allowed
Press Enter to Exit
>>>

Okay I hope This example Makes It Clear about the usage of 'and' and 'or'

Hope you liked the post and was informative. Practice this, make your own program, and Dont forget to check my next post.

Thank You.

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!