Start learning free

LESSON 80 OF 410 · CONDITIONAL STATEMENTS

Logical Operators: And

Lesson Read the full explanation

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

    PYTHON · CONDITIONAL STATEMENTS

    And Operator in Python, explained

    Python's `and` Operator: Requiring Multiple Conditions to be True

    Real decisions are rarely based on a single fact. A user can log in only if their username and password are correct. A discount applies only if the cart total is above $50 and the user is a member. Python's and operator lets you express exactly these kinds of multi-condition rules — cleanly and intuitively.

    How `and` Works: The Truth Table

    and returns True only when both sides are True. If either side is False, the whole expression is False:

    print(True  and True)    # True  — both true
    print(True  and False)   # False — right side is false
    print(False and True)    # False — left side is false
    print(False and False)   # False — both false
    

    Think of it as a strict requirement: every condition must pass.

    Using `and` in if Statements

    age = 25
    has_license = True
    
    if age >= 18 and has_license:
        print("You can drive.")
    else:
        print("You cannot drive.")
    # You can drive.
    
    # Change one condition:
    has_license = False
    if age >= 18 and has_license:
        print("You can drive.")
    else:
        print("You cannot drive.")
    # You cannot drive.  ← one False makes the whole thing False
    

    Real-World Examples

    # Login validation — both must match:
    username = "alex"
    password = "secure123"
    
    if username == "alex" and password == "secure123":
        print("Access granted.")
    else:
        print("Access denied.")
    
    # Discount eligibility — two conditions required:
    cart_total = 75
    is_member = True
    
    if cart_total >= 50 and is_member:
        discount = cart_total * 0.10
        print(f"10% discount applied: ${discount:.2f} off")
    else:
        print("No discount available.")
    
    # Age range check:
    age = 20
    if age >= 18 and age <= 25:
        print("Young adult pricing applies.")
    

    Chaining Multiple `and` Conditions

    You can chain as many and conditions as you need. All of them must be True for the block to run:

    def can_graduate(gpa, credits_earned, fees_paid):
        if gpa >= 2.0 and credits_earned >= 120 and fees_paid:
            return "Eligible to graduate!"
        return "Requirements not met."
    
    print(can_graduate(3.5, 130, True))   # Eligible to graduate!
    print(can_graduate(3.5, 130, False))  # Requirements not met.
    print(can_graduate(1.8, 130, True))   # Requirements not met.
    

    Short-Circuit Evaluation: Python Stops Early

    Python evaluates and expressions left to right and stops as soon as it finds a False. This is called short-circuit evaluation and it matters for performance and safety:

    my_list = []
    
    # Safe: if list is empty, the second condition never runs (no IndexError):
    if len(my_list) > 0 and my_list[0] == 42:
        print("First item is 42")
    else:
        print("List is empty or first item isn't 42")
    

    Put the cheapest or most likely to fail condition first — Python won't bother evaluating the rest.

    Common Mistakes to Avoid

    • Confusing and with or: and requires ALL conditions; or needs just ONE.
    • Writing if x == 1 and 2: instead of if x == 1 and x == 2: — The second form always evaluates 2 as truthy. Repeat the variable name for each comparison.
    x = 1
    
    # BUG — "and 2" is always True (non-zero integer):
    if x == 1 and 2:
        print("This runs — but probably not what you meant")
    
    # CORRECT:
    if x == 1 and x == 2:
        print("This never runs because x can't be both 1 and 2")
    
    # Probably intended:
    if x == 1 or x == 2:
        print("x is 1 or 2")   # This runs
    

    Key Takeaways

    • and returns True only when both sides are True.
    • One False anywhere in an and chain makes the whole expression False.
    • Python short-circuits — it stops evaluating as soon as it finds a False.
    • Use and when a condition must satisfy multiple independent requirements simultaneously.

    What to Learn Next

    Now that you know and, explore the or operator (True if at least one condition is True) and the not operator (inverts a boolean). Together, these three logical operators let you express any decision your program needs to make.

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free