educative.io

Booleans and logical expressions

Why is it that when it is “False or True” it prints true? would it ever be false and why not?
Same question with “True and False”, why is it always false?


Course: Learn Python 3 from Scratch - Free Interactive Course
Lesson: Logical Operators

Hi @Nikhi_Boggavarapu !!
In the context of logical operators in Python, the ‘or’ operator evaluates to True if at least one of the operands is True, and it evaluates to False only if both operands are False. This is because the ‘or’ operator essentially checks for the presence of at least one “truth” among the operands. Thus, when you have the expression “False or True”, it evaluates to True because one of the operands (the second one, which is True) is indeed True, satisfying the condition for the ‘or’ operator. If both operands were False, then the expression would evaluate to False.

Similarly, the ‘and’ operator evaluates to True only if both operands are True; otherwise, it evaluates to False. Therefore, when you have the expression “True and False”, it evaluates to False because one of the operands (the second one, which is False) is not True. For the ‘and’ operator, both operands must be True for the entire expression to evaluate to True.

Understanding the behavior of these operators is crucial when building logical expressions in programming. The ‘or’ operator essentially checks for the presence of at least one True, while the ‘and’ operator requires both operands to be True for the entire expression to be True.
I hope it helps. Happy Learning :blush: