Loops ! We've seen variables change in-place before: [ x*6 for x in range(8) ] [ 0, 6, 12, 18, 24, 30, 36, 42 ] remember range ?

Slides:



Advertisements
Similar presentations
Two motion and change: programming with imperatives.
Advertisements

2/7/2008. >>> Overview * boolean * while * random * tuples.
Introduction to Computing Science and Programming I
CS0004: Introduction to Programming Repetition – Do Loops.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
Complexity (Running Time)
CSE 113 Week 5 February , Announcements  Module 2 due 2/15  Exam 3 is on 2/15  Module 3 due 2/22  Exam 4 is on 2/25  Module 4 due 2/29.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while.
Guide to Programming with Python
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
Programming for Engineers in Python Sawa 2015 Lecture 2: Lists and Loops 1.
Lists Computers and Programming. Agenda What is a list? How to access elements in the list? The for statement Operations on lists Looping with.
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
8 For-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming 1.
The building blocks of functional computing data, sequences conditionals recursion CS 121 today List Comprehensions map and applications.
Xin Liu Feb 11, * Part 1 1. What value does s have, after this code is run? s = '*' s = s + s s = s + s + s (A) '**' (B) '***' (C) '****' (D) '*****'
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Roles of Variables with Examples in Python ® © 2014 Project Lead The Way, Inc.Computer Science and Software Engineering.
IS 313: Putting loops to work for you What's next? [ 1, 11, 21, 1211, , ? ] [ -35, -24, -13, -2, 9, 20, 31, ? ] [ 2, 22, 222, ? ] [ 26250, 5250,
EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print.
Question : How many different ways are there to climb a staircase with n steps (for example, 100 steps) if you are allowed to skip steps, but not more.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Comprehending List Comprehensions
EECS 110: Lec 10: Definite Loops and User Input Aleksandar Kuzmanovic Northwestern University
Code Compression the benefits of looping... Today in CS 5 HW 4 - (3 problems) M/T sections W/Th sections due Sunday, 9/26 at midnight due Monday, 9/27.
GCSE Computing: Programming GCSE Programming Remembering Python.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
Spring  Problems:  Mandelbrot set  Time Travel Securities, Inc.  Pi from pie  Extra Credits:  Sequence Sums.
CS 101 – Oct. 15 Common elements in solutions –Types of variables –Traversing values in a list –Assigning vs. checking if equal –Printing things on the.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
H ASKELL Session 2 Ronald L. Ramos Proglan DLS-CSB 2 nd Term SY
 See pages 65 – 67 in your book  While loops are all around us ◦ Shampoo  Rinse, Lather, Repeat  While (Condition Is True) : ◦ Execute a block of.
Python Flow of Control CS 4320, SPRING Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
EECS 110: Lec 10: Definite Loops and User Input
CSc 110, Autumn 2017 Lecture 13: Cumulative Sum and Boolean Logic
EECS 110: Lec 17: Review for the Final Exam
EECS 110: Lec 11: Indefinite Loops and Program Design
Printing Lines of Asterisks
CS 115 Lecture 8 Structured Programming; for loops
CS5 Stylin' !.
While Loops in Python.
Counted Loops.
The CS 5 Black Post Penguins Invade Dormitory
Looping.
Arrays, For loop While loop Do while loop
EECS 110: Lec 10: Definite Loops and User Input
#31 He was seized by the fidgets,
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
Lecture 8:The For Loop AP Computer Science Principles.
Flowcharts and Pseudo Code
For loops Taken from notes by Dr. Neil Moore
More While Loops.
Python While Loops.
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
List Comprehensions Problem: given a list of prices, generate a new list that has a 20% discount to each. Formally: input: list of old prices; output:
While Loops in Python.
REPETITION Why Repetition?
COMPUTING.
Lecture 6 - Recursion.
Presentation transcript:

Loops ! We've seen variables change in-place before: [ x*6 for x in range(8) ] [ 0, 6, 12, 18, 24, 30, 36, 42 ] remember range ?

for e! for x in range(8): print('x is', x) print 'Phew!' x is assigned each value from this sequence the BODY or BLOCK of the for loop runs with that x Code AFTER the loop will not run until the loop is finished LOOP back to step 1 for EACH value in the list

four on for for x in range(8): print('x is', x) factorial function? sum the list? construct the list?

Fact with for def fact( n ): answer = 1 for x in range(n): answer = answer * x return answer

Fact with for def fact( n ): answer = 1 for x in range(1,n+1): answer = answer * x return answer

Accumulating an answer… def sum( L ): """ returns the sum of L's elements """ sum = 0 for x in L: sum = sum + x return sum Finding the sum of a list: Accumulator! shortcuts? vs. recursion? sum every OTHER element?

Shortcut Shortcuts for changing variables: age = 38 age = age + 1 age += 1 #shortcut for age = age + 1

Two kinds of for loops Element-based Loops sum = 0 for x in L: sum += x L = [ 42, -10, 4 ] x "selfless"

Two kinds of for loops Element-based Loops L = [ 42, -10, 4 ] x i 0 12 Index-based Loops sum = 0 for x in L: sum += x sum = 0 for i in : sum +=

Two kinds of for loops Element-based Loops L = [ 42, -10, 4 ] x i 0 12 Index-based Loops sum = 0 for x in L: sum += x sum = 0 for i in range(len(L)) : sum += L[i] L[i]

Sum every other element def sum( L ): """ returns the sum of L's elements """ sum = 0 for i in range(len(L)): if ________: sum += L[i] return sum Finding the sum of a list: Accumulator! shortcuts? vs. recursion? sum every OTHER element?

