Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

2 Casa 15m Perspectiva Lateral Izquierda.
Repaso: Unidad 2 Lección 2
1 A B C
Scenario: EOT/EOT-R/COT Resident admitted March 10th Admitted for PT and OT following knee replacement for patient with CHF, COPD, shortness of breath.
Variations of the Turing Machine
3rd Annual Plex/2E Worldwide Users Conference 13A Batch Processing in 2E Jeffrey A. Welsh, STAR BASE Consulting, Inc. September 20, 2007.
AP STUDY SESSION 2.
1
Slide 1Fig 39-CO, p Slide 2Fig 39-1, p.1246.
Select from the most commonly used minutes below.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Processes and Operating Systems
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
David Burdett May 11, 2004 Package Binding for WS CDL.
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Local Customization Chapter 2. Local Customization 2-2 Objectives Customization Considerations Types of Data Elements Location for Locally Defined Data.
Create an Application Title 1Y - Youth Chapter 5.
Process a Customer Chapter 2. Process a Customer 2-2 Objectives Understand what defines a Customer Learn how to check for an existing Customer Learn how.
Custom Services and Training Provider Details Chapter 4.
CALENDAR.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt BlendsDigraphsShort.
1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.
The 5S numbers game..
Media-Monitoring Final Report April - May 2010 News.
Break Time Remaining 10:00.
EE, NCKU Tien-Hao Chang (Darby Chang)
Turing Machines.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
PP Test Review Sections 6-1 to 6-6
1 The Blue Café by Chris Rea My world is miles of endless roads.
Bright Futures Guidelines Priorities and Screening Tables
Bellwork Do the following problem on a ½ sheet of paper and turn in.
1 The Royal Doulton Company The Royal Doulton Company is an English company producing tableware and collectables, dating to Operating originally.
Operating Systems Operating Systems - Winter 2012 Chapter 4 – Memory Management Vrije Universiteit Amsterdam.
Operating Systems Operating Systems - Winter 2010 Chapter 3 – Input/Output Vrije Universiteit Amsterdam.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
BEEF & VEAL MARKET SITUATION "Single CMO" Management Committee 22 November 2012.
TESOL International Convention Presentation- ESL Instruction: Developing Your Skills to Become a Master Conductor by Beth Clifton Crumpler by.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
Adding Up In Chunks.
MaK_Full ahead loaded 1 Alarm Page Directory (F11)
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
Before Between After.
: 3 00.
5 minutes.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
1 Let’s Recapitulate. 2 Regular Languages DFAs NFAs Regular Expressions Regular Grammars.
Types of selection structures
Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1.
Speak Up for Safety Dr. Susan Strauss Harassment & Bullying Consultant November 9, 2012.
1 Titre de la diapositive SDMO Industries – Training Département MICS KERYS 09- MICS KERYS – WEBSITE.
Essential Cell Biology
Converting a Fraction to %
Clock will move after 1 minute
PSSA Preparation.
Immunobiology: The Immune System in Health & Disease Sixth Edition
Physics for Scientists & Engineers, 3rd Edition
Energy Generation in Mitochondria and Chlorplasts
Select a time to count down from the clock above
Copyright Tim Morris/St Stephen's School
1.step PMIT start + initial project data input Concept Concept.
1 Dr. Scott Schaefer Least Squares Curves, Rational Representations, Splines and Continuity.
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Presentation transcript:

Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1

Lilian Blot Overview Tuples Mutable, Immutable object For Loops While Loops Autumn 2012 TPOP 2

Lilian Blot Lists in Python Lists are mutable objects, e.g. we can modify their contents. Autumn 2012 TPOP 3 >>> my_list = [1, 10, 4, 5] # List creation [ ] >>> my_list[2] # Accessing the third element 4 >>> my_list [1, 10, 4, 5] >>> my_list[2] = 9 # Modifying the third element >>> my_list # The modified list [1, 10, 9, 5] >>> Code

Lilian Blot Tuples in Python Tuples are immutable objects, e.g. we CANNOT modify their contents. Autumn 2012 TPOP 4 >>> my_tuple = (1, 10, 4, 5) # tuple creation (,) >>> my_tuple[2] # Accessing the third element 4 >>> my_tuple (1, 10, 4, 5) >>> my_tuple[2] = 9 # Modifying the third element Traceback (most recent call last): File " ", line 1, in my_tuple [2] = 9 TypeError: 'tuple' object does not support item assignment >>> Code

