TL ; DR:
We use the
andoperator insideifstatements to check if multiple conditions are being met:car = 'Ford' year = 1993 if car == 'Chevrolet' and year > 1985: print("This is a great car!") else: print("Not my style")
Full lesson:
Sometimes you will need to test more than one thing at a time. The logical and operator returns True if and only if both conditions to the left and right of it are True. For example:
10 == 10 and 7 < 10 # Evaluates to True
We have two conditions separated by and operator:
10 == 10, which evaluates to True7 < 10, which evaluates to True
An example inside an if statement:
x = 10
if x != 7 and 12 < x: # Evaluates to False
print("This is true!")
Inside the if, we have two conditions separated by and operator:
x != 7, equivalent to10 != 7, which evaluates to True12 < x, equivalent to12 < 10, which evaluates to False
Assignment
Follow the Coding Tutorial and play with the and operator.
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. PRO is unlimited.
