-1

i was coding a simple view in django when the result is always wrong then i noticed the if statement is triggering when it should not i inspected the code to realize it was correct here is the code i tested

>>> li = [""]
>>> li == True
False
>>> if li:
...     print("li is true")
...
li is true
>>> li == False
False
>>> li == None
False
>>> li == True
False

im so confused?? li == False is false should it not return true?

ahmed mani
  • 182
  • 1
  • 10
  • 2
    The list is not empty, as it contains one item (an empty string, in this case), so as any not empty list, it is truthy (evaluates to `True` in boolean context, as in `if li:`). But it is not *equal* to True, as `True` and your list are completely different things... – Thierry Lathuille Oct 10 '21 at 13:33
  • `if li:`, it returns true if list is not empty, false if it's empty – sittsering Oct 10 '21 at 13:33
  • And it can't be *equal* to `False` either, for the same reason. – Thierry Lathuille Oct 10 '21 at 13:34
  • Why do you think ``li == False`` should return ``True``? I think it is correct the way it is right now. I mean can you explain a bit more about your logic why it should be ``True``? – Karina Oct 10 '21 at 13:35
  • @Karina since a empty sting in python also returns false so a empty string a a list would also be false but i thought if it returns false then it must be equal to false – ahmed mani Oct 10 '21 at 13:38
  • 1
    @ahmed mani, oh okay, it makes sense to think that way. But as Thierry said, a list with an empty string is not the same as an empty list. – Karina Oct 10 '21 at 13:40
  • @ThierryLathuille that makes sense thank you – ahmed mani Oct 10 '21 at 13:41
  • 1
    You may want to read about the ["Truthiness of objects in Python"](https://stackoverflow.com/questions/39983695/what-is-truthy-and-falsy-how-is-it-different-from-true-and-false) – Jan Oct 10 '21 at 13:48

2 Answers2

1

Firstly, you are assigning to variable li a list with one element. Altough, the element is an empty string, it is still an element. Therefore, the list is not empty. If you are not convinced check this len(li); it will be equal to 1. This is also why the if li: statement is True and it actually prints "li is true". Instead try: if []: or if "" and you will see that it won't print.

Now, li==True and li==False are both False because it checks if li is True i.e. li=True and the same goes for False. Also, it should have been li=None so that the statement li==None is True.

tzinie
  • 717
  • 5
  • 9
0

The result an expression (whether True or False), is determined by how it is expressed as a boolean expression. Python differs from strongly typed languages like Java in that any expression (even a variable reference) can be evaluated to a boolean. Refer to Truth Value Testing.

By default, an object is considered True unless its class defines either a bool() method that returns False or a len() method that returns zero, when called with the object.

Here are 3 statements to consider:

1. li = [""]
2. li == True
3. if li: print("li is true")
  • Line 1, assign a non-empty list to variable li
  • Line 2, list instance is not a boolean value so expression is evaluated as False, likewise li == False also evaluates to False.
  • Line 3, if li: ... evaluates same as if bool(li): print... which evaluates to True and prints "li is true".

If evaluate the expression bool(li), the value is True since the list variable is non-empty and len() function returns 1. An empty List len() returns 0 so will return False.

CodeMonkey
  • 22,825
  • 4
  • 35
  • 75