Python Loops and Iteration Repeated Steps n5 No
- Slides: 20
Python Loops and Iteration
Repeated Steps n=5 No n>0? Yes Program: n=5 while n > 0 : print n n=n– 1 print 'Blastoff!' print n Output: 5 4 3 n = n -1 2 1 Blastoff! 0 print 'Blastoff' Loops (repeated steps) have iteration variables that change each time through a loop. Often these iteration variables go through a sequence of numbers. print n
n=5 No Yes n>0? print 'Lather' print 'Rinse' An Infinite Loop n=5 while n > 0 : print 'Lather' print 'Rinse' print 'Dry off!' What is wrong with this loop?
Breaking Out of a Loop • The break statement ends the current loop and jumps to the statement immediately following the loop while True: line = raw_input('> ') if line == 'done' : break print line print 'Done!' > hello there > finished > done Done!
while True: line = raw_input('> ') if line == 'done' : break print line print 'Done!' No True ? Yes. . break. . . print 'Done' http: //en. wikipedia. org/wiki/Transporter_(Star_Trek)
Definite Loops • • Quite often we have a list of items of the lines in a file effectively a finite set of things We can write a loop to run the loop once for each of the items in a set using the Python for construct These loops are called "definite loops" because they execute an exact number of times We say that "definite loops iterate through the members of a set"
A Simple Definite Loop for i in [5, 4, 3, 2, 1] : print i print 'Blastoff!' 5 4 3 2 1 Blastoff!
A Simple Definite Loop friends = ['Joseph', 'Glenn', 'Sally'] for friend in friends : print 'Happy New Year: ', friend print 'Done!' Happy New Year: Joseph Happy New Year: Glenn Happy New Year: Sally Done!
The range() function (revisited) • • • range() is a built-in function that allows you to create a sequence of numbers in a range Very useful in “for” loops which are discussed later in the Iteration chapter Takes as an input 1, 2, or 3 arguments. See examples. x = range(5) print x [0, 1, 2, 3, 4] x = range(3, 7) print x [3, 4, 5, 6] x = range(10, 1, -2) print x [10, 8, 6, 4, 2]
A Simple Definite Loop iterating over a range 7 for i in range(7, 0, -1) : print i print 'Blastoff!' 6 5 4 3 2 1 Blastoff!
Looping through a Set print 'Before' for thing in [9, 41, 12, 3, 74, 15] : print thing print 'After' Before 9 41 12 3 74 15 After
Counting in a Loop zork = 0 print 'Before', zork for thing in [9, 41, 12, 3, 74, 15] : zork = zork + 1 print zork, thing print 'After', zork Before 0 19 2 41 3 12 43 5 74 6 15 After 6 To count how many times we execute a loop we introduce a counter variable that starts at 0 and we add one to it each time through the loop.
Summing in a Loop zork = 0 print 'Before', zork for thing in [9, 41, 12, 3, 74, 15] : zork = zork + thing print zork, thing print 'After', zork Before 0 99 50 41 62 12 65 3 139 74 15 After 154 To add up a value we encounter in a loop, we introduce a sum variable that starts at 0 and we add the value to the sum each time through the loop.
Finding the Average in a Loop count = 0 sum = 0 print 'Before', count, sum for value in [9, 41, 12, 3, 74, 15] : count = count + 1 sum = sum + value print count, sum, value print 'After', count, sum / count Before 0 0 199 2 50 41 3 62 12 4 65 3 5 139 74 6 154 15 After 6 154 25 An average just combines the counting and sum patterns and divides when the loop is done.
Filtering in a Loop print 'Before' for value in [9, 41, 12, 3, 74, 15] : if value > 20: print 'Large number', value print 'After' Before Large number 41 Large number 74 After We use an if statement in the loop to catch / filter the values we are looking for.
Control flow tools The for loop has the following general form. for letter in "aeiou": print "vowel: ", letter for i in [1, 2, 3]: print i for i in range(0, 3): print i for var in sequence: statements If a sequence contains an expression list, it is evaluated first. Then, the first item in the sequence is assigned to the iterating variable var. Next, the statements are executed. Each item in the sequence is assigned to var, and the statements are executed until the entire sequence is exhausted. For loops may be nested with other control flow tools such as while loops and if statements. vowel: a vowel: e vowel: i vowel: o vowel: u 1 2 3 0 1 2
Example write a program that reads in the grades of 50 students in a course (out of 100 points each ) and then count the number of A students ( grade > 85 ) and the number of B students (grade > 75 ).
Count-Controlled Loop Example Use a while loop to read the 100 blood pressures and find their total
Example Write a program to display all the numbers divisible by 5 in the range 0 to 5000. 19
Example Assume you put 1000 pounds in a projects that returns a profit of about 5% per year. How long will it take for your money to double ? Assume you put 5000 pounds in a projects that returns a profit of about 10% per year. How much money will you have in 5 years ?
- Perulangan python
- Nested loops python
- Python sentinel loop
- A repeated back and forth motion
- While loops and if-else structures
- Node electrical circuit
- Turtle small basic
- Movements are also sometimes called axial movements
- Python next steps
- What is a procedure python
- Sequencing selection and iteration
- Sequencing selection and iteration
- Pseudocode examples
- Similarities between iteration and recursion
- Repeated percentage change
- Conflict of get up and bar the door
- Flow volume loop obstructive
- Reddish loops of gas that connect sunspot regions
- Matlab nested for loop
- Types of loops in matlab
- Programming puzzle