mardi 2 août 2016

What is the order of operations for 'and' and 'or'?

In Python is this:

def blackjack_check(hand): # hand is a tuple
    winning_cards = [10,'Jack','Queen','King']
    if hand[0] in winning_cards and hand[1] == 'Ace':
        return True
    elif hand[0] == 'Ace' and hand[1] in winning_cards:
        return True
    else:
        return False

the same as this...?

def blackjack_check(hand): # hand is a tuple
    winning_cards = [10,'Jack','Queen','King']
    if (hand[0] in winning_cards and hand[1]=='Ace' or 
        hand[0] == 'Ace' and hand[1] in winning_cards):
        return True
    else:
        return False

Can I use the second code block instead of the first? It would eliminate an extra elif statement and it just seems cleaner. My concern is how the 'and' and 'or' operators work. Are the two 'and' comparisons separate and the 'or' compares them? Is there an order of operations for 'and' and 'or'? I ran the code and it works both ways but I want to make sure I understand exactly how the operators work.

Aucun commentaire:

Enregistrer un commentaire