If else Statement with Logical Operators

The logical operators and and or in Python are used to combine multiple conditions in if-else statements. These operators allow you to create complex conditional logic by combining simpler Boolean expressions.

Logical Operators

  1. and Operator:
    • The and operator returns True if both conditions are True. Otherwise, it returns False.
    • Syntax: condition1 and condition2
  2. or Operator:
    • The or operator returns True if at least one of the conditions is True. If both conditions are False, it returns False.
    • Syntax: condition1 or condition2

Using and in if-else Statements

The and operator is used when you want to check if multiple conditions are all True.

Example 1: Checking Age and License

age = 25
has_license = True

if age >= 18 and has_license:
    print("You are allowed to drive.")
else:
    print("You are not allowed to drive.")

Explanation:

  • The if statement checks if age is greater than or equal to 18 and if has_license is True.
  • If both conditions are True, it prints “You are allowed to drive.”
  • If either condition is False, it prints “You are not allowed to drive.”

Example 2: Checking Scores

math_score = 85
science_score = 90

if math_score >= 75 and science_score >= 75:
    print("You passed both subjects.")
else:
    print("You did not pass both subjects.")

Explanation:

  • The if statement checks if both math_score and science_score are greater than or equal to 75.
  • If both conditions are True, it prints “You passed both subjects.”
  • If either condition is False, it prints “You did not pass both subjects.”

Using or in if-else Statements

The or operator is used when you want to check if at least one of the conditions is True.

Example 1: Checking Discount Eligibility

is_student = True
is_senior = False

if is_student or is_senior:
    print("You are eligible for a discount.")
else:
    print("You are not eligible for a discount.")

Explanation:

  • The if statement checks if is_student is True or if is_senior is True.
  • If at least one of the conditions is True, it prints “You are eligible for a discount.”
  • If both conditions are False, it prints “You are not eligible for a discount.”

Example 2: Checking Access Rights

has_admin_rights = False
has_editor_rights = True

if has_admin_rights or has_editor_rights:
    print("You have access to edit the content.")
else:
    print("You do not have access to edit the content.")

Explanation:

  • The if statement checks if has_admin_rights is True or if has_editor_rights is True.
  • If at least one of the conditions is True, it prints “You have access to edit the content.”
  • If both conditions are False, it prints “You do not have access to edit the content.”

Combining and and or

You can combine and and or operators to create more complex conditions. Use parentheses () to ensure the correct order of evaluation.

Example: Complex Condition

age = 20
has_permission = True
is_vip = False

if (age >= 18 and has_permission) or is_vip:
    print("Access granted.")
else:
    print("Access denied.")

Explanation:

  • The if statement first checks if (age >= 18 and has_permission) is True. If both conditions inside the parentheses are True, it evaluates to True.
  • If the first part is False, it then checks if is_vip is True.
  • If either the combined condition (age >= 18 and has_permission) is True or is_vip is True, it prints “Access granted.”
  • If both parts are False, it prints “Access denied.”

Summary

Using the and and or logical operators in if-else statements allows you to handle complex logical conditions efficiently. By understanding how these operators work, you can create more readable and maintainable code. Always remember to use parentheses () to clarify the order of evaluation when combining multiple conditions.