list - Python in operator is not working -


when run code, nothing shows up. example call ind(1, [1, 2, 3]), don't integer 13.

def ind(e, l):     if (e in l == true):         print('13')     else:          print('12') 

operator precedence. if put ( , ) around e in l work:

def ind(e, l):     if ((e in l) == true):         print('13')     else:         print('12')  ind(1, [1, 2, 3]) 

but testing true can done (and usual idiom) done without true

def ind(e, l):     if (e in l):         print('13')     else:         print('12')  ind(1, [1, 2, 3]) 

edit: bonus can use true , false keep/nullify things. example:

def ind(e, l):     print('13' * (e in l) or '12')  ind(1, [1, 2, 3])  ind(4, [1, 2, 3]) 

and ouputs:

13 12 

because e in l has first been evaluated true , 13 * true 13. 2nd part of boolean expression not looked up.

but when calling function 4, following happens:

`13` * (e in l) or '12` -> `13` * false or '12' -> '' or '12' -> 12 

becase , empty string evaluates false , therefore 2nd part of or boolean expression returned.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -