EE 194/BIO 196: Modeling biological systems

Slides:



Advertisements
Similar presentations
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Advertisements

EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Reading and Writing Mathematical Proofs
If statements while loop for loop
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Python Conditionals chapter 5
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Control flow Ruth Anderson UW CSE 160 Spring
Control flow Ruth Anderson UW CSE 160 Winter
Sorting Algorithms. Algorithms, revisited What is an algorithm? Wikipedia Definition: an algorithm is a definite list of well-defined instructions for.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
Types CSCE 314 Spring 2016.
REPETITION CONTROL STRUCTURE
COMP 14 Introduction to Programming
Selection (also known as Branching) Jumail Bin Taliba by
Introduction To Repetition The for loop
ECS10 10/10
EGR 2261 Unit 4 Control Structures I: Selection
CS1371 Introduction to Computing for Engineers
Chapter 4: Control Structures
Algorithm Analysis CSE 2011 Winter September 2018.
Topic 5 for Loops -Arthur Schopenhauer
Design and Analysis of Algorithms
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
Teach A level Computing: Algorithms and Data Structures
Quicksort 1.
Ruth Anderson UW CSE 160 Spring 2018
Ruth Anderson UW CSE 160 Winter 2017
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Arrays, For loop While loop Do while loop
Unit 2 Programming.
Winter 2018 CISC101 11/19/2018 CISC101 Reminders
Introduction to Programming
Decision Structures, String Comparison, Nested Structures
CS190/295 Programming in Python for Life Sciences: Lecture 6
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Algorithm Discovery and Design
Ruth Anderson UW CSE 140 Winter 2014
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Michael Ernst UW CSE 140 Winter 2013
Coding Concepts (Basics)
Testing and Repetition
Problem Solving Designing Algorithms.
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
15-110: Principles of Computing
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Quicksort.
CISC101 Reminders All assignments are now posted.
For loops Taken from notes by Dr. Neil Moore
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
Instructor: Craig Duckett
CHAPTER 5: Control Flow Tools (if statement)
EE 194/BIO 196: Modeling,simulating and optimizing biological systems
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
EE 194/BIO 196: Modeling,simulating and optimizing biological systems
EE 194 / Bio 196: Modeling biological systems
Michael Ernst UW CSE 190p Summer 2012
EE 194/Bio 196: Modeling biological systems
Software Development Techniques
EE 194/BIO 196: Modeling,simulating and optimizing biological systems
Quicksort.
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

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 and #(i+1) are backwards then if (card[i] > card[i+1]) 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 10 6 8 10 6 [0] [1] [2] 10>6 is True U U 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 10 8 10 8 6 [0] [1] [2] 10>8 is True 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 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 Starting this loop all over again from the top 8 10 6 [0] [1] [2] 6>8 is False skip over the “if” code 1 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 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: We can rewrite it: if (it’s Sunday evening): do your homework if (it's not Sunday evening): go to sleep early We can rewrite it: else: “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 Example: 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 or False)): if (False): if (True and False and True): if (True and False and !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): if (time>22 && time<=24): 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 if (time<=22): 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): else: 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 As always, there’s a trick to doing it. Try this code instead. tmp=y y=x x=tmp 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 5 7 y It works  U 7 tmp EE 194/Bio 196 Joel Grodstein