0

How does print(not("0")) gives a false as output and print(not()) gives a True as output? what does the statement do?

print(not("0"))
print(not())
Manvir
  • 769
  • 1
  • 7
  • 15
  • 4
    `not()` -> `not ()` where `()` is the empty tuple, since empty tuples are falsey, then not falsey is `True`. In the case of `not("0")` -> `not "0"`, since `"0"` is truthy, not truthy is `False` – juanpa.arrivillaga Aug 06 '19 at 22:13
  • 6
    Possible duplicate of [What is Truthy and Falsy? How is it different from True and False?](https://stackoverflow.com/questions/39983695/what-is-truthy-and-falsy-how-is-it-different-from-true-and-false) – G. Anderson Aug 06 '19 at 22:16
  • 4
    Fundamentally, you seem to think that the `not` keyword is a function, which is not the case – juanpa.arrivillaga Aug 06 '19 at 22:19

3 Answers3

2

Not is very similar to the if condition !=. If a value is truthy, then it returns false. If a value is falsy, it returns true. Since most strings are truthy, it returns false, and since a None value is falsy, it returns true so for example print(not(True)) would return false and print(not(False)) would return true

1

This is because when you pass 0 as a string to the not function, it considers it as a True value and thus negating it becomes False. Whereas an empty string is considered as False value and thus negating it becomes True. If you pass 0 as a number instead of a string, it will return you True again.

asanoop24
  • 449
  • 4
  • 13
1

In Python, empty sequences such as (), [], '' and {} all evaluate to False, as well as the interger 0. You can check this by using the bool() function on any of these values.

In your first print, the not operator returns the boolean value that is the opposite of the boolean value of ("0"), which isn't an empty sequence nor 0. In other words, if you call bool(("0")), you will get True in return, and not True returns False.

In your second print, it's exactly the opposite happening. bool(()) is False, therefore not () should be True.


BTW: In your first print example, the value ("0") is not a tuple, but a string. I mention this just in case you were thinking otherwise.

Rafael FT
  • 115
  • 1
  • 7