Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

all vs and AND any vs or

Tags:

python

I was eager to know about the what is the difference between

all and "and"
any and "or"

for example: status1=100,status2=300,status3=400

Which is better to use:

if status1==100 and status2 ==300 and status3 ==400:

or

if all([status1==100,status2==300,status3==400]):

similarly for the any and or condition:

   if status1==100 or status2 ==300 or status3==400:
or
   if any([status1==100, status2 ==300, status3==400])

which one is more efficent, using the builtin functions or the primitive or and and conditions ?

like image 236
Nishant Kashyap Avatar asked Mar 19 '14 15:03

Nishant Kashyap


1 Answers

The keywords and and or follow Python's short circuit evaluation rules. Since all and any are functions, all arguments would be evaluated. It's possible to get different behaviour if some of the conditions are functions calls.

like image 196
eduffy Avatar answered Sep 18 '22 22:09

eduffy