Python Logical Operators


Logical Operators in Python

Logical operators in Python are used to combine conditional statements and evaluate logical expressions. They allow you to perform logical operations on Boolean values (True and False) and are commonly used in control flow statements, such as if statements, to determine the outcome based on multiple conditions.

List of Logical Operators

OperatorDescriptionExampleOutput
andReturns True if both operands are truea and bDepends on a and b
orReturns True if at least one operand is truea or bDepends on a and b
notReturns True if the operand is false (negates the Boolean value)not aDepends on a

Examples of Logical Operators

1. Logical AND (and)

The and operator returns True if both operands are true; otherwise, it returns False.

a = True b = False # Logical AND result = a and b print(result) # Output: False (since b is False) # Example with both True x = 5 y = 10 result = (x > 0) and (y > 5) print(result) # Output: True (both conditions are true)

2. Logical OR (or)

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

a = True b = False # Logical OR result = a or b print(result) # Output: True (since a is True) # Example with one True x = 5 y = 2 result = (x < 0) or (y > 1) print(result) # Output: True (y > 1 is true)

3. Logical NOT (not)

The not operator negates the Boolean value of its operand. If the operand is True, it returns False, and vice versa.

a = True # Logical NOT result = not a print(result) # Output: False # Example b = False result = not b print(result) # Output: True

Using Logical Operators in Conditional Statements

Logical operators are often used in if statements to combine multiple conditions.

age = 20 has_permission = True # Combine conditions with logical operators if age >= 18 and has_permission: print("You are allowed to enter.") # Output: You are allowed to enter. else: print("You are not allowed to enter.")

Example with Multiple Conditions

You can combine multiple logical conditions using and, or, and not.

a = 5 b = 10 c = 15 # Check multiple conditions using logical operators if (a < b and b < c) or (a > c): print("Condition is True") else: print("Condition is False") # Output: Condition is True

Short-circuit Evaluation

Logical operators in Python use short-circuit evaluation, meaning:

  • For and, if the first operand is False, the second operand is not evaluated because the overall result cannot be True.
  • For or, if the first operand is True, the second operand is not evaluated because the overall result will be True.

Example:

def function(): print("Function is called.") return True # Example of short-circuit evaluation with `and` result = False and function() # Function is not called because the first operand is False print(result) # Output: False # Example of short-circuit evaluation with `or` result = True or function() # Function is not called because the first operand is True print(result) # Output: True

Summary of Logical Operators:

  • and: Returns True if both operands are true; otherwise returns False.
  • or: Returns True if at least one operand is true; returns False only if both are false.
  • not: Negates the Boolean value of the operand; returns True if false, and False if true.

Logical operators are essential for controlling the flow of logic in Python programs, enabling complex conditions and decision-making processes.