Sum every other element def sum( L ): """ returns the sum of L's elements """ sum = 0 for i in range(len(L)): if i % 2 == 0: sum += L[i] return sum Finding the sum of a list: Accumulator! shortcuts? vs. recursion? sum every OTHER element?

Extreme Looping What does this code do? print 'It keeps on' while True: print('going and’) print('Phew! I\'m done!’)

Extreme Looping Anatomy of a while loop: print 'It keeps on' while True: print('going and’) print('Phew! I\'m done!’) “while” loop the loop keeps on running as long as this test is True alternative tests? This won't print until the while loop finishes - in this case, never!

Extreme Looping import time print 'It keeps on' while True: print('going and’) time.sleep(1) print('Phew! I\'m done!’) “while” loop Slowing things down… the loop keeps on running as long as this test is True

Making our escape! import random escape = 0 while escape != 42: print('Help! Let me out!’) escape = random.choice([41,42,43]) print('At last!’) how could we count the number of loops we run?

Loops aren't just for lists… for c in 'down with CS!': print ( c )

What do these two loops print? n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print(n) def min( L ): Write a loop to find and return the min of a list, L L is a list of numbers. n = 3 while n > 1: print n if n % 2 == 0: n = n/2 else: n = 3*n + 1 def isPrime( n ): Write a loop so that this function returns True if its input is prime and False otherwise: n is a positive integer

What do these two loops print? n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print(n) ?? n = 3 while n > 1: print(n) if n%2 == 0: n = n/2 else: n = 3*n + 1

What do these two loops print? n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print n 7 n = 3 while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 ??

What do these two loops print? n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print n 7 n = 3 while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n + 1 3

What do these two loops print? n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print n 7 n = 3 while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n

What do these two loops print? n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print n 7 n = 3 while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n

What do these two loops print? n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print n 7 n = 3 while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n

What do these two loops print? n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print n 7 n = 3 while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n

What do these two loops print? n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print n 7 n = 3 while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n

What do these two loops print? n = 0 for c in 'forty-two': if c not in 'aeiou': n += 1 print n 7 n = 3 while n > 1: print n if n%2 == 0: n = n/2 else: n = 3*n

def min( L ): L is a list of numbers. def isPrime( n ): n is a positive integer

L is a list of numbers. def isPrime( n ): n is a positive integer def min( L ): mn = L[0] for i in range(1,len(L)): if L[i] < mn: mn = L[i] return mn

L is a list of numbers. def isPrime( n ): n is a positive integer def min( L ): mn = L[0] for i in range(1,len(L)): if L[i] < mn: mn = L[i] return mn def min( L ): mn=L[0] for s in L: if s < mn: mn = s return mn

def min( L ): mn = L[0] for i in range(1,len(L)): if L[i] < mn: mn = L[i] return mn def min( L ): mn=L[0] for s in L: if s < mn: mn = s return mn L is a list of numbers. def isPrime( n ): for i in range (n): if i not in [0,1]: if n%i == 0: return False return True n is a positive integer

Consider the following update rule for all complex numbers c: z 0 = 0 z n+1 = z n 2 + c If z does not diverge, c is in the M. Set. Real axis Imaginary axis c the Mandelbrot Set Benoit M. z0z0 z1z1 z2z2 z3z3 z4z4

Consider the following update rule for all complex numbers c: z 0 = 0 z n+1 = z n 2 + c If c does not diverge, it's in the M. Set. Real axis Imaginary axis c the Mandelbrot Set Benoit M. z0z0 z1z1 z2z2 z3z3 z4z4 example of a non-diverging cycle

Consider the following update rule for all complex numbers c: z 0 = 0 z n+1 = z n 2 + c the Mandelbrot Set The shaded area are points that do not diverge.

Python and images for creating and saving images import bmp.py image = BitMap( 300, 200, Color.GREEN ) creates a bitmap object and names it image

Python and images for creating and saving images import bmp.py image = BitMap( 300, 200, Color.GREEN ) creates a bitmap object and names it image objects are software abstractions containing structured information

Python and images and objects import bmp.py image = BitMap( 300, 200, Color.GREEN ) here, a bitmap object named image is calling an internal method named saveFile objects are variables that can contain their own functions, often called methods image.saveFile( "test.bmp" )

Python and images and objects from bmp import * image = BitMap( 300, 200, Color.GREEN ) two more internal methods objects are variables that can contain their own functions, often called methods image.saveFile( "test.bmp" ) image.plotPixel( 150, 100 ) image.setPenColor( Color.Red )

Q: What does this plot? from bmp import * def test(): """ image demonstration """ width = 200 height = 200 image = BitMap( width, height ) # a 2d loop over each pixel for col in range(width): for row in range(height): if col == row: image.plotPoint( col, row ) image.saveFile( "test.bmp" )

Q: What does this plot? A: A diagonal in the SW -> NE direction from bmp import * def test(): """ image demonstration """ width = 200 height = 200 image = BitMap( width, height ) # a 2d loop over each pixel for col in range(width): for row in range(height): if col == row: image.plotPoint( col, row ) image.saveFile( "test.bmp" )

How could you change this code so that it plots a diagonal from NW to SE? def test(): """ demonstrating images """ width = 200 height = 200 image = BitMap( width, height ) # a 2d loop over each pixel for col in range(width): for row in range(height): if col == row: image.plotPoint( col, row ) image.saveFile( "test.bmp" )

How could you change this code so that it plots a diagonal from NW to SE? def test(): """ demonstrating images """ width = 200 height = 200 image = BitMap( width, height ) # a 2d loop over each pixel for col in range(width): for row in range(height): if col == height – row -1: image.plotPoint( col, row ) image.saveFile( "test.bmp" )