Download presentation
Presentation is loading. Please wait.
Published byTheodora Williams Modified over 9 years ago
1
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1
2
Lilian Blot What we have seen so Far What variables are Introduced the notion of Functions & Parameters Selection Structure (branching) How to do simple repetitions/iterations Autumn 2014 TPOP 2
3
Lilian Blot Overview How to represent a collection of values? Tuples Lists Mutable, Immutable object For Loops While Loops Autumn 2014 TPOP 3
4
Lilian Blot Data Types We have seen numbers int: whole number float: decimal point boolean (True, False) String str: “a word” What about a collection of data? Autumn 2014 TPOP 4
5
Lilian Blot text 0 1 2 3 text 0 1 2 3 TPOP String (str) A kind of collection of characters Autumn 2014 TPOP 5 >>> text = ‘TPOP’ >>> letter = text[2] # Accessing the third element >>> letter ‘O’ letter text[2]
6
Lilian Blot Tuples in Python Tuples are immutable objects, e.g. we CANNOT modify their contents. Autumn 2014 TPOP 6 >>> 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
7
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 2014 TPOP 7 >>> my_t = (1, 10, 4, 5) # tuple creation (,) >>> my_t[1] 10 >>> my_t = my_t[:2] + (9,) + my_t[3:] # Modifying the third element >>> my_t (1, 10, 9, 5) Code
8
Lilian Blot Lists in Python Lists are mutable objects, e.g. we can modify their contents. Autumn 2014 TPOP 8 >>> 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
9
Lilian Blot Be Careful Example: Autumn 2014 TPOP 9 >>> 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
10
Lilian Blot Be Careful Example: Autumn 2014 TPOP 10 >>> 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
11
Lilian Blot Be Careful Example: Autumn 2014 TPOP 11 >>> 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
12
Lilian Blot Be Careful State Diagram: Autumn 2014 TPOP 12 >>> lst1 = lst2 = [2,4,6,8] >>> Code lst1 lst2 0 1 2 3 24 6 8
13
Lilian Blot Be Careful State Diagram: Autumn 2014 TPOP 13 >>> lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>> Code t1 t2 lst1 lst2 0 1 2 3 1379 24 6 8
14
Lilian Blot Be Careful State Diagram: Autumn 2014 TPOP 14 >>> lst1 = lst2 = [2,4,6,8] >>> t1 = t2 = (1,3,7,9) >>> t2 = t2[:2] + (5, ) + t2[3:] >>> Code t1 t2 lst1 lst2 0 1 2 3 1379 24 6 8 1359
15
Lilian Blot Be Careful State Diagram: Autumn 2014 TPOP 15 >>> 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 lst2 0 1 2 3 1379 24 6 8 1359 5
16
Lilian Blot Be Careful State Diagram: Autumn 2014 TPOP 16 >>> 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 lst2 0 1 2 3 1379 24 5 8 1359 2458
17
Lilian Blot Be Careful State Diagram: Autumn 2014 TPOP 17 >>> 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 lst2 0 1 2 3 1379 24 5 8 1359 2458 0
18
Lilian Blot Swapping Values State Diagram: Autumn 2014 TPOP 18 >>> num_one = 5 >>> num_two = 9 >>> Code num_two num_one 5 9
19
Lilian Blot Swapping Values State Diagram: First Attempt Autumn 2014 TPOP 19 >>> num_one = 5 >>> num_two = 9 >>> num_one = num_two >>> num_two = num_one >>> Code num_two num_one 5 9
20
Lilian Blot Swapping Values State Diagram: Second Attempt Autumn 2014 TPOP 20 >>> num_one = 5 >>> num_two = 9 >>> temp = num_two >>> num_two = num_one >>> num_one = temp >>> Code num_two num_one temp 5 9
21
Lilian Blot Personal work Investigate what is meant by SLICING strings, lists and tuples. Autumn 2014 TPOP 21
22
Lilian Blot DEFINITE & INDEFINITE LOOPS Iteration Autumn 2014 TPOP 22
23
Lilian Blot Iteration We need a structure to execute a sequence of statements multiple times in succession How can we proceed if we are not sure how many times it needs to be repeated? Autumn 2014 TPOP 23
24
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 2014 TPOP 24
25
Lilian Blot Example Autumn 2014 TPOP 25 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
26
Lilian Blot Example Autumn 2014 TPOP 26 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” print "After the for loop" CodePython shell
27
Lilian Blot Example Autumn 2014 TPOP 27 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” print "After the for loop" Code Before the for loop Python shell 123 val
28
Lilian Blot Example Autumn 2014 TPOP 28 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” print "After the for loop" Code Before the for loop Python shell 123 1 val square_val
29
Lilian Blot Example Autumn 2014 TPOP 29 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 Python shell 123 1 val square_val
30
Lilian Blot Example Autumn 2014 TPOP 30 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 Python shell 12 3 1 val square_val
31
Lilian Blot Example Autumn 2014 TPOP 31 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” print "After the for loop" Code Before the for loop --> inside loop: square of 1 is 1 Python shell 23 4 val square_val
32
Lilian Blot Example Autumn 2014 TPOP 32 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” 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
33
Lilian Blot Example Autumn 2014 TPOP 33 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” 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
34
Lilian Blot Example Autumn 2014 TPOP 34 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” 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
35
Lilian Blot Example Autumn 2014 TPOP 35 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” 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
36
Lilian Blot Example Autumn 2014 TPOP 36 print "Before the for loop" for val in [1,2,3]: square_val = val * val print “--> inside...” 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
37
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 2014 TPOP 37
38
Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 38 False True
39
Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 39 False True
40
Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 40 False True
41
Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 41 False True
42
Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 42 False True
43
Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 43 False True
44
Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 44 False True
45
Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 45 False True
46
Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 46 False True
47
Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 47 False True
48
Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 48 False True
49
Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 49 False True
50
Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 50 False True
51
Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 51 False True
52
Lilian Blot Indefinite Loop: While Statement The while loop statement while : Autumn 2014 TPOP 52 False True
53
Lilian Blot Indefinite Loop: While Statement The while loop statement while : The usually contains statements that modify the evaluation of at some point Autumn 2014 TPOP 53 False True
54
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 2014 TPOP 54 False True
55
Lilian Blot General Form The while loop general form Two new Keywords: 1. break 2. continue while : if : break if : continue Autumn 2014 TPOP 55 False True if : break if : continue
56
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 2014 TPOP 56 False True if : break if : continue
57
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 2014 TPOP 57 False True if : break if : continue
58
Lilian Blot General Form The for loop general form for in : if : break if : continue Autumn 2014 TPOP 58
59
Lilian Blot Summary We have seen mutable and immutable data type consequences when modifying data Definite iteration for loops Indefinite iteration (a.k.a conditional loop) while loops General form using break and continue Autumn 2014 TPOP 59
60
Lilian Blot Next week Scope of variables and parameters Parameter passed by value or by reference Understanding mutable and immutable object is essential Autumn 2014 TPOP 60
61
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 2014 TPOP 61
62
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 2014 TPOP 62
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.