Boolean Expressions

The Boolean data type contains two Boolean values, denoted as True and False in Python. A Boolean expression is an expression that evaluates to a Boolean value. Boolean expressions are used to denote the conditions for selection and iterative control statements.

Relational Operators

  • The relational operators in Python perform the comparison operations
  • Relational expressions are a type of Boolean expression, since they evaluate to a Boolean result.
  • These operators not only apply to numeric values, but to any set of values that has an ordering, such as strings.
  • String values are ordered based on their character encoding, which normally follows a lexographical (dictionary) ordering .
  • For example, 'Alan' is less than 'Brenda' since the Unicode (ASCII) value for 'A' is 65, and 'B' is 66.
  • However, 'alan' is greater than (comes after) 'Brenda' since the Unicode encoding of lowercase letters (97, 98, . . .) comes after the encoding of uppercase letters (65, 66, . . .).

Membership Operators

  • These operators can be used to easily determine if a particular value occurs within a specified list of values.
  • The membership operators are given in Figure.
  • The in operator is used to determine if a specific value is in a given list, returning True if found, and False otherwise.
  • The not in operator returns the opposite result.

The membership operators can also be used to check if a given string occurs within another string.

Boolean Operators

Boolean algebra contains a set of Boolean (logical) operators, denoted by and, or, and not in Python. The Boolean operators are shown in Figure

 Logical and is true only when both its operands are true—otherwise, it is false. Logical or is true when either or both of its operands are true, and thus false only when both operands are false.  Logical not simply reverses truth values—not False equals True, and not True equals False.