Anyone Can Code.

Logical operators are used the way **logic gates** are used. In Pyton, we have tree logical
operators: `not`

,`or`

,`and`

`not`

returns the opposite Boolean value of a statement. For example, ```
not
True
```

would return False. We can also integrate it with the conditional operators we
learnt about.

`not 2 > 3`

would return `True`

since 2 is less than 3, so ```
2 <
3
```

is `False`

and hence the opposite of that is `True`

.

`or`

returns `True`

when one or more conditions are `True`

.
Therefor `3 > 2 or 2 < 4`

returns `True`

because `3 > 2`

is
`True`

.

`and`

returns `True`

when both conditions are `True`

.
Therefore `3 < 4 and 1==1`

returns `True`

while `3 < 4 and 1 !=1`

returns `False`

Did you know that you can use logical operators with sequences as well? It's true, logical
operators like `or`

and `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
is `True`

. Note, however, that if you try `"" == False`

it will return
`False`

because the value of `""`

is not actually `False`

, but
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 `'a'`

. Why?
Well, the interpreter evaluates `""`

as `False`

, so it moves on to the
next element which is
`"a"`

. It sees that `"a"`

is `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!

This site uses cookies to improve your experience as well as for site metrics. To see more, check out our Privacy Policy

Copyright ©
2021, Q-Programming | Created by Arjun Sodhi, Balpreet Juneja and Sammarth Kumar | Developed and Designed by Sammarth Kumar