Good! Now, what happens when we want to combine and, or, and not together?
Imagine a car rental company in Spain: it will rent a car to any driver from Spain or to any foreign driver who is at least 26 years old:
driver_nationality = input('What is your nationality? ')
driver_age = int(input('What is your age? '))
if (driver_nationality == 'Spanish'
or driver_nationality != 'Spanish' and driver_age >= 26):
print('You are good to go!')
else:
print('Sorry, no car for you!')
Which operator – or or and – has higher priority? Is this condition really:
(driver_nationality == 'Spanish'
or driver_nationality != 'Spanish') and driver_age >= 26
Or is it:
driver_nationality == 'Spanish'
or (driver_nationality != 'Spanish' and driver_age >= 26)
Python checks all conditions in the following order: not, and, or.
This means that the statement will be interpreted as:
driver_nationality == 'Spanish'
or (driver_nationality != 'Spanish' and driver_age >= 26)
If you want your conditions to be evaluated in another way, simply use parentheses. Actually, it's a best practice to always use them, as they improve the readability of your code.