This preview shows pages 1–32. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Engineering Computation: Tutorial 08 Problem Solving with Computing, using the Python Language Questions from Week 07? Programming Exercises from Zelle textbook 1 pg. 262  263 # 2, 3, 5, 6, 12. 2 Whats in your toolbox? Print statement, string literals Basic calculator, integer division Variables Builtin functions Math library functions Nonbranching 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 Complex Conditions Testing and Debugging Exception Handling 3 Todays Topics: 1 While loops 2 Common Loop Patterns 3 Boolean Operators 4 Unit Testing 4 Loops Recall: the for loop for v a r i a b l e in l i s t : block 5 Loops Recall: the for loop for v a r i a b l e in l i s t : block The for loop executes a definite number of times. When the loop begins, Python knows how many times to go around (or iterate ) the body of the loop. This particular loop pattern is called a counted loop . 5 Loops The for loop is useful if you know how many iterations of the loop you need. 6 Loops The for loop is useful if you know how many iterations of the loop you need. Counted loops can also be written using a while loop. 6 Loops The for loop is useful if you know how many iterations of the loop you need. Counted loops can also be written using a while loop. There are situations where its impossible to know the number of iterations in advance. 6 Loops The for loop is useful if you know how many iterations of the loop you need. Counted loops can also be written using a while loop. There are situations where its impossible to know the number of iterations in advance. In such cases, a for loop can not be used. 6 Loops The for loop is useful if you know how many iterations of the loop you need. Counted loops can also be written using a while loop. There are situations where its impossible to know the number of iterations in advance. In such cases, a for loop can not be used. In such cases we can use a while loop ( conditional loop). 6 Loops while loops are sometimes called conditional loops because they iterate only as long as some condition is true. 7 Loops while loops are sometimes called conditional loops because they iterate only as long as some condition is true. the general form of the while loop is: while condition : block 7 While Loops Here is a trivial example: >>> i = 0 >>> while i < 4: i = i + 1 print i, 8 While Loops Here is a trivial example: >>> i = 0 >>> while i < 4: i = i + 1 print i, i i < 4 output 8 While Loops Here is a trivial example: >>> i = 0 >>> while i < 4: i = i + 1 print i, i i < 4 output 8 While Loops Here is a trivial example: >>> i = 0 >>> while i < 4: i = i + 1 print i, i i < 4 true output 8 While Loops Here is a trivial example: >>> i = 0 >>> while i < 4: i = i + 1 print i, i 1 i < 4 output 8 While Loops Here is a trivial example: >>> i = 0 >>> while i < 4: i = i + 1 print i, i 1 i < 4 output 1 8 While Loops Here is a trivial example: >>> i = 0 >>> while i < 4: i = i + 1 print i, i 1 i < 4 true output 1 8 While Loops Here is a trivial example: >>> i = 0 >>> while i < 4: i = i + 1 print i, i 2 i < 4 output 1 8 While Loops Here is a trivial example: >>> i = 0 >>> while i < 4: i = i + 1 print i, i 2 i < 4 output 1 2 8 While Loops Here is a trivial example: >>> i = 0 >>> while i < 4: i = i + 1 print i, i 2 i < 4 true output 1 2 8 While Loops Here is a trivial example: >>> i = 0 >>> while i < 4: i = i + 1 print i, i 3 i < 4 output 1 2 8 While Loops Here is a trivial example: >>> i = 0 >>> while i < 4: i = i + 1 print i, i 3 i < 4 output 1 2 3 8 While Loops Here is a trivial example: >>> i = 0 >>> while i < 4: i = i + 1 print i, i 3 i < 4 true output 1 2 3 8 While Loops Here is a trivial example: >>> i = 0 >>> while i < 4: i = i + 1 print i, i 4 i < 4 output 1 2 3 8 While Loops Here is a trivial example: >>> i = 0 >>> while i < 4: i = i + 1 print i, i 4 i < 4 output 1 2 3 4 8 While Loops Here is a trivial example: >>> i = 0 >>> while i < 4: i = i + 1 print i, i 4 i < 4 false output 1 2 3 4 8 While Loops Here is a trivial example: >>> i = 0 >>> while i < 4: i = i + 1 print i, i i < 4 output 1 2 3 4 8 For Loops in More Depth For loops can always be transformed into while loops....
View
Full
Document
This note was uploaded on 12/14/2010 for the course ENGINEERIN 1d04 taught by Professor Smith during the Spring '10 term at McMaster University.
 Spring '10
 SMITH

Click to edit the document details