+ 4

Is an empty string true or false in python 3

This is the code I have made to check it but I don't get it still... https://code.sololearn.com/cAV8IGGRwinU/?ref=app When I check that ''==False it returns false. But if I use bool('') it returns False....

29th Apr 2019, 1:19 PM
Saksham Jain
Saksham Jain - avatar
12 Respostas
+ 6
7.0 and 7 have different types too, yet they're equal. The point is that '' and False have the same boolean values, but that doesn't make them equal. None, [], set() etc. are all False. If we were to say that '' == False were True, we'd consequently have to say that 0 == '', [] == None etc. are also True because if a=b and b=c, then a=c (btw, 5 and 7 also have the same boolean values but that doesn't mean that 5 == 7 should be True)
29th Apr 2019, 2:24 PM
Anna
Anna - avatar
+ 5
Saksham Jain Both ''==False and '' ==True are False because an empty string is not a boolean expression. It has a boolean value which can be found out with bool(''). But an empty string is not equivalent to False. If it were the same, an expression like if 5-5=='' would evaluate to True. 5-5 is 0, 0 is False. '' is False. False == False is True. This expression would also be True: 'hello' == 'goodbye' (True == True is True). Using boolean logic this way obviously leads to unwanted results
4th May 2019, 1:56 PM
Anna
Anna - avatar
+ 4
Anna I still not got why ''==False is False And also ''==True is False But bool('')==False is True.
29th Apr 2019, 2:50 PM
Saksham Jain
Saksham Jain - avatar
+ 2
Aymane Boukrouh that I can see from my code but why doesn't ""==False give True?
29th Apr 2019, 1:43 PM
Saksham Jain
Saksham Jain - avatar
+ 2
Aymane Boukrouh I wanted ask why doesn't the second if block executed in my code
29th Apr 2019, 1:45 PM
Saksham Jain
Saksham Jain - avatar
+ 1
Saksham Jain I think an empty string is neither false nor true. If you try: if(not None): print('test') Then you get test. So probably not None ans not empty_string returns True
29th Apr 2019, 1:51 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
Try if bool("") == False
29th Apr 2019, 2:03 PM
Anna
Anna - avatar
+ 1
It's like comparing apples with pears, the conditions: "==False or "==None or "==[] or "=={}, will never be True because are different types
29th Apr 2019, 2:10 PM
raget88
raget88 - avatar
0
False
29th Apr 2019, 1:42 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
Try: a = '' If(not a): print("test") OUTPUT: test
29th Apr 2019, 1:43 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
I don't know to be honest, I'll look for it and answer you.
29th Apr 2019, 1:48 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
you are using if function, if the result of if function is some value the it will say "True" , otherwise if it return zero or blank, it will return boolean "False"
4th May 2019, 1:45 PM
Gary Jonathan
Gary Jonathan - avatar