[Audio] CS177 Python Programming Chapter 8 Loop Structures and Booleans Adapted from John Zelle’s Book Slides.
[Audio] To understand the concepts of definite and indefinite loops as they are realized in the Python for and while statements. To understand the programming patterns interactive loop and sentinel loop and their implementations using a Python while statement. Objectives Python Programming, 2/e.
[Audio] To understand the programming pattern end-of-file loop and ways of implementing such loops in Python. To be able to design and implement solutions to problems involving loop patterns including nested loop structures..
[Audio] To understand the basic ideas of Boolean algebra and be able to analyze and write Boolean expressions involving Boolean operators..
[Audio] The for statement allows us to iterate through a sequence of values. for in : The loop index variable var takes on each successive value in the sequence, and the statements in the body of the loop are executed once for each value. For Loops: A Quick Review.
[Audio] We’ve run into some of these things before! A series of numbers could be handled by some sort of loop. If there are n numbers, the loop should execute n times. We need a running sum. This will use an accumulator..
[Audio] Input the count of the numbers, n Initialize sum to 0 Loop n times Input a number, x Add x to sum Output average as sum/n.
[Audio] # average1.py # A program to average a set of numbers # Illustrates counted loop with accumulator def main(): n = eval(input("How many numbers do you have? ")) sum = 0.0 for i in range(n): x = eval(input("Enter a number >> ")) sum = sum + x print("\nThe average of the numbers is", sum / n) Note that sum is initialized to 0.0 so that sum/n returns a float!.
[Audio] How many numbers do you have? 5 Enter a number >> 32 Enter a number >> 45 Enter a number >> 34 Enter a number >> 76 Enter a number >> 45 The average of the numbers is 46.4.
[Audio] One good use of the indefinite loop is to write interactive loops. Interactive loops allow a user to repeat certain portions of a program on demand. Remember how we said we needed a way for the computer to keep track of how many numbers had been entered? Let’s use another accumulator, called count. Interactive Loops.
[Audio] get the first data item while item is not the sentinel process the item get the next data item The first item is retrieved before the loop starts. This is sometimes called the priming read, since it gets the process started. If the first item is the sentinel, the loop terminates and no data is processed. Otherwise, the item is processed and the next one is read. Sentinel Loops.
[Audio] # average3.py # A program to average a set of numbers # Illustrates sentinel loop using negative input as sentinel def main(): sum = 0.0 count = 0 x = eval(input("Enter a number (negative to quit) >> ")) while x >= 0: sum = sum + x count = count + 1 x = eval(input("Enter a number (negative to quit) >> ")) print("\nThe average of the numbers is", sum / count).
[Audio] Enter a number (negative to quit) >> 32 Enter a number (negative to quit) >> 45 Enter a number (negative to quit) >> 34 Enter a number (negative to quit) >> 76 Enter a number (negative to quit) >> 45 Enter a number (negative to quit) >> -1 The average of the numbers is 46.4.
[Audio] At the top level, we will use a file-processing loop that computes a running sum and count. sum = 0.0 count = 0 line = infile.readline() while line != "": #update sum and count for values in line line = infile.readline() print("\nThe average of the numbers is", sum/count) Nested Loops.
[Audio] # average7.py # Computes the average of numbers listed in a file. # Works with multiple numbers on a line. import string def main(): fileName = input("What file are the numbers in? ") infile = open(fileName,'r') sum = 0.0 count = 0 line = infile.readline() while line != "": for xStr in line.split(","): sum = sum + eval(xStr) count = count + 1 line = infile.readline() print("\nThe average of the numbers is", sum / count).
[Audio] The and of two expressions is true exactly when both of the expressions are true. We can represent this in a truth table. Boolean Operators P Q P and Q T T T T F F F T F F F F.
[Audio] Boolean Expressions The or of two expressions is true when either expression is true. P Q P or Q T T T T F T F T T F F F.
[Audio] Boolean Operators The not operator computes the opposite of a Boolean expression. not is a unary operator, meaning it operates on a single expression. P not P T F F T.
[Audio] Boolean Algebra Algebra Boolean algebra a * 0 = 0 a and false == false a * 1 = a a and true == a a + 0 = a a or false == a and has properties similar to multiplication or has properties similar to addition 0 and 1 correspond to false and true, respectively..
[Audio] Anything ored with true is true: a or true == true Both and and or distribute: a or (b and c) == (a or b) and (a or c) a and (b or c) == (a and b) or (a and c) Double negatives cancel out: not(not a) == a DeMorgan’s laws: not(a or b) == (not a) and (not b) not(a and b) == (not a) or (not b).
[Audio] We can use these rules to simplify our Boolean expressions. while not(scoreA == 15 or scoreB == 15): #continue playing This is saying something like “While it is not the case that player A has 15 or player B has 15, continue playing.” Applying DeMorgan’s law: while (not scoreA == 15) and (not scoreB == 15): #continue playing.
[Audio] This becomes: while scoreA != 15 and scoreB != 15 # continue playing Isn’t this easier to understand? “While player A has not reached 15 and player B has not reached 15, continue playing.”.