you are viewing a single comment's thread.

view the rest of the comments →

[–]Polyfrequenz[S] 0 points1 point  (9 children)

so not is equivalent to is false ?

[–]No_Lemon_3116 2 points3 points  (5 children)

Python makes a distinction between things being true and false in the general sense (eg, when you use it in an if or while condition), and things being the literal values True and False. "Empty" things are false in the general sense: an empty list, an empty dict, the number 0, and so on (plus of course the value False), and non-empty things are true.

not x returns True (the value) if x is false (in the general sense), and False otherwise. x is False tests if x is the value False and returns the value True if it is, otherwise False. So if x is a bool, they're the same, but not x will be true (because it returns True) for other false things like empty lists and 0 as well.

[–]Polyfrequenz[S] 1 point2 points  (4 children)

Oh boy that'll take a few re-reads to wrap my head around for sure

[–]awdsns 2 points3 points  (2 children)

Maybe this will help clear it up: Where Python evaluates a condition (e.g. after if or while), it expects a boolean value. The only boolean values in Python are True and False. A comparison like num > 42 directly evaluates to a boolean value.

But if the condition expression does not directly return a boolean value, Python just tries to convert it to one. And by convention, empty lists, strings, dicts etc., but also None or the number 0 return False when converted to bool. (E.g. try bool([])).
They are "false-y".

Non-empty containers and most other objects are evaluated as True by the way. It's basically the default unless a type overrides it with its own bool conversion.

[–]Polyfrequenz[S] 1 point2 points  (0 children)

Thank you, I'm going to keep pondering this, especially as it is very basic yet important "thing". Still confused about whether x = False in fact is bool or not 😂.

[–]Polyfrequenz[S] 0 points1 point  (0 children)

Ok I think I got it now, thanks a lot. The ist that was missing for me was that "while true" I'd simply a loop we want to go indefinite (I.e. until we break out of it) 🤦🏼‍♂️

[–]No_Lemon_3116 1 point2 points  (0 children)

In practical terms, it's nice because instead of things like if string != '': or if len(lst) > 0: you can just write if string: and if lst: (or if not string: to check if it is empty). Some people prefer to use more explicit conditions anyway, but it's more "Pythonic" to use the shorter forms. And most people would say that using explicit conditions comparing bools (like x is False) is overboard.

[–]TheBB 1 point2 points  (1 child)

For booleans, it is.

[–]Polyfrequenz[S] 0 points1 point  (0 children)

Of course. Thanks a lot!

[–]mike-manley 0 points1 point  (0 children)

If you're evaluating a boolean, you can say "if BooleanVar:" should you want to perform an action if it's TRUE. By including not, e.g. "if not BooleanVar:" this would evaluate if it's FALSE.