+ 3

Why the result of print("a" or "b") is "a" and the result of print("a" and "b") is "b"?

https://code.sololearn.com/c9axtWL4o0F4/?ref=app

29th May 2018, 11:02 AM
Andrei
3 Antworten
+ 1
It happens because of the way the 'or' and 'and' works: or → return first condition if it is True else return the second and → return first condition if it is False else return second
29th May 2018, 12:00 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 1
Well, a very good question, Input1 If you print 'a' or 'b', then this means as character 'a' is equivalent to true for the computer and in case of 'or' the computer knows that anything (True or False) may be the Boolean value of the number after 'or'(b in this case), it will always be true due to the property of 'or' so the curser doesn't go further and prints the first value(thats 'a' in this case). So, a is the output. Input2 As here we have 'and' in between, this means both first value and second value should be True.So, even after checking the first value('a' on this case), it will go the second statement to check why it is also true and then will print the second value('b' in this case. In short, if there is an 'or' then it will print the first statement and if it is 'and' then it will print second statement unless there is a zero anywhere, as then it will return 0 as output(0 is equivalent to False) in case of and and will return the other statement(other than zero)
29th May 2018, 12:21 PM
Pulkit Kamboj
Pulkit Kamboj - avatar