0% found this document useful (0 votes)
12 views

Lec 5. Logical Operator - Boolean Operator

Uploaded by

sudeepiitb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lec 5. Logical Operator - Boolean Operator

Uploaded by

sudeepiitb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Fall 2019

Computational Thinking with @cse_bennett @csebennett

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.

Operator Meaning Example

and True if both the operands are true x and y


or True if either of the operands is true x or y
not True if operand is false (complements the operand) not x
Boolean Logic Truth Table
Example
Binary Representation of Decimal Numbers
Binary Representation of Decimal Numbers
Bitwise operators
Bitwise operators act on operands as if they were string of binary
digits.

It operates bit by bit.


• 2 is 10 in binary and 7 is 111.

In the table below:


• Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)
Bitwise operators
Operator Description
& Binary AND Operator copies a bit to the result if it exists in
both operands
| Binary OR It copies a bit if it exists in either operand.
^ Binary XOR It copies the bit if it is set in one operand but not
both.
~ Binary Ones Complement It is unary and has the effect of 'flipping' bits.
<< Binary Left Shift The left operands value is moved left by the
number of bits specified by the right operand.

>> 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.

• Binary for 4 is 0100, and that for 8 is 1000.


• After operation, output is 1100
4|8 • Which is binary for 12.
Bitwise operators AND OR XOR
Shift operator
Left Shift (<<) Right Shift (>>)
Operator Precedence
Operators Meaning
() Parentheses
** Exponent
+x, -x, ~x Unary plus, Unary minus, Bitwise NOT

*, /, //, % Multiplication, Division, Floor division, Modulus

+, - 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

E.g., Adding a float and an integer will


automatically result in a float value

Takes place automatically during run


time between ONLY numeric values
E.g., Adding a string and an integer (or
a float) will result in an error since
string is not numeric
Implicit Data Conversion

Conversion goes from integer to float


Applies type promotion to avoid loss (e.g., upon adding a float and an
of information integer) and not vice versa so as the
fractional part of the float is not lost

You might also like