0
Python
list = ['hello', 'buddy'] If 'no' or 'nobuddy' in list: print("not good") else: print("good") It's print: not good why?
3 Answers
+ 9
Mohammad Faiz ,
a simple way to write this conditional statemenf would be:
...
if 'no' in lst or 'nobuddy' in lst: # i have used `lst` as variable name for a list and not `list`
...
+ 3
'no' or 'nobuddy' in list is evaluated as
if ('no') or ( 'nobuddy' in list) :
True or ('nobuddy' in list) : => True
Anything other than 'empty string or null' are True in boolean equalent. so prints 'not good'
+ 1
Thanks