-4

Can someone please look what is wrong here:

dang=True
if [ dang == False ] :
    print ("False")
    print("Value of dang is %s"%dang)
else :
    print ("True")
    print("Value of dang is %s"%dang)

ding=True
if  ding == False  :
    print ("False")
    print("Value of ding is %s"%ding)
else :
    print ("True")
    print("Value of ding is %s"%ding)   

Below is the result

False
Value of dang is True
True
Value of ding is True
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Abhishek Dave
  • 699
  • 1
  • 8
  • 21
  • 2
    what do you use square brackets for? Plus, do the comparison with `is` instead of == for boolean and None values. http://stackoverflow.com/questions/9494404/use-of-true-false-and-none-as-return-values-in-python-functions – Paco Oct 31 '14 at 17:20
  • 1
    `[ dang == False ]` returns `[False]`. Have a look at this: http://www.tutorialspoint.com/python/python_lists.htm – Paco Oct 31 '14 at 17:21
  • 1
    @Paco, your link seems to disagree with you. "You should never use `is` with basic built-in immutable types like booleans (True, False), numbers, and strings" – Kevin Oct 31 '14 at 17:23
  • 1
    Not necessarily, OP is checking that it returns True or False, which are singletons. It makes it more obvious. The test will fail if it returns 0 or 1 (for example, but works with strings) – Paco Oct 31 '14 at 17:26
  • 7
    The correct way to test the boolean negative of a value is `if not foo:`, not `if [foo == False]:` (which is wrong in about three different ways). – jonrsharpe Oct 31 '14 at 17:29
  • Ok. can you also tell me why this work then `if ( dang == False ) : print ("False") print("Value of dang is %s"%dang)` – Abhishek Dave Oct 31 '14 at 17:32
  • 1
    @jonrsharpe is correct. The most Pythonic way to check is to use the inherent "truthiness" of an object. Instead of `if var == True` you should use `if var:` and `if not var`. In cases where 0 or an empty sequence is acceptable `if var is not None:` can be used. – kylieCatt Oct 31 '14 at 17:33
  • 2
    @DaveA Because putting something in standard parentheses doesn't do anything if it is only one item long without a comma. – anon582847382 Oct 31 '14 at 17:36
  • 1
    @DaveA per the [style guide](http://legacy.python.org/dev/peps/pep-0008/#programming-recommendations): *"Don't compare boolean values to `True` or `False` using `==`. Yes: `if greeting:` No: `if greeting == True:`. Worse: `if greeting is True:`"* Also, the parentheses in your example (although not an outright error like the square brackets were) are redundant, and the whitespace is also non-compliant (specifically, the spaces immediately inside the parentheses and before the colon). – jonrsharpe Oct 31 '14 at 17:36
  • 1
    @DaveA, this is a place where python syntax can be confusing. Parens can be used to group expressions or to create tuples. To remove the amgibuity, python creates tuples when there is a comma in the parens. So, `(1)` creates an int while `(1,)` creates a tuple. Angle brackets always create lists. `(dang == False)` creates a boolean and `(dang == False,)` creates a tuple with 1 boolean item. – tdelaney Oct 31 '14 at 17:44

2 Answers2

4

In the first example, it says it is False because you put the check into a list ([]- so it becomes [False]). Non-empty lists evaluate to boolean truth in Python; so that if statement is always positive as that condition will always exist. To fix it, just remove those brackets.

anon582847382
  • 19,907
  • 5
  • 54
  • 57
  • The puzzle to me is why should i remove square brackets? – Abhishek Dave Oct 31 '14 at 17:22
  • 2
    @DaveA Because they are not syntactically required... and as I just explained they make a list... – anon582847382 Oct 31 '14 at 17:23
  • 1
    @DaveA Square brackets indicate a list in Python, which is an instance of the `list` class. Instances of classes are `True` by default in Python. Except in the case of an empty list (Or any empty sequence such as `[]`, `()` or `{}`). – kylieCatt Oct 31 '14 at 17:23
  • 1
    @IanAuld you're right about the brackets, but the last sentence is wrong. The list is True because it is not empty - it contains a single boolean value - not because it is an instance. – Daniel Roseman Oct 31 '14 at 17:25
  • 1
    @DanielRoseman There is nothing wrong in the last sentence. AN empty list or sequence is inherently `False`. I dind't say the list was empty, I was replying to him asking why he shouldn't use brackets. He shouldn't use them because they create a list. – kylieCatt Oct 31 '14 at 17:28
  • @IanAuld No, you're incorrect about instances being truthy, certainly for numerical types. `bool(int()) == False` – anon582847382 Oct 31 '14 at 17:30
  • You added that extra sentence while I was writing my comment: I was referring to the second sentence which was inaccurate without it. – Daniel Roseman Oct 31 '14 at 17:32
1

In Python, you don't need to use parenthesis (let alone square brackets) to structure an if-statement.

In your first example, you wrote if [ dang == False ] : which ends up being simplified to if [False] which evaluates to True, being a non-empty list. That means that the if-statement passes, it's code gets ran, and the next conditions (if any) are skipped.

[ dang == False ] creates a list, it's not how you structure ifs in Python, simple as that.

TankorSmash
  • 12,186
  • 6
  • 68
  • 106
  • 1
    actually, `[False]` evaluates to `True` in a boolean context, as it's a non-empty `list`. – ch3ka Oct 31 '14 at 17:55