EE 194/Bio 196: Modeling biological systems

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Simple Python Loops Sec 9-7 Web Design.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
Do-while loop Syntax do statement while (loop repetition condition)
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
For loops in programming Assumes you have seen assignment statements and print statements.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Repetition Statements while and do while loops
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Repetition: Definite Loops Sec 53 Web Design. Objectives The Student will: Understand loops and why they are used Understand definitive loops Know how.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Control flow Ruth Anderson UW CSE 160 Spring
COMP Loop Statements Yi Hong May 21, 2015.
Control flow Ruth Anderson UW CSE 160 Winter
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Loops BIS1523 – Lecture 10.
What to do when a test fails
Printing Lines of Asterisks
Programming 101 Programming for non-programmers.
Repetition-Counter control Loop
( Iteration / Repetition / Looping )
Web Programming– UFCFB Lecture 16
Gaming with conditionals
Ruth Anderson UW CSE 160 Spring 2018
Ruth Anderson UW CSE 160 Winter 2017
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Learning to Program in Python
Repetition: Definite Loops
Count Controlled Loops (Nested)
Ruth Anderson UW CSE 140 Winter 2014
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Michael Ernst UW CSE 140 Winter 2013
Module 4 Loops.
Repetition: Definite Loops
Repetition: Definite Loops
Computing Fundamentals
Suppose I want to add all the even integers from 1 to 100 (inclusive)
Gaming with conditionals
ECS15 while.
For loops Taken from notes by Dr. Neil Moore
Loops.
EE 194/BIO 196: Modeling,simulating and optimizing biological systems
Python While Loops.
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
Looping Structures.
REPETITION Why Repetition?
Control flow: Loops UW CSE 160.
Condition Switches between actions
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

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