/
EE 194/Bio 196 Joel EE 194/Bio 196 Joel

EE 194/Bio 196 Joel - PowerPoint Presentation

jane-oiler
jane-oiler . @jane-oiler
Follow
343 views
Uploaded On 2019-11-07

EE 194/Bio 196 Joel - PPT Presentation

EE 194Bio 196 Joel Grodstein EE 194BIO 196 Modeling biological systems Spring 2018 Tufts University Instructor Joel Grodstein joelgrodsteintuftsedu if then logical types Demo a bubble sort with cards ID: 764440

194 joel bio 196 joel 194 196 bio cards card false grodstein time true temp range pass print amp

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "EE 194/Bio 196 Joel" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

EE 194/Bio 196 Joel Grodstein EE 194/BIO 196: Modeling biological systems Spring 2018 Tufts University Instructor: Joel Grodstein joel.grodstein@tufts.edu if then, logical types

Demo a bubble sort with cardsEE 194/Bio 196 Joel Grodstein

Card sort with if thenThe code below is for a card sort. It already has an "if"And hopefully it even makes sense .Let's just fix the format a bit.EE 194/Bio 196 Joel Grodsteinn_cards = 3for pass in range(n_cards-1):for i in range(n_cards-1):swap themif (card[i ] > card[i+1])if card # i and #(i+1) are backwards then

Follow the bouncing balln_cards = 3for pass in range(n_cards-1):for i in range(n_cards-1):if (card[i] > card[i+1]):swap themEE 194/Bio 196 Joel Grodstein [0] [1] [2] n_cards pass i card 3 0 0 U U U 10 6 8 10>6 is True 10 6

Follow the bouncing balln_cards = 3for pass in range(n_cards-1):for i in range(n_cards-1):if (card[i] > card[i+1]):swap themEE 194/Bio 196 Joel Grodstein 3 0 0 1 8 10 8 10>8 is True 10 6 The “for i ” loop is now done But the “for pass” loop is not done [0] [1] [2] n_cards pass i card

Follow the bouncing balln_cards = 3for pass in range(n_cards-1):for i in range(n_cards-1):if (card[i] > card[i+1]):swap themEE 194/Bio 196 Joel Grodstein 3 1 0 1 8 10 Starting this loop all over again from the top 6 The “for i ” loop is not done 6>8 is False skip over the “if” code [0] [1] [2] n_cards pass i card

Follow the bouncing balln_cards = 3for pass in range(n_cards-1):for i in range(n_cards-1):if (card[i] > card[i+1]):swap themEE 194/Bio 196 Joel Grodstein 3 1 0 1 8 10 6 The “for i =” loop is done The “pass=” loop is done 8>10 is False skip over the “if” code Finally finished!!! [0] [1] [2] n_cards pass i card

if then – detailsThe format is simple:if (condition):statement…The usual Python note: if there are more than 1 “statement”s, they must all have the same indentationAs usual, there are several variations:We can make the condition fairly complexThere is an “else”.Let's look at both of these.EE 194/Bio 196 Joel Grodstein