Lilian Blot Tuples in Python Tuples are immutable objects, e.g. we CANNOT modify their contents. Note that numbers and strings are immutable too. Autumn 2012 TPOP 5 >>> my_t = (1, 10, 4, 5) # tuple creation (,) >>> my_t = my_t(:2) + (9,) + my_t(3:) # Modifying the third element >>> my_t (1, 10, 9, 5) Code

Lilian Blot Be Careful Example: Autumn 2012 TPOP 6 >>> lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>> lst1 [2, 4, 6, 8] >>> lst2 [2, 4, 6, 8] >>> t1 (1, 3, 7, 9) >>> t2 (1, 3, 7, 9) >>> Code

Lilian Blot Be Careful Example: Autumn 2012 TPOP 7 >>> t2 = t2[:2] + (5, ) + t2[3:] # Modifying the third element t2 >>> lst2[2] = 5 # Modifying the third element of lst2 >>> t1 (1, 3, 7, 9) >>> t2 (1, 3, 5, 9) >>> Code A change in t2 does not affect t1

Lilian Blot Be Careful Example: Autumn 2012 TPOP 8 >>> t2 = t2[:2] + (5, ) + t2[3:] # Modifying the third element t2 >>> lst2[2] = 5 # Modifying the third element of lst2 >>> t1 (1, 3, 7, 9) >>> t2 (1, 3, 5, 9) >>> lst1 [2, 4, 5, 8] >>> lst2 [2, 4, 5, 8] >>> Code A change in t2 does not affect t1 A change in lst2 does affect lst1

Lilian Blot Be Careful State Diagram: Autumn 2012 TPOP 9 >>> lst1 = lst2 = [2,4,6,8] >>> Code lst1 lst

Lilian Blot Be Careful State Diagram: Autumn 2012 TPOP 10 >>> lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>> Code t1 t2 lst1 lst

Lilian Blot Be Careful State Diagram: Autumn 2012 TPOP 11 >>> lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>> t2 = t2[:2] + (5, ) + t2[3:] >>> Code t1 t2 lst1 lst

Lilian Blot Be Careful State Diagram: Autumn 2012 TPOP 12 >>> lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>> t2 = t2[:2] + (5, ) + t2[3:] >>> lst2[2] = 5 >>> Code t1 t2 lst1 lst

Lilian Blot Be Careful State Diagram: Autumn 2012 TPOP 13 >>> lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>> t2 = t2[:2] + (5, ) + t2[3:] >>> lst2[2] = 5 >>> >>> lst2 = [2, 4, 5, 8] >>> Code t1 t2 lst1 lst

Lilian Blot Be Careful State Diagram: Autumn 2012 TPOP 14 >>> lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>> t2 = t2[:2] + (5, ) + t2[3:] >>> lst2[2] = 5 >>> >>> lst2 = [2, 4, 5, 8] >>> lst2[2] = 0 Code t1 t2 lst1 lst

Lilian Blot Swapping Values State Diagram: Autumn 2012 TPOP 15 >>> num_one = 5 >>> num_two = 9 >>> Code num_two num_one 5 9

Lilian Blot Swapping Values State Diagram: First Attempt Autumn 2012 TPOP 16 >>> num_one = 5 >>> num_two = 9 >>> num_one = num_two >>> num_two = num_one >>> Code num_two num_one 5 9

Lilian Blot Swapping Values State Diagram: Second Attempt Autumn 2012 TPOP 17 >>> num_one = 5 >>> num_two = 9 >>> temp = num_two >>> num_two = num_one >>> num_one = temp >>> Code num_two num_one temp 5 9

Lilian Blot DEFINITE & INDEFINITE LOOPS Iteration Autumn 2012 TPOP 18

Lilian Blot Iteration We need a structure to execute a sequence of statements multiple times in succession So Far, we have to write the sequence of statement n times to achieve n repetition How can we proceed if we are not sure how many times it needs to be repeated? Autumn 2012 TPOP 19

Lilian Blot Definite Loop: For statement The simplest kind of loop It will execute a definite amount of times The for loop statement for in :  Iterable can return its element one at a time  The body of the loop can be any sequence of Python statements  The variable after the keyword for is called the loop index. Autumn 2012 TPOP 20

Lilian Blot Example Autumn 2012 TPOP 21 print "Before the for loop" for val in [1,2,3]: square_val = val * val print "--> inside loop: square of", str(val), "is", str(square_val) print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 --> inside loop: square of 2 is 4 --> inside loop: square of 3 is 9 After the for loop Python shell

Lilian Blot Example Autumn 2012 TPOP 22 print "Before the for loop" for val in [1,2,3]: square_val = val * val print... print "After the for loop" CodePython shell

