ObjectOriented Programming in Python Goldwasser and Letscher Chapter
Object-Oriented Programming in Python Goldwasser and Letscher Chapter 4 Elementary Control Structures Terry Scott University of Northern Colorado 2007 Prentice Hall
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 2
For Loop for 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. 3
For Loop 4
For Loop Example (animated) #for loop printing a sequence guests = ['Carol', 'Alice', 'Bob'] for person in guests: print person ================= Carol Alice Bob 5
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 6
For Loop Diagram for Previous Slide 7
For Loop Bank Transactions (animated) transactions = [200, -50, 100] balance = 0 for entry in transactions: print balance = balance + entry print 'Your balance is', balance ================== 0 200 150 Your balance is 250 8
For Loop Diagrams for Previous Slide 9
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! 10
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 11
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. 12
For Loop Assignments 13
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() 14
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 ' + section print 'Appendix' 15
Nested Loop Output Chapter 1 Section a Section b Section c Section d Chapter 2 Section a Section b Section c Section d Appendix 16
DNA to RNA Transcription DNA RNA A U C G G C T A 17
Case Study: DNA to RNA Transcription #Uses two coordinated lists. Look up base in DNA list. #Use that index to obtain comparable RNA base dna. Codes = 'ACGT' rna. Codes = 'UGCA' dna = raw_input('Enter a DNA sequence: ') rna. List = [] for base in dna: which. Pair = dna. Codes. index(base) rna. Letter = rna. Codes[which. Pair] rna. List. append(rna. Letter) rna = ''. join(rna. List) print 'Transcribed into RNA: ', rna 18
Drawing a Pyramid: Using Rectangles and Squares 19
Conditional Statement 20
If Statement Flow Chart if condition: body 21
Animated If x=3 if x == 3: x=2*x print x ================= 6 22
Animated If Continued x=4 if x == 3: x=2*x print x ================= 3 23
If-Else Statement 24
If-Else Flow Chart if condition: body 1 else: body 2 25
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 26
Conditional Statements (continued) If condition 1: body 1 elif condition 2: body 2 else: body 3 27
Flowchart for If – elif - else 28
Counting DNA Bases Using Conditional Statements num. A = num. C = num. G = num. T = 0 for base in dna: if base == 'A': num. A += 1 else: if base == 'C': num. C += 1 else: if base == 'G': num. G += 1 else: num. T += 1 29
Counting DNA Bases Using Conditional Statements: Better Way (elif is else and if combined) num. A = num. C = num. G = num. T = 0 for base in dna: if base == 'A': num. A += 1 elif base == 'C': num. C += 1 elif base == 'G': num. G += 1 else: num. T += 1 30
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] 31
List Comprehension 32
List Comprehension • Output: 33
List Comprehension • Output: 34
List Comprehension • Output: 35
List Comprehension • Output: 36
- Slides: 36