if then elseHere’s some code with an obvious use:if (it’s Sunday evening):do your homeworkif (it's not Sunday evening):go to sleep earlyWe can rewrite it:if (it’s Sunday evening):do your homeworkelse:go to sleep earlyEE 194/Bio 196 Joel Grodstein “Else” replaces the inverted condition Gain in clarity

More complex conditionsWe don’t have homework due every week.if (it’s Sunday evening and there's homework due)do your homeworkelse:go to sleep earlyThe condition can have 'and' and 'or', or really be any arbitrary expression.But how arbitrary can an expression get, anyway?In fact, very arbitrary.EE 194/Bio 196 Joel GrodsteinWithout the 'else', this would have been “if ((it's not Sunday evening) or (there's no homework due))”. That's a bit of a mess!

Logical expressionsFalse and True are the simplest expressions.if (False) → do not execute the statementsif (True) → do execute the statements<, >, <=, >= work as expected3<4 evaluates to True. 3>=4 evaluates to False.And then see the case above.So “if (3<4)” becomes “if (True)”, which executes the subsequent statements.You can replace “3” with any variable that evaluates to 3.a=3if (a<4):statementsEE 194/Bio 196 Joel Grodstein These statements do happen, since 3<4.

Testing equality & inequalityTesting for equality is a bit weirdif (3=4) → errorif (3==4) → evaluates to false.Single '=' is used only for assignment to a variable.Testing for equality is '=='; test for inequality is ‘!='.Example:a=3b=4if (a==b):statementsEE 194/Bio 196 Joel GrodsteinThese statements do not happen, since 3 is not equal to 4.

AND, ORexpression and expression is an expressionDitto for or.And you can do this recursively…Example:a=3; b=4; c=5; d=6; e=7;if ((a<4) and (b==c) and !((d==5) or (e==6))):EE 194/Bio 196 Joel Grodsteinif ((3<4) and (4==5) and !((6==5)or(7==6))):if (True and False and !(False or False)):if (True and False and !False): if (True and False and True): if (False): So in the end, this big expression just evaluates to 'False'

elifSometimes you have a sequence of conditions:If it's midnight-10am, keep the house at 55°If 10-11am, 70°11am-10pm, 55 °10pm-midnight, 71°EE 194/Bio 196 Joel Grodsteinif (time <= 10):temp=55if (time>10 && time<=11):temp=70if (time>11 && time<=22):temp=55if (time>22 && time<=24):temp=70 The program on the right works, but it’s inefficient, since it tests the last 3 " if"s even when the first is true. it’s easy to mis -type the conditions

Nested ifHow about this?Pros and cons?More obvious that it worksMore efficientBut the indentation is uglyEE 194/Bio 196 Joel Grodsteinif (time <= 10):temp=55else:if (time<=11):temp=70else:if (time<=22):temp=55else:temp=70 These must line up These too And these

ElifThe solution is elifMany (not all) languages have it.Code is more obvious and elegantWe won’t need this until the Manduca homework.EE 194/Bio 196 Joel Grodsteinif (time <= 10):temp=55elif (time<=11):temp=70elif (time<=22):temp=55else:temp=70

Group activityWhat do each of these evaluate to?a=3b=2(a==3) and (b==2)(a==4) or (b==2)(a*b==6) and (a+b==6)(a*b==6) and ((a+b==6) or (1==1))EE 194/Bio 196 Joel GrodsteinTrue True False True

Group activityWhat do each of these print? Assume a=3 and b=2EE 194/Bio 196 Joel Grodsteinyesif (a==3) and (b==2):print ('yes')yes if (a==4) or (b==2): print ('yes') else: print ('no') if (a==3): if (b==2): print ('32') else: print ('4 not 2') 32 if (a==4): if (b==2): print ('42') else: print ('4 not 2') nothing for i in range(2:6): if ( i >4): print ( i , '*', i , '=', i * i ) 5*5=25

Follow-up activitiesTry the examples from this lecture yourselfVary them, or even mis-type some to see what happensMore exercises. Write a program that…Prints all of the even numbers in an arrayPrint all the elements of array1 that are not in array2Makes a new array with “pos”, “neg” or “zero” based on a first array of numbersDetermines the indices of the largest and smallest numbers from an arrayChecks whether a specified value is contained in an arrayTests if a number N is within 100 of 1000 or 2000.Prints all divisors of a given numberCreates a 5-element array of random integers between 1 and 10, and then sums up only those elements that are 3 or higher EE 194/Bio 196 Joel Grodstein

Another group exerciseTake an array p. Multiply every element by a random number r, and then bound them all to be ≥0 and ≤1.You may want to Google “numpy clip”This will be useful for HW3EE 194/Bio 196 Joel Grodstein

Swappingx=yy=xEE 194/Bio 196 Joel Grodsteiny is 7, so now x is too.x is 7, so copy that to y. But y already was 7, so that does nothing. Once we set x to 7, there was no longer any variable anywhere with a value of 5. So there is no way to assign y=5  x 7 5 Let’s say we have two variables x and y , and we want to swap their values. Consider this code: y 7

Swapping – take 2tmp=yy=xx=tmpEE 194/Bio 196 Joel Grodsteiny is 7, so now tmp is too.x is 5, so copy that to y. tmp=7, so copy 7 to x x 7 5 As always, there’s a trick to doing it. Try this code instead. y 7 5 tmp U 7 It works 