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

L07 - Conditional Execution - Part 2

This document discusses conditional execution in Python programming. It covers nested conditionals, short-circuit evaluation, and factors that determine expression evaluation like precedence, associativity, and order of evaluation from left to right. It also discusses potential issues with using floats for comparisons due to representation approximations, and debugging syntax errors in Python.

Uploaded by

aamirneyazi12
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)
25 views

L07 - Conditional Execution - Part 2

This document discusses conditional execution in Python programming. It covers nested conditionals, short-circuit evaluation, and factors that determine expression evaluation like precedence, associativity, and order of evaluation from left to right. It also discusses potential issues with using floats for comparisons due to representation approximations, and debugging syntax errors in Python.

Uploaded by

aamirneyazi12
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/ 11

Branch - CSE

Python Programming

Lecture – 7

Conditional Execution – Part 2


By

Dr. Shobhit Tyagi


Assistant Professor
Department of Computer Science and Engineering
School of Engineering & Technology
Nested conditionals (Contd.)
• One conditional can also be nested within another. We could have written the three-
branch example like this:
Class Quiz
• What is the value of expression:

(5<2) and (3/0 > 1)


a) Run time crash/error

b) I don’t know / I don’t care

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

not( (2>5) and (3/0 > 1) ) or (4/0 < 2)

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

• Equivalent to decimal value


0.1000000000000000055511151231257827021181583404541015625

• Approximation is similar to decimal approximation 1/3 =


0.333333333...
• No matter how many digits you use, you have an approximation
Debugging
• Python displays the traceback when an error occurs and contains a lot of information,
but it can be overwhelming. The useful parts are usually:
• What kind of error it was, and
• Where it occurred.
• Syntax errors are usually easy to find, but there are a few “catches”. Whitespace errors
can be tricky because spaces and tabs are invisible, and we ignore them.

Output from Python IDLE 3.10.1

Output from Python 3.8 (Jupyter Notebook)


• Here, the 2nd line is idented by one space. But the error message points to y, which is
misleading. Python IDLE 3.10.1 points to the exact error spot now.
THANK YOU

01/17/2024 Python Programming 11

You might also like