
Modeling Biological Systems with Loops in Spring 2018 at Tufts
Learn about loops in biological system modeling with examples like simple loops, running sums, and group activities to compute and print values. Understand the importance and application of loops in code repetition.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
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 1
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 2
Simple loop Simple code: for i in range(3): print ('Hello') This code prints out Hello Hello Hello what do you think? EE 194/Bio 196 Joel Grodstein 3
How it executes Simple code: for i in range(3): print ('Hello') Output: 0 1 U 2 i Hello Hello Hello EE 194/Bio 196 Joel Grodstein 4
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') print ('Hello') 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 5
What does this code do? Simple code: for i in range(3): print (i) 0 1 U 2 i Output: 0 1 2 EE 194/Bio 196 Joel Grodstein 6
How about this code? Simple code: for i in range (2,5): print (i) 2 3 U 4 i Output: 2 3 4 EE 194/Bio 196 Joel Grodstein 7
Running sums Sum the numbers from 2 to 4: sum=0 for i in range(2,5): sum = sum+i 2 3 U 4 i U 0 2 5 9 sum EE 194/Bio 196 Joel Grodstein 8
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 9
Multiply 1*2*3*4 fact=1 for i in range(2,5): fact = fact*i 2 3 U 4 i U 1 2 6 24 fact This is called factorial, and written as 4! EE 194/Bio 196 Joel Grodstein 10
Multiply 1*2*3**n fact=1 for i in range (1,n+1): fact=fact*i EE 194/Bio 196 Joel Grodstein 11
Triangle printing Here's a size-3 triangle: for i in range (1,4): print ('*'*i) 1 2 U 3 i Output: * ** *** EE 194/Bio 196 Joel Grodstein 12
Slight variations on for loop for i in range(4,10,2): for i in range(4,9,2): for i in range(10,5,-1): for i in range(10,9): for i in range(10,5,-2): # Counts 4,6,8 # Counts 4,6,8 # Counts 10,9,8,7,6 # Doesn t count! # Counts 10,8,6 EE 194/Bio 196 Joel Grodstein 13
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 14
How about this code? i=7 while (i is not a good password): print (i, 'does not work') i=i+1 print (i, 'is the password! ) Try 7: no good Try 8: no good 9 gets me in! 7 8 U 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 Output: 7 does not work 8 does not work 9 is the password! EE 194/Bio 196 Joel Grodstein 15
Group activity: dancing for u in range(2,4): clap u times for v in range (1,2,3): spin left v times nod your head Try the following dances: for i in range(2,7,2): for i in range(3): spin left spin right hop i%3 times i=10 j=1 while (j<i): spin left j times spin right j=j*2 leader=pick somebody while (leader has hand raised): for i in range(3): copy leader's move Code your own dance! EE 194/Bio 196 Joel Grodstein 16
Nested loops for u in range(2,4): clap u times for v in range (1,2,3): spin left v times nod your head 2 U 3 1 U u v EE 194/Bio 196 Joel Grodstein 17
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 18