0
Wat is the difference between if [variable] : and if [variable] == True :
6 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:
...
+ 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.
0
The first if block will run for all truthy values.
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
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.