Isy Goldwasser (CEO, Thync) The Future of Mental Superpowers with Wearables
Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control...
-
Upload
evangeline-patterson -
Category
Documents
-
view
227 -
download
0
Transcript of Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control...
![Page 1: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/1.jpg)
Object-Oriented Programming in PythonGoldwasser and Letscher
Chapter 4Elementary Control Structures
Terry ScottUniversity of Northern Colorado
2007 Prentice Hall
![Page 2: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/2.jpg)
2
Introduction: Chapter 4 Topics
• Flow of control– for loop statement– if statement– list comprehension
• Case Study: DNA to RNA Transcription
• Case Study: Drawing a Pyramid
• Chapter 5 continues with other flow of control statements
![Page 3: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/3.jpg)
3
For Loopfor i in sequence: <body of loop>
• i is called the loop variable.• sequence can be any sequence of values:
– list. – str.– tuple.– range command.
• body of loop – instructions that are executed by the loop. Must be indented.
![Page 4: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/4.jpg)
4
For Loop
![Page 5: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/5.jpg)
5
For Loop Example (animated)
#for loop printing a sequenceguests = ['Carol', 'Alice', 'Bob']for person in guests: print person=================================
CarolAliceBob
![Page 6: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/6.jpg)
6
For Loop Another Example (animated)
#Another way to do this (previous code is better)
guests = ['Carol', 'Alice', 'Bob']
for i in range(len(guests)):
print i, guests[i]
=================================
0 Carol
1 Alice
2 Bob
![Page 7: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/7.jpg)
7
For Loop Diagram for Previous Slide
![Page 8: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/8.jpg)
8
For Loop Bank Transactions (animated)
transactions = [200, -50, 100]balance = 0for entry in transactions:
print balancebalance = balance + entry
print 'Your balance is', balance===================================0200150250Your balance is 250
![Page 9: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/9.jpg)
9
For Loop Diagrams for Previous Slide
![Page 10: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/10.jpg)
10
Index-Based Loops
for count in range(10,0,-1):
print count,
print 'Blastoff!'
=================================
#output below
10 9 8 7 6 5 4 3 2 1 Blastoff!
![Page 11: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/11.jpg)
11
Index-Based Loops (continued)
#position starts at 0. add 1 to position to have it start at 1
groceries = ['milk', 'cheese', 'bread', 'cereal']
for position in range(len(groceries)):
label = str(1 + position) + '. '
print label + groceries[position]
========================================
1. milk
2. cheese
3. bread
4. cereal
![Page 12: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/12.jpg)
12
Flawed Attempt at Making Names in a List Lower Case
guests = ['Carol', 'Alice', 'Bob']
for person in guests
person = person.lower()
• The problem is once a name is made lower case it is not put back in list.
• See diagram on next slide.
![Page 13: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/13.jpg)
13
For Loop Assignments
![Page 14: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/14.jpg)
14
Solution to Previous Problem with Lower Case
• Use an indexed loop.• Notice the lower case name is assigned
back to the list.
guests = ['Carol', 'Alice', 'Bob']for i in range(len(guests)):
guests[i] = guests[i].lower()
![Page 15: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/15.jpg)
15
Nested Loops
# code prints out chapters 1 and 2 each with # sections a – d and then Appendix. Output # on next slide.for chapter in '12': print 'Chapter ' + chapter for section in 'abcd': print ' Section ' + sectionprint 'Appendix'
![Page 16: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/16.jpg)
16
Nested Loop Output
Chapter 1 Section a Section b Section c Section dChapter 2 Section a Section b Section c Section dAppendix
![Page 17: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/17.jpg)
17
DNA to RNA Transcription
DNA RNA
A U
C G
G C
T A
![Page 18: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/18.jpg)
18
Case Study: DNA to RNA Transcription
#Uses two coordinated lists. Look up base in DNA list.#Use that index to obtain comparable RNA basednaCodes = 'ACGT'rnaCodes = 'UGCA'dna = raw_input('Enter a DNA sequence: ')rnaList = []for base in dna: whichPair = dnaCodes.index(base) rnaLetter = rnaCodes[whichPair] rnaList.append(rnaLetter)rna = ''.join(rnaList)print 'Transcribed into RNA:', rna
![Page 19: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/19.jpg)
19
Drawing a Pyramid: Using Rectangles and Squares
![Page 20: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/20.jpg)
20
Drawing Pyramid with Rectanglesfrom cs1graphics import *numLevels = 8unitSize = 12screenSize = unitSize * (numLevels + 1)paper = Canvas(screenSize, screenSize)centerX = screenSize/2.0for level in range(numLevels): #create top to bottom levels.
width = (level + 1)* unitSizeblock = Rectangle(width, unitSize)centerY = (level + 1) * unitSize
block.move(centerX, centerY)block.setFillColor('grey')paper.add(block)
![Page 21: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/21.jpg)
21
Pyramid Made of Squares
from cs1graphics import *numLevels = 8unitSize = 12screenSize = unitSize * (numLevels + 1)paper = Canvas(screenSize, screenSize)centerX = screenSize/2.0#create levels from top to bottomfor level in range(numLevels):
centerY = (level + 1) * unitSizeleftmostX=centerX-unitSize+level/2.0
![Page 22: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/22.jpg)
22
Pyramid Made of Squares (continued)
#nested for loop – inside previous for
for blockCount in range(level + 1):
block = Square(unitSize)block.move(leftmostX+unitSize*
blockCount.centerY)
block.setFillColor('grey')
paper.add(block)
![Page 23: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/23.jpg)
23
Conditional Statement
![Page 24: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/24.jpg)
24
If Statement Flow Chart
if condition:
body
![Page 25: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/25.jpg)
25
Animated If
x = 3
if x == 3:
x = 2 * x
print x
=================================
6
![Page 26: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/26.jpg)
26
Animated If Continued
x = 4
if x == 3:
x = 2 * x
print x
=================================
3
![Page 27: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/27.jpg)
27
If-Else Statement
![Page 28: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/28.jpg)
28
If-Else Flow Chart
if condition:
body1
else:
body2
![Page 29: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/29.jpg)
29
Animated If-Else
x = 3
if x < 3:
print 'x less than 3'
else:
print x, 'greater than or equal to 3'
================================
3 greater than or equal to 3
![Page 30: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/30.jpg)
30
Conditional Statements (continued)
If condition1:
body1
elif condition2:
body2
else:
body3
![Page 31: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/31.jpg)
31
Flowchart for If – elif - else
![Page 32: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/32.jpg)
32
Counting DNA Bases Using Conditional Statements
numA = numC = numG = numT = 0for base in dna: if base == 'A':
numA += 1else:
if base == 'C': numC += 1else: if base == 'G': numG += 1 else: numT += 1
![Page 33: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/33.jpg)
33
Counting DNA Bases Using Conditional Statements: Better Way
(elif is else and if combined)
numA = numC = numG = numT = 0for base in dna: if base == 'A':
numA += 1elif base == 'C':
numC += 1elif base == 'G':
numG += 1else:
numT += 1
![Page 34: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007.](https://reader030.fdocuments.in/reader030/viewer/2022032708/56649e5e5503460f94b58284/html5/thumbnails/34.jpg)
34
List Comprehension
auxiliary = []
for person in guests:
auxiliary.append(person.lower())
# List comprehension is a shorter way to write
# this:
auxiliary = [person.lower() for person in guests]