Python > Core Python Basics > Fundamental Data Types > Booleans (bool)

Boolean Operations: and, or, not

This snippet demonstrates boolean operations in Python, including and, or, and not. These operators are essential for creating complex conditional logic.

Boolean Operators Example

This code demonstrates the use of and, or, and not operators.

  • and: The and operator returns True if both operands are True, otherwise it returns False. In the example, is_adult and has_license evaluates to False because has_license is False.
  • or: The or operator returns True if at least one of the operands is True, otherwise it returns False. has_discount or is_student evaluates to True because has_discount is True.
  • not: The not operator returns the opposite boolean value of its operand. not has_umbrella evaluates to True because has_umbrella is False. Therefore, is_raining and not has_umbrella evaluates to True.

is_adult = True
has_license = False

can_drive = is_adult and has_license
print(f"Can drive: {can_drive}")

is_raining = True
has_umbrella = False

will_get_wet = is_raining and not has_umbrella
print(f"Will get wet: {will_get_wet}")

has_discount = True
is_student = False

apply_discount = has_discount or is_student
print(f"Apply discount: {apply_discount}")

Concepts behind the snippet

Boolean operators allow you to combine or negate boolean expressions. Understanding these operators is crucial for building complex conditional logic in your programs. The and operator requires both conditions to be true, the or operator requires at least one to be true, and the not operator reverses the boolean value.

Real-Life Use Case Section

Imagine a scenario where you need to verify if a user is both logged in and has admin privileges before allowing them to perform certain actions. You would use the and operator to combine these two conditions. Or, if a user needs to meet at least one of several criteria (e.g. age or student status) to qualify for a promotion, you would use the or operator.

Best Practices

When dealing with multiple boolean operators, use parentheses to clarify the order of operations and avoid ambiguity. This makes your code easier to read and maintain. For instance, (a and b) or c is clearer than a and b or c.

Interview Tip

Be prepared to explain the truth tables for and, or, and not. Also, practice simplifying complex boolean expressions.

When to use them

Use boolean operators whenever you need to combine or negate conditions in your code. They are essential for creating flexible and powerful decision-making logic.

FAQ

  • What is the order of precedence for boolean operators?

    The order of precedence for boolean operators in Python is not, and, then or. Parentheses can be used to override this order.
  • Can I use boolean operators with non-boolean values?

    Yes, Python supports truthiness. Non-boolean values can be implicitly converted to boolean values. For example, an empty string is considered False, while a non-empty string is considered True. Similarly, 0 is considered False, while any non-zero number is considered True.