220 likes | 231 Views
Explore if-then statements and logical expressions in Python programming with practical examples. Learn about logical types, Boolean operations, equality testing, difference between '=' and '==', and efficiency with nested if statements.
E N D
EE 194/BIO 196: Modeling biological systems Spring 2018 Tufts University Instructor: Joel Grodstein joel.grodstein@tufts.edu if then, logical types EE 194/Bio 196 Joel Grodstein
Demo a bubble sort with cards EE 194/Bio 196 Joel Grodstein
Card sort with if then • The 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. n_cards = 3 for pass in range(n_cards-1): for i in range(n_cards-1): if (card[i] > card[i+1]) if card #i and #(i+1) are backwards then swap them EE 194/Bio 196 Joel Grodstein
Follow the bouncing ball n_cards = 3 for pass in range(n_cards-1): for i in range(n_cards-1): if (card[i] > card[i+1]): swap them 6 8 10 6 10 [0] [1] [2] 10>6 is True U 0 U 0 U 3 n_cards pass i card EE 194/Bio 196 Joel Grodstein
Follow the bouncing ball n_cards = 3 for pass in range(n_cards-1): for i in range(n_cards-1): if (card[i] > card[i+1]): swap them 8 10 8 10 6 [0] [1] [2] 10>8 is True 0 0 1 3 The “for i” loop is now done n_cards pass i card But the “for pass” loop is not done EE 194/Bio 196 Joel Grodstein
Follow the bouncing ball Starting this loop all over again from the top n_cards = 3 for pass in range(n_cards-1): for i in range(n_cards-1): if (card[i] > card[i+1]): swap them 8 10 6 [0] [1] [2] 6>8 is False skip over the “if” code 1 0 1 3 The “for i” loop is not done n_cards pass i card EE 194/Bio 196 Joel Grodstein
Follow the bouncing ball n_cards = 3 for pass in range(n_cards-1): for i in range(n_cards-1): if (card[i] > card[i+1]): swap them 8 10 6 [0] [1] [2] 8>10 is False skip over the “if” code 1 0 1 3 The “for i=” loop is done n_cards pass i card The “pass=” loop is done Finally finished!!! EE 194/Bio 196 Joel Grodstein
if then – details • The format is simple: if (condition): statement… • The usual Python note: if there are more than 1 “statement”s, they must all have the same indentation • As usual, there are several variations: • We can make the condition fairly complex • There is an “else”. • Let's look at both of these. EE 194/Bio 196 Joel Grodstein
if then else • Here’s some code with an obvious use: if (it’s Sunday evening): do your homework if (it's not Sunday evening): go to sleep early • We can rewrite it: if (it’s Sunday evening): do your homework else: go to sleep early • “Else” replaces the inverted condition • Gain in clarity EE 194/Bio 196 Joel Grodstein
More complex conditions • We don’t have homework due every week. if (it’s Sunday evening and there's homework due) do your homework else: go to sleep early • The condition can have 'and' and 'or', or really be any arbitrary expression. • But how arbitrary can an expression get, anyway? • In fact, very arbitrary. Without 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! EE 194/Bio 196 Joel Grodstein
Logical expressions • False and True are the simplest expressions. • if (False) → do not execute the statements • if (True) → do execute the statements • <, >, <=, >= work as expected • 3<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=3 if (a<4): statements These statements do happen, since 3<4. EE 194/Bio 196 Joel Grodstein
Testing equality & inequality • Testing for equality is a bit weird • if (3=4) → error • if (3==4) → evaluates to false. • Single '=' is used only for assignment to a variable. • Testing for equality is '=='; test for inequality is ‘!='. • Example: a=3 b=4 if (a==b): statements These statements do not happen, since 3 is not equal to 4. EE 194/Bio 196 Joel Grodstein
AND, OR • expression and expression is an expression • Ditto 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))): So in the end, this big expression just evaluates to 'False' if ((3<4) and (4==5) and !((6==5)or(7==6))): if (True and False and !False): if (True and False and !(False or False)): if (True and False and True): if (False): EE 194/Bio 196 Joel Grodstein
elif • Sometimes 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° • if (time <= 10): • temp=55 • if (time>10 && time<=11): • temp=70 • if (time>11 && time<=22): • temp=55 • if (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 EE 194/Bio 196 Joel Grodstein
Nested if • How about this? • Pros and cons? • More obvious that it works • More efficient • But the indentation is ugly • if (time <= 10): • temp=55 • else: • if (time<=11): • temp=70 • else: • if (time<=22): • temp=55 • else: • temp=70 These must line up These too And these EE 194/Bio 196 Joel Grodstein
Elif • The solution is elif • Many (not all) languages have it. • Code is more obvious and elegant • We won’t need this until the Manduca homework. • if (time <= 10): • temp=55 • elif (time<=11): • temp=70 • elif (time<=22): • temp=55 • else: • temp=70 EE 194/Bio 196 Joel Grodstein
Group activity • What do each of these evaluate to? a=3 b=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)) True True False True EE 194/Bio 196 Joel Grodstein
Group activity • What do each of these print? Assume a=3 and b=2 • if (a==3) and (b==2): • print ('yes') yes • if (a==4): • if (b==2): • print ('42') • else: • print ('4 not 2') nothing • if (a==4) or (b==2): • print ('yes') • else: • print ('no') yes • for i in range(2:6): • if (i>4): • print (i, '*', i, '=', i*i) 5*5=25 • if (a==3): • if (b==2): • print ('32') • else: • print ('4 not 2') 32 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… • Prints all of the even numbers in an array • Print all the elements of array1 that are not in array2 • Makes a new array with “pos”, “neg” or “zero” based on a first array of numbers • Determines the indices of the largest and smallest numbers from an array • Checks whether a specified value is contained in an array • Tests if a number N is within 100 of 1000 or 2000. • Prints all divisors of a given number • Creates 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 exercise • Take 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 HW3 EE 194/Bio 196 Joel Grodstein
Swapping • Let’s say we have two variables x and y, and we want to swap their values. Consider this code: x=y y=x y is 7, so now x is too. 7 5 x is 7, so copy that to y. But y already was 7, so that does nothing. x 7 y 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 EE 194/Bio 196 Joel Grodstein
Swapping – take 2 tmp=y y=x x=tmp As always, there’s a trick to doing it. Try this code instead. y is 7, so now tmp is too. 7 5 x is 5, so copy that to y. x tmp=7, so copy 7 to x 7 5 y It works U 7 tmp EE 194/Bio 196 Joel Grodstein