Lec 5. Logical Operator - Boolean Operator
Lec 5. Logical Operator - Boolean Operator
Programming
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
• 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.
• Note the use of the comparison operator == for determining if two
values are equal. This, rather than the (single) equal sign = is used since
the equal sign is used as the assignment operator.
• This is often a source of confusion for new programmers,
• num = 10 variable num is assigned the value 10
• num == 10 variable num is compared to the value 10
Relational Operators
Example
True
True
True
True
False
True
Membership Operators
Boolean Operators
• George Boole, in the mid-1800s, developed what we now call Boolean
algebra.
• His goal was to develop an algebra based on true/false rather than
numerical values.
• Boolean algebra contains a set of Boolean ( logical ) operators , denoted
by and, or, and not in Python.
Boolean Operators
• 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.
>> Binary Right Shift The left operands value is moved right by the
number of bits specified by the right operand.
Bitwise operators
Bitwise operators AND OR
• Same as 101 & 111.
• This results in 101
5&7 • Which is binary for 5.
+, - Addition, Subtraction
<<, >> Bitwise shift operators
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
==, !=, >, >=, <, <=, in, not in Comparisions, Membership operators
not Logical NOT
and Logical AND
or Logical OR
DATA TYPE CONVERSION
Explicit and Implicit
We saw this earlier with the int, float,
Explicit Data Conversion
and str built-in functions