Lecture 04 More Iteration, Nested Loops - Brown University
Lecture 04 More Iteration, Nested Loops
Meet UTA Jarrett's dog Greta, lying in her "nest"
1
Indefinite Loops
Chase tail
Rest
Got tail!
Almost got it...
Based in part on notes from the CS-for-All curriculum developed at Harvey Mudd College
2
So far: Two Types of for Loops
vals = [3, 15, 17, 7] x
vals[0] vals[1]vals[2] vals[3]
vals = [3, 15, 17, 7] 01 23
def sum(vals): result = 0 for x in vals: result += x return result
i
def sum(vals): result = 0 for i in range(len(vals)): result += vals[i] return result
element-based loop
index-based loop
Both are examples of definite loops (i.e., fixed number of iterations)
3
Indefinite Loops
? Use an indefinite loop when the # of repetitions you need is: ? not obvious or known ? impossible to determine before the loop begins, e.g.,
? Finding an element ? Computing an estimate up to some error bound ? Playing a game of rock, paper, scissors (as
opposed to one round)
? Toy problem: print_multiples(n, bound) ? should print all multiples of n that are less than bound ? output for print_multiples(9, 100):
9 18 27 36 45 54 63 72 81 90 99
4
Rock, Paper, Scissors, Lizard, Spock
5
Indefinite Loop for Printing Multiples
while loops are how you code indefinite loops in Python:
def print_multiples(n, bound): mult = n while mult < bound: print(mult, end=" ") mult = mult + n print()
6
while Loops
while :
Steps:
1. evaluate the loop test (a boolean expression)
2. if it's True, execute the statements in the body, and go back to step 1
3. if it's False, skip the statements in the body and go to the statement after the loop
loop test
True
False
7
Tracing a while Loop
? Let's trace the loop for print_multiples(15, 70):
mult = n while mult < bound:
print(mult, end=' ') mult = mult + n print()
n bound
Prints everything on the same line with spaces in between! Neat!
mult < bound
output thus far
mult
8
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- lecture 04 more iteration nested loops brown university
- evaluating nested expressions in python idc online
- list comprehensions department of computer science and electrical
- ppyytthhoonn nneesstteedd llooooppss
- chapter 3 selection statements southeastern louisiana university
- random access lists nested data types and numeral systems haskell
- lecture 6 lists nested loops and files github pages
- python if else and nested if else tutorial kart
- lecture 14 nested lists tuples and dictionaries
- about python 5 while loop
Related searches
- while loops in verilog
- dilated bowel loops icd 10
- ubuntu 20 04 vs 18 04 performance
- free loops and sound kits
- brown university philosophy faculty
- dilated small bowel loops icd 10
- examples of loops in python
- brown university biology
- brown university marine biology
- brown university biology concentration
- brown university marine biology program
- brown university biology ranking