L07 - Conditional Execution - Part 2
L07 - Conditional Execution - Part 2
Python Programming
Lecture – 7
c) False
d) True
The correct answer is
False
Short-circuit Evaluation
• Do not evaluate the second operand of binary short-circuit logical
operator if the result can be deduced from the first operand
• Also applies to nested logical operators
true
true
false false
Evaluates to true
Short circuit Evaluation
3 Factors for Expr Evaluation
• Precedence
• Applied to two different class of operators
• + and *, - and *, and and or, …
• Associativity
• Applied to operators of same class
• * and *, + and -, * and /, …
• Order
• Precedence and associativity identify the operands for each operator
• Not which operand is evaluated first
• Python evaluates expressions from left to right
• While evaluating an assignment, the right-hand side is evaluated before the left-
hand side.
Class Quiz
• What is the output of the following program:
y = 0.1*3
if y != 0.3:
print ('Launch a Missile')
else:
print ("Let's have peace")
Launch a Missile
Caution about Using Floats
• Representation of real numbers in a computer can not be exact
• Computers have limited memory to store data
• Between any two distinct real numbers, there are infinitely many real numbers.
• On a typical machine running Python, there are 53 bits of precision available for a Python
float
Caution about Using Floats
• The value stored internally for the decimal number 0.1 is the binary
fraction
0.00011001100110011001100110011001100110011001100110011010