Wat is the difference between if [variable] : and if [variable] == True : | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Wat is the difference between if [variable] : and if [variable] == True :

14th Oct 2020, 9:39 AM
Hemanth Kumar
Hemanth Kumar - avatar
5 Answers
+ 5
This is what the python PEP8 style guide is saying about how to compare to bool: # Correct: if greeting:    ... # Wrong: if greeting == True:    ... Worse: # Wrong: if greeting is True: ...
14th Oct 2020, 10:02 AM
Lothar
Lothar - avatar
+ 3
When variable is a numeric datatype there shouldn't be any practical difference. But when variable is for example a list or a string, then [variable] == True would never result in True. if variable is a list, then if [variable] is based on whether the list is empty or not.
14th Oct 2020, 10:48 AM
Seb TheS
Seb TheS - avatar
0
The first if block will run for all truthy values.
14th Oct 2020, 10:00 AM
Ankit
0
that is about boolean functions, 'is' stands for bool(?)==True. You know that , their root language was C language but have developed that recent languages through compilers and interpreters, as far as have learned. If [var]: means that if that stored value was a true value(either in +ive or -ive , non zero or an immutable value like string or the others). if [var]==True: That is about matching value as exactly equal to True. try those in the code play yard... varriable=[1, 'true'] for var in varriable : if var==True: print(var) elif var : print(var, str(2)) # first returns 1 # second returns true2 or try that:- varriable=[7, 'true'] for var in varriable : if var==True: print(var) elif var : print(var) # first returns 7 # second returns true
14th Oct 2020, 11:47 AM
Azad m. A.
Azad m. A. - avatar
0
'is' used to mean matched valued like print(a is b) #True or 1 #False or 0 I doubt that to be used, for such cases.
14th Oct 2020, 12:21 PM
Azad m. A.
Azad m. A. - avatar