Anyone Can Code.
Logical operators are used the way logic gates are used. In Pyton, we have tree logical
not returns the opposite Boolean value of a statement. For example,
True would return False. We can also integrate it with the conditional operators we
not 2 > 3 would return
True since 2 is less than 3, so
False and hence the opposite of that is
True when one or more conditions are
3 > 2 or 2 < 4 returns
3 > 2 is
Truewhen both conditions are
3 < 4 and 1==1returns
3 < 4 and 1 !=1returns
Did you know that you can use logical operators with sequences as well? It's true, logical
and can be used with sequences. If the sequence
is empty, it is treated as
False whereas if it is having more than one element, it
True. Note, however, that if you try
"" == False it will return
False because the value of
"" is not actually
the interpreter interprets it that way while using sequences with logical operators.
If we take the code
"" or "a", we will get the output as
Well, the interpreter evaluates
False, so it moves on to the
next element which is
"a". It sees that
True and then the evaluation
ends and we get
"a" as output.
That's it for this tutorial! Click Next to access the next tutorial!