Eli Zatlawy
Eli Zatlawy

Reputation: 1012

Python multiple 'OR' 'AND' conditions without parenthesis behaviour

The fooliwng code:

def foo(arg):
    return arg==arg[::-1]

if __name__ == '__main__':
    print (foo(['A', 'V', 'I', 'V', 'A']) or foo(['A', 'B', 'C', 'D']) and foo([1, 2, 3, 1]))

will print True - why?

why the behavior is like:

   print (foo(['A', 'V', 'I', 'V', 'A']) or (foo(['A', 'B', 'C', 'D']) and foo([1, 2, 3, 1])))

And not like:

   print ((foo(['A', 'V', 'I', 'V', 'A']) or foo(['A', 'B', 'C', 'D'])) and foo([1, 2, 3, 1]))

Upvotes: 0

Views: 449

Answers (1)

Sadegh Hayeri
Sadegh Hayeri

Reputation: 109

This is because AND operator has higher precedence than OR, check python documentation.

Upvotes: 1

Related Questions