Lilian Blot Example Autumn 2012 TPOP 23 print "Before the for loop" for val in [1,2,3]: square_val = val * val print... print "After the for loop" Code Before the for loop Python shell 123 val

Lilian Blot Example Autumn 2012 TPOP 24 print "Before the for loop" for val in [1,2,3]: square_val = val * val print... print "After the for loop" Code Before the for loop Python shell val square_val

Lilian Blot Example Autumn 2012 TPOP 25 print "Before the for loop" for val in [1,2,3]: square_val = val * val print... print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 Python shell val square_val

Lilian Blot Example Autumn 2012 TPOP 26 print "Before the for loop" for val in [1,2,3]: square_val = val * val print... print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 Python shell val square_val

Lilian Blot Example Autumn 2012 TPOP 27 print "Before the for loop" for val in [1,2,3]: square_val = val * val print... print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 Python shell 23 4 val square_val

Lilian Blot Example Autumn 2012 TPOP 28 print "Before the for loop" for val in [1,2,3]: square_val = val * val print... print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 --> inside loop: square of 2 is 4 Python shell 23 4 val square_val

Lilian Blot Example Autumn 2012 TPOP 29 print "Before the for loop" for val in [1,2,3]: square_val = val * val print... print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 --> inside loop: square of 2 is 4 Python shell 23 4 val square_val

Lilian Blot Example Autumn 2012 TPOP 30 print "Before the for loop" for val in [1,2,3]: square_val = val * val print... print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 --> inside loop: square of 2 is 4 Python shell 3 9 val square_val

Lilian Blot Example Autumn 2012 TPOP 31 print "Before the for loop" for val in [1,2,3]: square_val = val * val print... print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 --> inside loop: square of 2 is 4 --> inside loop: square of 3 is 9 Python shell 3 9 val square_val

Lilian Blot Example Autumn 2012 TPOP 32 print "Before the for loop" for val in [1,2,3]: square_val = val * val print... print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 --> inside loop: square of 2 is 4 --> inside loop: square of 3 is 9 After the for loop Python shell 3 9 val square_val

Lilian Blot Indefinite Loop: While Statement An indefinite loop keeps iterating until certain condition are met (conditional loop) There is no guarantee ahead of time regarding how many times the loop will go around  zero time  x-times  indefinitely Autumn 2012 TPOP 33

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2012 TPOP 34 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2012 TPOP 35 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2012 TPOP 36 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2012 TPOP 37 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2012 TPOP 38 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2012 TPOP 39 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2012 TPOP 40 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2012 TPOP 41 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2012 TPOP 42 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2012 TPOP 43 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2012 TPOP 44 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2012 TPOP 45 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2012 TPOP 46 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2012 TPOP 47 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2012 TPOP 48 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : The usually contains statements that modify the evaluation of at some point Autumn 2012 TPOP 49 False True

Lilian Blot Indefinite Loop: While Statement The while loop statement while : The usually contains statements that modify the evaluation of at some point The may never be executed! Autumn 2012 TPOP 50 False True

Lilian Blot General Form The while loop general form  Two new Keywords: 1. break 2. continue while : if : break if : continue Autumn 2012 TPOP 51 False True if : break if : continue

Lilian Blot General Form The while loop general form  Two new Keywords: 1. break 2. continue while : if : break if : continue  Test A is True Autumn 2012 TPOP 52 False True if : break if : continue

Lilian Blot General Form The while loop general form  Two new Keywords: 1. break 2. continue while : if : break if : continue  Test A is False & Test B is True Autumn 2012 TPOP 53 False True if : break if : continue

Lilian Blot General Form The for loop general form for in : if : break if : continue Autumn 2012 TPOP 54

Lilian Blot Summary We have seen mutable and immutable data type  consequences when modifying data Definite iteration  for loops Indefinite iteration (conditional loop)  while loops General form using break and continue Autumn 2012 TPOP 55

Lilian Blot Exercises Consider a problem where we want to record and store the marks of many students, each students having more than one mark (several modules with same credits).  what form the data structure would look like  how would you write a program to enter the marks of one student  how would you proceed to enter the marks of more than one students  how would you calculate the average mark of each student  what if modules don’t have the same credits (e.g. 10, 20, 30) Autumn 2012 TPOP 56

Lilian Blot Exercises Calculate the average of values in a list Calculate the average of values entered by a user, we don’t know how many values the user want to enter. The user should enter an empty value to signal the end of data input, then a result must be returned. Autumn 2012 TPOP 57