Predict the output of following statements:
a=50
b=27
print(a<100 and b<a)
print(a<b and b>a)
print(not(a>100))
print(b<=a and b<100)
Answers
Given statements:
>>> a = 50
>>> b = 27
>>> print(a < 100 and b < a) #output 1
>>> print(a < b and b > a) #output 2
>>> print(not(a > 100)) #output 3
>>> print(b <= a and b < 100) #output 4
Output:
#output 1
True
We've to check if the value stored in 'a' is lesser than 100 or not. Since 'a' (50) is lesser than 100, that part of the statement is true.
We then need to check if the value stored in 'b' is lesser than the value stored in 'a'. Since 'b' (27) is lesser than 'a' (50), it will be true.
When the 'and' operator is used, it will result to True only when both sides of the operator are true [both sides give the same result]. Since both sides of the operator here are true, the output will be True.
#output 2
False
We've to check if the value stored in 'a' is lesser than the value stored in 'b' or not. Since 'a' (50) is not lesser than 'b' (27), it will be false.
We then need to check if the value stoed in 'b' is greater than the value stored in 'a' or not. Since 'b' (27) is not greater than 'a', it will be false.
When the 'and' operator is used, it will result to False only when both sides of the operator are false [both sides give the same result]. Since both sides of the operator here are false, the output will be False.
#output 3
True
We've to check whether the value stored in 'a' is greater than 100 or not. Since 'a' (50) is not greater than 100, it will be false.
When the 'not' operator is used, it will return the opposite of the Boolean result of the relation to be tested.
Since the result of a > 100 is False, the opposite of it will be True. Hence the output will be True.
#output 4
True
We've to check whether the value stored in 'b' is lesser than or equal to the value stored in 'a'. Since 'b' (27) is lesser than 'a' (50), it will be true.
We then need to check if the value stored in 'b' is lesser than 100 or not. Since 'b' (27) is lesser than 100, it will be true.
When the 'and' operator is used, it will result to True only when both sides of the operator are true [both sides give the same result]. Since both sides of the operator here are true, the output will be True.