t_07.4up - Questions from Week 06? Engineering Computation:...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Engineering Computation: Tutorial 07 Problem Solving with Computing, using the Python Language Questions from Week 06? Programming Exercises from Zelle textbook 1 pg. 228 # 3, 5, 11, 16, 17. 2 What’s in your toolbox? Print statement, string literals Basic calculator, integer division Variables Built-in functions Math library functions Non-branching programs Branching programs Python lists For loops Nested for loops Strings Converting between number system using Python Matplotlib Algorithms File Processing Graphics Functions Scope of Variables Comments 3 Today’s Topics: 1 Complex Conditions 2 Testing 3 Debugging 4 Exception Handling 4
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Recap: If Statements >>> from graphics import * >>> def pnAbsSlope(p1,p2): x1=p1.getX() y1=p1.getY() x2=p2.getX() y2=p2.getY() slope = (float(y2) - y1) / (float(x2) - x1) if slope > 0: print "Positive Slope" else: print "Negative Slope" slope = -slope return slope >>> slope = pnAbsSlope(Point(0,0), Point(4,0)) Negative Slope # ??? >>> slope -0.0 5 Point to Ponder What if we want our slope calculator to output ”Zero Slope”? 6 Recap: If Statements >>> from graphics import * >>> def pnAbsSlope(p1,p2): x1=p1.getX() y1=p1.getY() x2=p2.getX() y2=p2.getY() slope = (float(y2) - y1) / (float(x2) - x1) if slope > 0: print "Positive Slope" elif slope < 0: print "Negative Slope" slope = -slope else: print "Zero Slope" return slope >>> 7 Which branch(es) will be run? >>> def doIt(i): if i > 5: print "greater than 5" elif i > 2: print "greater than 2" elif i > -25: print "greater than -25" else: print "a very small number" >>> doIt(30) greater than 5 >>> 8
Background image of page 2
Relation Operators Python Math Meaning < < Less than < = Less than or equal to == = Equal to > = Greater than or equal to > > Greater than != 6 = Not equal to There are six relational operators in Python. 9 Review Statements in the form < expr >< relop >< expr > evaluate to True or False. [DEMO] 1 == 2 1 != 2 ”Zebra” < ”alpaca” ”a” > 4 ”eng1d04” == ”boring” 10 Logical Operators Python Name Description and logical AND If both operands are true then the condition is True. or logical OR If any of the operands are true then the condition is True. not logical NOT Reverses the logical state of the operand. If condition is True then Logical NOT operator will make it False. not has the highest precendence, followed by and , followed by or. 11 Examples for not >>> not True False >>> not False True >>> 3 == 3 True >>> not(3 == 3) False >>> 3 > 4 False >>> not(3 > 4) True >>> 12
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Examples for and >>> True and True True >>> True and False False >>> False and False False >>> (3 == 3) and (4 < 5) True >>> (3 == 3) and (3 > 4)
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

t_07.4up - Questions from Week 06? Engineering Computation:...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online