Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python Loops and Iteration

Similar presentations


Presentation on theme: "Python Loops and Iteration"— Presentation transcript:

1 Python Loops and Iteration

2 Repeated Steps No Yes Program: n = 5 while n > 0 : print n
n = n – 1 print 'Blastoff!' Output: 5 4 3 2 1 Blastoff! print n n = n -1 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.

3 What is wrong with this loop?
An Infinite Loop n = 5 No Yes n > 0 ? print 'Lather' n = 5 while n > 0 : print 'Lather' print 'Rinse' print 'Dry off!' print 'Rinse' print 'Dry off!' What is wrong with this loop?

4 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 hello there > finished finished > done Done!

5 line = raw_input('> ') if line == 'done' : break print line
No Yes while True: line = raw_input('> ') if line == 'done' : break print line print 'Done!' True ? .... break ... print 'Done'

6 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"

7 A Simple Definite Loop for i in [5, 4, 3, 2, 1] : 5 print i 4
print 'Blastoff!' 5 4 3 2 1 Blastoff!

8 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!

9 The range() function (revisited)
x = range(5) print x [0, 1, 2, 3, 4] x = range(3, 7) [3, 4, 5, 6] x = range(10, 1, -2) [10, 8, 6, 4, 2] 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.

10 A Simple Definite Loop iterating over a range
7 6 5 4 3 2 1 Blastoff! for i in range(7, 0, -1) : print i print 'Blastoff!'

11 Looping through a Set print 'Before'
9 41 12 3 74 15 After print 'Before' for thing in [9, 41, 12, 3, 74, 15] : print thing print 'After'

12 Counting in a Loop Before 0 1 9 2 41 3 12 4 3 5 74 6 15 After 6
zork = 0 print 'Before', zork for thing in [9, 41, 12, 3, 74, 15] : zork = zork + 1 print zork, thing print 'After', zork 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.

13 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 9 9 50 41 62 12 65 3 139 74 154 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.

14 Finding the Average in a Loop
Before 0 0 1 9 9 4 65 3 After 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, sum / count An average just combines the counting and sum patterns and divides when the loop is done.

15 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.

16 Control flow tools for letter in "aeiou": print "vowel: ", letter for i in [1,2,3]: print i for i in range(0,3): The for loop has the following general form. 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. for var in sequence: statements vowel: a vowel: e vowel: i vowel: o vowel: u 1 2 3

17 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 ).

18 Count-Controlled Loop Example
Use a while loop to read the 100 blood pressures and find their total

19 Example Write a program to display all the numbers divisible by 5 in the range 0 to 5000.

20 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 ?


Download ppt "Python Loops and Iteration"

Similar presentations


Ads by Google