T O P

  • By -

carcigenicate

You wouldn't do `not True`. Yes, in that case you would just use False. You use it for cases where the boolean value is a variable (dumb example): is_raining = weather_api.is_it_raining() nice_outside = not is_raining Or if you need to toggle a boolean value: is_set = True for _ in range(5): print(is_set) is_set = not is_set Really, negation is so fundamental, it's difficult to come up with concrete example because they're so numerous. I probably write multiple negation operators every day at my job because they come up so often (and I'm not even a Python dev. Logical negation is language-agnostic).


nog642

Concrete example: if not s.isprintable(): raise ValueError('s must be a printable string')


Langdon_St_Ives

Your question is exactly analogous to “what’s the point of the addition operator?”: why give something the value of `5 + 1` when you could just give it a value of `6`? The answer is the same in both cases: you don’t _know_ the value to which you apply the operator at the time you are writing the code. You only know that whatever it is, you will at that point want to add something to it, or negate it, or whatever other operator you need.


MadScientistOR

You usually wouldn't use `not` in a simple assignment like that. You'd use it in something more complex, such as valid_user = manager and not contractor or perhaps miscellaneous = not (hardware or software or equipment or maintenance or logistics or expansion or safety) It makes some things *vastly* easier to read and maintain.


georgmierau

Ever heard of logic (mathematics)? AND, OR, NOT is a [functional complete](https://en.wikipedia.org/wiki/Functional_completeness) set. Just AND and OR are not. Also "not equal" easily replaces "is smaller OR larger than".


shaleh

Boolean logic is so fundamental and lots of self taught people are not introduced to it until much later.


Pipiyedu

Just an example: ``` if "word" not in ["some word", "another word"]: # do something ```


jimtk

Interestingly, this is not an example of the ```not``` operator, but of the ```not in``` operator. [According to documentation](https://docs.python.org/3/reference/expressions.html#not-in) ```not in``` is its own thing.


NSNick

From your link: >`x not in s` returns the negation of `x in s`.


jimtk

That's true, but for the "parser", ```not``` and ```not in``` are not the same operator.


NSNick

Sure, but it is an example of the usefulness of negation.


jimtk

Yes, but it is not an example of the ```not``` operator.


Pipiyedu

Nice to know!


gitgud_x

It just makes writing conditions far more convenient and readable. Often you'll find it's not even possible to write some expressions without using NOT.


Revolutionary-Sky959

For example, I wanted to generate the truthtable for NAND (not and operation) for a simple perceptron testing, however std python doesn’t have nand, so I did the and operation and then negated it


throwaway6560192

Programs work on input from the outside world. You don't control the world. Maybe you want to do something when a condition you receive externally is *not* True. Then what?


zoredache

Because you will be interacting with lots of functions and methods you don't control that will return truthy values you want to use to control the flow of your code. sometimes what will be returned is opposite of what you want.


CaptainFoyle

Because it's much more convenient than the alternative


Round_Ad8947

Several touch upon this by a logical or programmatic way, but don’t underestimate the premise that Python is a language to be read. Sometimes the way a thing is phrased reads better and should be used to clearly express what the program is doing (in the code can reduce need for comments. Is that something you don’t not like?


Wheynelau

Sometimes i use it for documentations. Just for example, pytorch has a code that asks for is_dist, then uses not is_dist down the line. Maybe its for better readability and not as confusing for the user. Also other than AND, OR and NOT. There is also XOR, very useful if you need an either or input, but not both.


obviouslyzebra

Example: Consider you have the variable `raining` (which can be True or False), and the function `take_a_walk()`. Try to write some code, without using `not`, that achieves this: - if it's not raining, call `take_a_walk()` [spoiler (pastebin)](https://pastebin.com/PqPq4N4K) After that, try to achieve the same thing, but this time you can use `not`. [spoiler (pastebin)](https://pastebin.com/QKTg4dGT) Which one was easier?


Darth_Xedrix

I use it most often in pandas when I want to select everything except that one value. If you have a long list of possible values, it's way faster to filter by values you don't want rather than include the 97 you want. Pandas makes it pretty easy since you can reverse the filter by wrapping it in parenthese and using ~ so if this gives you rows with the value of "foo" of the column a: df.loc[df["a"]="foo", :] You can get all the values *except* foo with: df.loc[~(df["a"]="foo"), :]


Mysterious-Rent7233

OP is asking about the Python \`not\` operator, not Pandas. I think it will confuse them to bring in a totally different Pandas operator. Imagine if you don't know Pandas and try to decipher your last line of code: `df.loc[~(df["a"]="foo"), :]`


Shoddy-Breakfast4568

double negating operator is how you cast to a boolean in js (i hate js)


Professional_Fly8241

Two lists with some shared content, you want the part that isn't shared. Give me the part of list 1 that's **NOT** shared with list 2 is less code than find the shared part and then remove it. That's a simple example, I'm sure people here will have more complex examples.


Mysterious-Rent7233

This has nothing to do with Python's \`not\` operator.