Start learning free

LESSON 88 OF 673 · CONDITIONAL STATEMENTS

Logical Operators: And

Lesson

TL ; DR:

  • We use the and operator inside if statements 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 True
  • 7 < 10, which evaluates to True
As both conditions evaluate to True, the entire line will evaluate 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 to 10 != 7, which evaluates to True
  • 12 < x, equivalent to 12 < 10, which evaluates to False
As one of the conditions evaluates to False, the entire statement will evaluate to False, and so the program prints nothing.

Assignment
Follow the Coding Tutorial and play with the and operator.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check