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.
0 comments:
Post a Comment