Logical Operators

Logical operators in Python are used to combine multiple conditions and perform logical operations. These operators are fundamental for decision-making in your code, allowing you to control code based on multiple conditions.

Operator Description Example
Logical Operators    
not Logical NOT not x
& Element-wise logical AND x & y
and Logical AND x and y
| Element-wise logical OR x | y
or Logical OR x or y

Logical NOT (not)

The not operator is a unary operator that inverts the Boolean value of its operand. If the operand is True, not makes it False, and vice versa.

x = True
result = not x
print(result)
# Output: False

Logical AND (and)

The and operator returns True if both operands are True; otherwise, it returns False. This operator is commonly used to ensure that multiple conditions are met before executing a block of code.

x = True
y = False
result = x and y
print(result)
# Output: False

Logical OR (or)

The or operator returns True if at least one of the operands is True. It is used when you want to execute code if one or more conditions are met.

x = True
y = False
result = x or y
print(result)
# Output: True

Element-wise Logical AND (&)

The & operator performs element-wise logical AND operations, typically used with arrays or lists. This operator is more commonly used in data science and array manipulations with libraries like NumPy.

import numpy as np

array1 = np.array([True, False, True])
array2 = np.array([False, False, True])

result = array1 & array2
print(result)
# Output: [False False  True]

Element-wise Logical OR (|)

The | operator performs element-wise logical OR operations, similar to the & operator but returns True if at least one of the corresponding elements is True.

import numpy as np

array1 = np.array([True, False, True])
array2 = np.array([False, False, True])

result = array1 | array2
print(result)
# Output: [ True False  True]

Updated: