0

Python

list = ['hello', 'buddy'] If 'no' or 'nobuddy' in list: print("not good") else: print("good") It's print: not good why?

17th Oct 2022, 6:45 PM
Mohammad Faiz
Mohammad Faiz - avatar
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` ...
17th Oct 2022, 8:37 PM
Lothar
Lothar - avatar
+ 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'
17th Oct 2022, 7:02 PM
Jayakrishna 🇼🇳
+ 1
Thanks
17th Oct 2022, 7:03 PM
Mohammad Faiz
Mohammad Faiz - avatar