jeudi 30 novembre 2017

Does a two parted condition with "and"/ && stop when one part is already false?

simplified example:

a = False
b = True

if a and b:
    #do stuff

Does python skip checking for b if a is already recognized as false because only one condition needs to be False for the whole statement to be False?

In my case i want to check an array for 3 conditions but want to stop if at least one of them is false (for a better runtime). Can i do

if a and b and c:
    #do stuff

or do i have to go the long way with

if a:
    if b:
        if c:
            return True
        else:
            return False
     else:
         return False
 else: 
     return False

or is there another way to check stuff like this?

Aucun commentaire:

Enregistrer un commentaire