Unit III Lecture Hour 1
Unit III Lecture Hour 1
Boolean expressions
A boolean expression (or logical expression) evaluates to one of two states true or false. Python provides
the boolean type that can be either set to False or True. Many functions and operations returns boolean
objects.
What is False ?
Every object has a boolean value. The following elements are false:
None
False
0 (whatever type from integer, float to complex)
Empty collections: “”, (), [], {}
>>> x = 0
>>> bool(x)
False
>>> y = False*False
>>> bool(y)
False
>>> a = []
>>> bool(a)
False
>>> b = ()
>>> bool(b)
False
Comparison operators
The <, <=, >, >=, ==, != operators compare the values of 2 objects and returns True or False.
Comparison depends on the type of the objects. See the Classes to see how to refedine the comparison
operator of a type.
10 == 10
10 <= 10.
>>> x = 2
>>> 1 < x < 3
True
>>> 10 < x < 20
False
>>> 3 > x <= 2
True
>>> 2 == x < 4
True
The comparison is performed between each pair of terms to be evaluated. For instance in the first
example, 1<x is evaluated to True AND x<2 is evaluated. It is not as if 1<x is evaluated to True and then
True<3 is evaluated to True !!! Each term is evaluated once.
Logical operators
There are three logical operators: and, or, and not. The semantics (meaning) of these operators is similar
to their meaning in English. For example, x > 0 and x < 10 is true only if x is greater than 0 and less than
10.
n%2 == 0 or n%3 == 0 is true if either or both of the conditions is true, that is, if the number
is divisible by 2 or 3.
The evaluation using the and and or operators follow these rules:
operators descriptions
not x Returns True if x is True, False otherwise
x and y Returns x if x is False, y otherwise
x or y Returns y if x is False, x otherwise
Membership operators
Identity operators
is evaluates to True if the variables on either side of the operator point to the same object and
False otherwise
is not evaluates to False if the variables on either side of the operator point to the same object
and True otherwise.
>>> p = 'hello'
>>> ps = p
>>> ps is p
True
Bitwise operators
When performing binary operations between 2 integers, there are first converted into binary numbers.
Let us show a few examples to explain the bitwise operations. The and operation between 2, the values
5 and 4 is actually the and operations between 1011 and 1001 binaries. It is therefore equal to 1001:
>>> 5 & 4
4
The left and right shifts can divide or multiply by power of 2 easily (integer conversion is made):
>>> 25 >> 2
6
>>> 25 << 2
100
operator
==
!=
and
or
To speed up boolean evaluations, Python uses short-circuit evaluations. It means that boolean
evaluation may stop if one of its expression is False. For instance the following expression is always
False
False and X
and X is never evaluated.
False or x
NameError Traceback (most recent call last)
<ipython-input-7-95b90495de85> in <module>()
----> 1 False or x
The floor division operator, //, divides two numbers and rounds down to an integer. For example,
suppose the run time of a movie is 105 minutes. You might want to know how long that is in hours.
Conventional division returns a floating-point number:
But we don’t normally write hours with decimal points. Floor division returns the integer
number of hours, rounding down:
To get the remainder, you could subtract off one hour in minutes:
>>> remainder = minutes - hours * 60
>>> remainder
45
An alternative is to use the modulus operator, %, which divides two numbers and returns
the remainder.
>>> remainder = minutes % 60
>>> remainder
45
The modulus operator is more useful than it seems. For example, you can check whether
one number is divisible by another—if x % y is zero, then x is divisible by y.