0% found this document useful (0 votes)
10 views5 pages

Unit III Lecture Hour 1

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

Unit III Lecture Hour 1

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

UNIT III CONTROL FLOW, FUNCTIONS

Lecture Hour 1 - Boolean values and operators

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.

The not keyword can also be used to inverse a boolean type.

>>> not True


False

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: “”, (), [], {}

Example of a class with type set to False:

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

Chaining comparison operators

Comparison operators can be chained. Consider the following examples:

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

 and and or evaluates expression from left to right.


 with and, if all values are True, returns the last evaluated value. If any value is false, returns the
first one.
 or returns the first True value. If all are False, returns the last value

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

 in evaluates to True if it finds a variable in a specified sequence and false otherwise.


 not in evaluates to False if it finds a variable in a sequence, True otherwise.

>>> 'good' in 'this is a great example'


False
>>> 'good' not in 'this is a great example'
True

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

Bitwise operators are used to compare integers in their binary formats.

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

bitwise operators descriptions


>> bitwise left shift
<< bitwise rightshift
& bitwise and
^ bitwise xor
bitwise or
~ bitwise not

The left and right shifts can divide or multiply by power of 2 easily (integer conversion is made):

>>> 25 >> 2
6
>>> 25 << 2
100

Here is the precedence table for the boolean operators only

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

NameError: name 'x' is not defined

Floor division and modulus

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:

>>> minutes = 105


>>> minutes / 60
1.75

But we don’t normally write hours with decimal points. Floor division returns the integer
number of hours, rounding down:

>>> minutes = 105


>>> hours = minutes // 60
>>> hours
1

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.

You might also like