python - If statement with composite condition to validate condition against each item in a list -
this question has answer here:
- specifics of list membership 3 answers
i found similar question here answers don't seem apply issue.
here code:
y = 3 list1 = [1,2,3,4,5] if y != 0 or y != list1: print("y not in range") else: print(y)
it keeps printing y not in range
.
my goal check if y
not equal 0
or if y
not equal item in list.
i understand above or
should and
, i'm interested in how check in condition of y
being contained in list.
you want check y
different 0
and not in list:
if y != 0 , y not in list1:
using or
means one of conditions sufficient, since y != 0
returns true
without going y != list1
return false
because int
not list
, have use in
in case.
if want use or
want:
if not (y == 0 or y in list1): print('y not in range') else: print(y)
rememer de morgan laws:
not (y == 0 or y in list1) == (not y == 0) , (not y in list1) == y != 0 , y not in list1
Comments
Post a Comment