Combine Multiple If...

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 Multiple Conditions:

  • In Python, you can combine multiple conditions in an if statement using logical operators such as and, or, and not.

Using and:

  • The and operator returns True if both conditions it connects are true. If either condition is false, it returns False.

    python
    if condition1 and condition2:
        # code to execute if both condition1 and condition2 are True
    

Using or:

  • The or operator returns True if at least one of the conditions it connects is true. If both conditions are false, it returns False.

    python
    if condition1 or condition2:
        # code to execute if either condition1 or condition2 is True
    

Using not:

  • The not operator negates the boolean value of the condition. It returns True if the condition is false, and False if the condition is true.

    python
    if not condition:
        # code to execute if condition is False
    

Example:

  • Here's an example combining multiple conditions using these operators:

    python
    x = 5
    y = 10
    
    if x == 5 and y == 10:
        print("Both x is 5 and y is 10")
    
    if x == 5 or y == 5:
        print("Either x is 5 or y is 5")
    
    if not (x == 10):
        print("x is not equal to 10")
    

Output:

Both x is 5 and y is 10
Either x is 5 or y is 5
x is not equal to 10
  • In this code:
    • The first if statement will print "Both x is 5 and y is 10" because both x == 5 and y == 10 are true.
    • The second if statement will print "Either x is 5 or y is 5" because x == 5 is true, even though y == 5 is false.
    • The third if statement will print "x is not equal to 10" because x is not equal to 10.

Exercises:

  1. Using and Operator:
    • Write a program to check if a number is between 1 and 10 and 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 either not between 1 and 10 or it is not even.")
      
  2. Using or Operator:
    • Write a program to check if a number is either less than 5 or greater than 15.

      python
      number = int(input("Enter a number: "))
      
      if number < 5 or number > 15:
          print("The number is either less than 5 or greater than 15.")
      else:
          print("The number is between 5 and 15.")
      
  3. Using not Operator:
    • Write a program to check if a user-provided string is not empty.

      python
      user_string = input("Enter a string: ")
      
      if not user_string:
          print("The string is empty.")
      else:
          print("The string is not empty.")
      

Summary:

  • Combining multiple conditions in an if-statement using and, or, and not allows for more complex and powerful conditional logic. Practice using these logical operators to become proficient in writing advanced if-statements in Python