loops - cornell university...2018/09/28 · for loops and conditionals defnum_ints(thelist):...
TRANSCRIPT
![Page 1: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/1.jpg)
Loops
Not-So-Mini-Lecture 14
![Page 2: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/2.jpg)
Example: Summing the Elements of a List
def sum(thelist):"""Returns: the sum of all elements in thelistPrecondition: thelist is a list of all numbers (either floats or ints)"""pass # Stub to be implemented
9/28/18 For Loops 2
Remember our approach:Outline first; then implement
![Page 3: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/3.jpg)
Example: Summing the Elements of a List
def sum(thelist):"""Returns: the sum of all elements in thelistPrecondition: thelist is a list of all numbers (either floats or ints)"""# Create a variable to hold result (start at 0)# Add each list element to variable# Return the variable
9/28/18 For Loops 3
![Page 4: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/4.jpg)
Example: Summing the Elements of a List
def sum(thelist):"""Returns: the sum of all elements in thelistPrecondition: thelist is a list of all numbers (either floats or ints)"""result = 0result = result + thelist[0]result = result + thelist[1]…return result
9/28/18 For Loops 4
There is a problem here
![Page 5: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/5.jpg)
For Loops: Processing Sequences
# Print contents of seqx = seq[0]print(x)x = seq[1]print(x)…x = seq[len(seq)-1]print(x)
• Remember: § Cannot program …
The for-loop:
for x in seq:print(x)
• Key Concepts§ loop sequence: seq§ loop variable: x§ body: print(x)§ Also called repetend
9/28/18 For Loops 5
![Page 6: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/6.jpg)
For Loops: Processing Sequences
The for-loop:
for x in seq:print(x)
• loop sequence: seq• loop variable: x• body: print(x)
To execute the for-loop:1. Check if there is a “next”
element of loop sequence2. If not, terminate execution3. Otherwise, put the element
in the loop variable4. Execute all of the body5. Repeat as long as 1 is true
seq has more elts
put next elt in x
True
Falseprint(x)
9/28/18 For Loops 6
![Page 7: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/7.jpg)
Example: Summing the Elements of a List
def sum(thelist):"""Returns: the sum of all elements in thelistPrecondition: thelist is a list of all numbers (either floats or ints)"""# Create a variable to hold result (start at 0)# Add each list element to variable# Return the variable
9/28/18 For Loops 7
![Page 8: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/8.jpg)
Example: Summing the Elements of a List
def sum(thelist):"""Returns: the sum of all elements in thelistPrecondition: thelist is a list of all numbers (either floats or ints)"""result = 0
for x in thelist:result = result + x
return result9/28/18 For Loops 8
• loop sequence: thelist• loop variable: x• body: result=result+x
![Page 9: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/9.jpg)
Example: Summing the Elements of a List
def sum(thelist):"""Returns: the sum of all elements in thelistPrecondition: thelist is a list of all numbers (either floats or ints)"""result = 0
for x in thelist:result = result + x
return result9/28/18 For Loops 9
• loop sequence: thelist• loop variable: x• body: result=result+x
Accumulatorvariable
![Page 10: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/10.jpg)
For Loops and Conditionals
def num_ints(thelist):"""Returns: the number of ints in thelistPrecondition: thelist is a list of any mix of types"""# Create a variable to hold result (start at 0)# for each element in the list…
# check if it is an int# add 1 if it is
# Return the variable
9/28/18 For Loops 10
![Page 11: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/11.jpg)
For Loops and Conditionals
def num_ints(thelist):"""Returns: the number of ints in thelistPrecondition: thelist is a list of any mix of types"""result = 0for x in thelist:
if type(x) == int:result = result+1
return result
9/28/18 For Loops 11
Body
![Page 12: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/12.jpg)
Modifying the Contents of a List
def add_one(thelist):"""(Procedure) Adds 1 to every element in the listPrecondition: thelist is a list of all numbers (either floats or ints)"""for x in thelist:
x = x+1# procedure; no return
9/28/18 For Loops 12
DOES NOT WORK!
![Page 13: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/13.jpg)
For Loops and Call Frames
def add_one(thelist):"""Adds 1 to every eltPre: thelist is all numb."""for x in thelist:
x = x+1
add_one(seq):
9/28/18 For Loops 13
012
id4
547
seq id4
add_one
thelist
1
id41
2
![Page 14: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/14.jpg)
For Loops and Call Frames
def add_one(thelist):"""Adds 1 to every eltPre: thelist is all numb."""for x in thelist:
x = x+1
add_one(seq):
9/28/18 For Loops 14
012
id4
547
seq id4
add_one
thelist
2
id41
2 x 5
![Page 15: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/15.jpg)
For Loops and Call Frames
def add_one(thelist):"""Adds 1 to every eltPre: thelist is all numb."""for x in thelist:
x = x+1
add_one(seq):
9/28/18 For Loops 15
012
id4
547
seq id4
add_one
thelist
1
id41
2 x 6
Increments x in frameDoes not affect folder
Loop backto line 1
![Page 16: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/16.jpg)
For Loops and Call Frames
def add_one(thelist):"""Adds 1 to every eltPre: thelist is all numb."""for x in thelist:
x = x+1
add_one(seq):
9/28/18 For Loops 16
012
id4
547
seq id4
add_one
thelist
2
id41
2 x 4
Next element stored in x.Previous calculation lost.
![Page 17: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/17.jpg)
For Loops and Call Frames
def add_one(thelist):"""Adds 1 to every eltPre: thelist is all numb."""for x in thelist:
x = x+1
add_one(seq):
9/28/18 For Loops 17
012
id4
547
seq id4
add_one
thelist
1
id41
2 x 5
Loop backto line 1
![Page 18: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/18.jpg)
For Loops and Call Frames
def add_one(thelist):"""Adds 1 to every eltPre: thelist is all numb."""for x in thelist:
x = x+1
add_one(seq):
9/28/18 For Loops 18
012
id4
547
seq id4
add_one
thelist
2
id41
2 x 7
Next element stored in x.Previous calculation lost.
![Page 19: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/19.jpg)
For Loops and Call Frames
def add_one(thelist):"""Adds 1 to every eltPre: thelist is all numb."""for x in thelist:
x = x+1
add_one(seq):
9/28/18 For Loops 19
012
id4
547
seq id4
add_one
thelist
1
id41
2 x 8
Loop backto line 1
![Page 20: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/20.jpg)
For Loops and Call Frames
def add_one(thelist):"""Adds 1 to every eltPre: thelist is all numb."""for x in thelist:
x = x+1
add_one(seq):
9/28/18 For Loops 20
012
id4
547
seq id4
add_one
thelist id41
2 x 8
Loop is completed.Nothing new put in x.
![Page 21: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/21.jpg)
For Loops and Call Frames
def add_one(thelist):"""Adds 1 to every eltPre: thelist is all numb."""for x in thelist:
x = x+1
add_one(seq):
9/28/18 For Loops 21
012
id4
547
seq id4
1
2
ERASE WHOLE FRAME
No changesto folder
![Page 22: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/22.jpg)
On The Other Hand
def copy_add_one(thelist):"""Returns: copy with 1 added to every elementPrecondition: thelist is a list of all numbers (either floats or ints)"""mycopy = [] # accumulatorfor x in thelist:
x = x+1mycopy.append(x) # add to end of accumulator
return mycopy9/28/18 For Loops 22
Accumulator keeps result from being lost
![Page 23: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/23.jpg)
How Can We Modify A List?
• Never modify loop var!• This is an infinite loop:
for x in thelist:thelist.append(1)
• Need a second sequence• How about the positions?
thelist = [5, 2, 7, 1]thepos = [0, 1, 2, 3]
for x in thepos:thelist[x] = x+1
9/28/18 For Loops 23
Try this in Python Tutorto see what happens
![Page 24: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/24.jpg)
How Can We Modify A List?
• Never modify loop var!• This is an infinite loop:
for x in thelist:thelist.append(1)
• Need a second sequence• How about the positions?
thelist = [5, 2, 7, 1]thepos = [0, 1, 2, 3]
for x in thepos:thelist[x] = x+1
9/28/18 For Loops 24
Try this in Python Tutorto see what happens
![Page 25: Loops - Cornell University...2018/09/28 · For Loops and Conditionals defnum_ints(thelist): """Returns: the number of intsin thelist Precondition: thelistis a list of any mix of](https://reader034.vdocument.in/reader034/viewer/2022050522/5fa5a071cd404d61f3232bb1/html5/thumbnails/25.jpg)
The Range Function
• range(x)§ Creates an iterable§ Stores [0,1,…,x-1]§ But not a list!§ But try list(range(x))
• range(a,b)§ Stores [a,…,b-1]
• range(a,b,n)§ Stores [a,a+n,…,b-1]
• Very versatile tool• Great for processing ints
total = 0# add the squares of ints# in range 2..200 to total
for x in range(2,201):total = total + x*x
9/28/18 For Loops 25
Accumulator