Boolean operators

Hi, everyone can anybody elaborate me the usage of ‘not’ operator in boolean expressions. I mean, can we use two ‘not’ operators at a time. what is best about it. I’m waiting here

Using not is quite straightforward. It just inverts the boolean value of the expression to the right so that not True evaluates to False and not False evaluates to True. You can use more than one not operator on a single line though there’d be no benefit of chaining them not not True for example is pointless. Combining them with other comparisons is common, like the following form: if a > 3 and b is not None to create more complex logic statements.

If you’re curious about the order of evaluation then the docs are always a good shout-


ok thank you, plz look here and kindly points out, why it’s not working
if not credits >= 120 and gpa >= 2.0:
return “You do not meet either requirement to graduate!”

I’m finding difficulty here

Please see this topic:

[How-to] Create a topic that everyone will read

if you include the information asked for in this topic, we are much better able to help you. Helping from a small snippet of code would involve a lot of guess work.

ok thannx. I’m gonna try it.

you can add this information (full code, exercise url) as a reply in this topic if you want :slight_smile: Then we can help you further :slight_smile:

thanx, @stetim94. I fixed it now. thanx for your help. I’ll need your guide again soon :smiley: :smiley:

NOT works on any value, not just booleans. The truth value coerces a boolean which NOT then toggles.

not 1  =>  False
not 0  =>  True

Using a double not is the same as bool()

not not 1  =>  True
bool(1)    =>  True

We can also use it in a toggle method…

a = 0
for _ in range(10):
    print (a)
    a = int(not(a))

We’ve seen above how the is keyword can be used in combination, and it is common to see, not in within a membership test.

x not in range(n)

As mentioned by @tgrtim, we can build some pretty sophisticated expressions that incorporate the not operator.

>>> x = []
>>> len(x) is not 0

Not like we would ever see that in code, but it does as promised.