EE 194/Bio 196: Modeling biological systems Spring 2018 Tufts University Instructor: Joel Grodstein joel.grodstein@tufts.edu Loops EE 194/Bio 196 Joel Grodstein
What is a loop? A piece of code that you repeat several times. Why are loops useful? Do you ever do things more than once in real life? EE 194/Bio 196 Joel Grodstein
Simple loop Simple code: This code prints out for i in range(3): print ('Hello') This code prints out Hello what do you think? EE 194/Bio 196 Joel Grodstein
How it executes Simple code: Output: for i in range(3): print ('Hello') Output: U 2 1 i Hello Hello Hello EE 194/Bio 196 Joel Grodstein
Is this really useful? Printing 'Hello' 3 times isn't really something we need to do too often. Even if we did, this would work too: print ('Hello') Printing 'Hello' 1000 times without a loop would be a bit unwieldy… but again, perhaps not that common. EE 194/Bio 196 Joel Grodstein
What does this code do? Simple code: Output: for i in range(3): print (i) Output: U 2 1 i 1 2 EE 194/Bio 196 Joel Grodstein
How about this code? Simple code: Output: for i in range (2,5): print (i) Output: U 4 3 2 i 2 3 4 EE 194/Bio 196 Joel Grodstein
Running sums Sum the numbers from 2 to 4: sum=0 for i in range(2,5): sum = sum+i 3 4 U 2 i 2 9 U 5 sum EE 194/Bio 196 Joel Grodstein
Group activity Can you write code to compute 1*2*3*4? Can you write code to compute 1*2*3*…*n? Can you write code to print * ** *** **** ***** EE 194/Bio 196 Joel Grodstein
Multiply 1*2*3*4 fact=1 for i in range(2,5): fact = fact*i U 3 4 2 i 24 2 6 U 1 fact This is called “factorial,” and written as 4! EE 194/Bio 196 Joel Grodstein
Multiply 1*2*3*…*n fact=1 for i in range (1,n+1): fact=fact*i EE 194/Bio 196 Joel Grodstein
Triangle printing Here's a size-3 triangle: Output: for i in range (1,4): print ('*'*i) Output: U 3 2 1 i * ** *** EE 194/Bio 196 Joel Grodstein
Slight variations on for loop for i in range(4,10,2): # Counts 4,6,8 for i in range(4,9,2): # Counts 4,6,8 for i in range(10,5,-1): # Counts 10,9,8,7,6 for i in range(10,9): # Doesn’t count! for i in range(10,5,-2): # Counts 10,8,6 EE 194/Bio 196 Joel Grodstein
while loop While loop looks like this: while (condition): statements… It keeps executing the statements, in order, until the condition becomes false. EE 194/Bio 196 Joel Grodstein
How about this code? Output: i=7 while (i is not a good password): print (i, 'does not work') i=i+1 print (i, 'is the password!’) Output: Try 7: no good Try 8: no good 9 gets me in! U 8 7 9 i How do we know that “i=i+1” is part of the same loop as the “print” above it? The indentation! Ditto for a ‘for’ loop 7 does not work 8 does not work 9 is the password! EE 194/Bio 196 Joel Grodstein
Group activity: dancing Try the following dances: for u in range(2,4): clap u times for v in range (1,2,3): spin left v times nod your head for i in range(2,7,2): spin right hop i%3 times for i in range(3): spin left leader=pick somebody while (leader has hand raised): for i in range(3): copy leader's move i=10 j=1 while (j<i): spin left j times spin right j=j*2 Code your own dance! EE 194/Bio 196 Joel Grodstein
Nested loops U 3 2 U 1 u v for u in range(2,4): clap u times for v in range (1,2,3): spin left v times nod your head U 3 2 U 1 u v EE 194/Bio 196 Joel Grodstein
Follow-up activities Try the examples from this lecture yourself Vary them, or even mis-type some to see what happens More exercises. Write a program that… Computes the future value of a specified principal amount, rate of interest, and a number of years Sums the first n positive integers. Takes a positive integer and returns the sum of the cube of all the positive integers smaller than the specified number Draws an N-by-N game board as shown in http://www.practicepython.org/exercise/2014/12/27/24-draw-a-game-board.html . Despite what the web site says, you do not need to write your own functions. EE 194/Bio 196 Joel Grodstein