If AND

Introduction:

  • Python supports the usual logical conditions from mathematics:
    • Equals: a == b
    • Not Equals: a != b
    • Less than: a < b
    • Less than or equal to: a <= b
    • Greater than: a > b
    • Greater than or equal to: a >= b

Combining Conditions with AND:

  • You can combine multiple conditions into a single expression in Python conditional statements like if, if-else, and elif statements using the AND logical operator. This helps avoid writing multiple nested if statements unnecessarily.

Examples:

  1. Python If with AND Operator:
    • Using the AND operator to combine two boolean conditions.

      python
      a = 5
      b = 2
      
      # Nested if
      if a == 5:
          if b > 0:
              print('a is 5 and', b, 'is greater than zero.')
      
      # OR you can combine the conditions as
      if a == 5 and b > 0:
          print('a is 5 and', b, 'is greater than zero.')
      
    • Output:

      a is 5 and 2 is greater than zero.
      a is 5 and 2 is greater than zero.
      
  2. Python If-Else with AND Operator:
    • Combining two basic conditional expressions in the boolean expression of a Python if-else statement.

      python
      a = 3
      b = 2
      
      if a == 5 and b > 0:
          print('a is 5 and', b, 'is greater than zero.')
      else:
          print('a is not 5 or', b, 'is not greater than zero.')
      
    • Output:

      a is not 5 or 2 is not greater than zero.
      
  3. Python Elif with AND Operator:
    • Combining two basic conditional expressions in the boolean expression of a Python elif statement.

      python
      a = 8
      
      if a < 0:
          print('a is less than zero.')
      elif a > 0 and a < 8:
          print('a is in (0.8)')
      elif a > 7 and a < 15:
          print('a is in (7.15)')
      
    • Output:

      a is in (7.15)
      

Exercises:

  1. If with AND Operator:
    • Write a program to check if a number is between 1 and 10 and if it is even.

      python
      number = int(input("Enter a number: "))
      
      if number > 1 and number < 10 and number % 2 == 0:
          print("The number is between 1 and 10 and is even.")
      else:
          print("The number is not between 1 and 10 or it is not even.")
      
  2. If-Else with AND Operator:
    • Write a program to check if a user is a teenager (age between 13 and 19) and if they have permission.

      python
      age = int(input("Enter age: "))
      has_permission = input("Do you have permission (yes/no): ").lower() == 'yes'
      
      if age >= 13 and age <= 19 and has_permission:
          print("You are a teenager with permission.")
      else:
          print("You are either not a teenager or do not have permission.")
      

Summary:

  • In this tutorial, we learned how to use the Python AND logical operator with Python conditional statements (if, if-else, elif). Practice using these operators to become proficient in forming compound logical expressions in your programs