Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1."— Presentation transcript:

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

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

3 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

4 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

5 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

6 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

7 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

8 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

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

10 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

11 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

12 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

13 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

14 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

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

16 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

17 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

18 Lilian Blot DEFINITE & INDEFINITE LOOPS Iteration Autumn 2012 TPOP 18

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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

28 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

29 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

30 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

31 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

32 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

33 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

49 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

50 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

51 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

52 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

53 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

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

55 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

56 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

57 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


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

Similar presentations


Ads by Google