core elements
DESCRIPTION
Core Elements. Part iii: Iterations. Overview. Tuples Mutable, Immutable object For Loops While Loops. Lists in Python. Lists are mutable objects, e.g. we can modify their contents. Code. >>> my_list = [1, 10, 4, 5 ] # List creation [ ] >>> my_list [2 ] # Accessing the third element - PowerPoint PPT PresentationTRANSCRIPT
Lilian BlotLilian Blot
PART I I I : ITERATIONS
Core Elements
Autumn 2012TPOP
1
Lilian Blot
Overview
Tuples
Mutable, Immutable object
For Loops
While Loops
Autumn 2012TPOP
2
Lilian Blot
Lists in Python
Lists are mutable objects, e.g. we can modify their contents.
Autumn 2012TPOP
3
>>> my_list = [1, 10, 4, 5] # List creation [ ]>>> my_list[2] # Accessing the third element4>>> 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 2012TPOP
4
>>> my_tuple = (1, 10, 4, 5) # tuple creation ( ,)>>> my_tuple[2] # Accessing the third element4>>> my_tuple (1, 10, 4, 5)>>> my_tuple[2] = 9 # Modifying the third element
Traceback (most recent call last): File "<pyshell#15>", line 1, in <module> my_tuple [2] = 9TypeError: '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 2012TPOP
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 2012TPOP
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 2012TPOP
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 2012TPOP
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 2012TPOP
9
>>> lst1 = lst2 = [2,4,6,8]>>>
Code
lst1
lst2
0 1 2 3
2 4 6 8
Lilian Blot
Be Careful
State Diagram:
Autumn 2012TPOP
10
>>> lst1 = lst2 = [2,4,6,8]>>> t1 = t2 = (1,3,7,9)>>>
Code
t1
t2
lst1
lst2 0 1 2 3
1 3 7 9
0 1 2 3
2 4 6 8
Lilian Blot
Be Careful
State Diagram:
Autumn 2012TPOP
11
>>> 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
1 3 7 9
0 1 2 3
2 4 6 8
0 1 2 3
1 3 5 9
Lilian Blot
Be Careful
State Diagram:
Autumn 2012TPOP
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
lst2 0 1 2 3
1 3 7 9
0 1 2 3
2 4 6 8
0 1 2 3
1 3 5 9
5
Lilian Blot
Be Careful
State Diagram:
Autumn 2012TPOP
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
lst2 0 1 2 3
1 3 7 9
0 1 2 3
2 4 5 8
0 1 2 3
1 3 5 9
0 1 2 3
2 4 5 8
Lilian Blot
Be Careful
State Diagram:
Autumn 2012TPOP
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
lst2 0 1 2 3
1 3 7 9
0 1 2 3
2 4 5 8
0 1 2 3
1 3 5 9
0 1 2 3
2 4 5 80
Lilian Blot
Swapping Values
State Diagram:
Autumn 2012TPOP
15
>>> num_one = 5>>> num_two = 9>>>
Code
num_two
num_one
5
9
Lilian Blot
Swapping Values
State Diagram: First Attempt
Autumn 2012TPOP
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 2012TPOP
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 BlotLilian Blot
DEFINITE&
INDEFINITE LOOPS
Iteration
Autumn 2012TPOP
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 2012TPOP
19
Lilian Blot
Definite Loop: For statement
The simplest kind of loop
It will execute a definite amount of times
The for loop statementfor <var> in <iterable>: <body>
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 2012TPOP
20
Lilian Blot
Example
Autumn 2012TPOP
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 9After the for loop
Python shell
Lilian Blot
Example
Autumn 2012TPOP
22
print "Before the for loop"
for val in [1,2,3]: square_val = val * val print . . .
print "After the for loop"
Code
Python shell
Lilian Blot
Example
Autumn 2012TPOP
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
1 2 3
val
Lilian Blot
Example
Autumn 2012TPOP
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
1 2 3
1
valsquare_val
Lilian Blot
Example
Autumn 2012TPOP
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
1 2 3
1
valsquare_val
Lilian Blot
Example
Autumn 2012TPOP
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
1 2 3
1
valsquare_val
Lilian Blot
Example
Autumn 2012TPOP
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
2 3
4
valsquare_val
Lilian Blot
Example
Autumn 2012TPOP
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
2 3
4
valsquare_val
Lilian Blot
Example
Autumn 2012TPOP
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
2 3
4
valsquare_val
Lilian Blot
Example
Autumn 2012TPOP
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
valsquare_val
Lilian Blot
Example
Autumn 2012TPOP
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
valsquare_val
Lilian Blot
Example
Autumn 2012TPOP
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 9After the for loop
Python shell
3
9
valsquare_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 2012TPOP
33
Lilian Blot
Indefinite Loop: While Statement
The while loop statementwhile <condition>: <body>
Autumn 2012TPOP
34
<condition>
<body>
False
True
Lilian Blot
Indefinite Loop: While Statement
The while loop statementwhile <condition>: <body>
Autumn 2012TPOP
35
<condition>
<body>
False
True
Lilian Blot
Indefinite Loop: While Statement
The while loop statementwhile <condition>: <body>
Autumn 2012TPOP
36
<condition>
<body>
False
True
Lilian Blot
Indefinite Loop: While Statement
The while loop statementwhile <condition>: <body>
Autumn 2012TPOP
37
<condition>
<body>
False
True
Lilian Blot
Indefinite Loop: While Statement
The while loop statementwhile <condition>: <body>
Autumn 2012TPOP
38
<condition>
<body>
False
True
Lilian Blot
Indefinite Loop: While Statement
The while loop statementwhile <condition>: <body>
Autumn 2012TPOP
39
<condition>
<body>
False
True
Lilian Blot
Indefinite Loop: While Statement
The while loop statementwhile <condition>: <body>
Autumn 2012TPOP
40
<condition>
<body>
False
True
Lilian Blot
Indefinite Loop: While Statement
The while loop statementwhile <condition>: <body>
Autumn 2012TPOP
41
<condition>
<body>
False
True
Lilian Blot
Indefinite Loop: While Statement
The while loop statementwhile <condition>: <body>
Autumn 2012TPOP
42
<condition>
<body>
False
True
Lilian Blot
Indefinite Loop: While Statement
The while loop statementwhile <condition>: <body>
Autumn 2012TPOP
43
<condition>
<body>
False
True
Lilian Blot
Indefinite Loop: While Statement
The while loop statementwhile <condition>: <body>
Autumn 2012TPOP
44
<condition>
<body>
False
True
Lilian Blot
Indefinite Loop: While Statement
The while loop statementwhile <condition>: <body>
Autumn 2012TPOP
45
<condition>
<body>
False
True
Lilian Blot
Indefinite Loop: While Statement
The while loop statementwhile <condition>: <body>
Autumn 2012TPOP
46
<condition>
<body>
False
True
Lilian Blot
Indefinite Loop: While Statement
The while loop statementwhile <condition>: <body>
Autumn 2012TPOP
47
<condition>
<body>
False
True
Lilian Blot
Indefinite Loop: While Statement
The while loop statementwhile <condition>: <body>
Autumn 2012TPOP
48
<condition>
<body>
False
True
Lilian Blot
Indefinite Loop: While Statement
The while loop statementwhile <condition>: <body>
The <body> usually contains statements that modify the evaluation of <condition> at some point
Autumn 2012TPOP
49
<condition>
<body>
False
True
Lilian Blot
Indefinite Loop: While Statement
The while loop statementwhile <condition>: <body>
The <body> usually contains statements that modify the evaluation of <condition> at some point
The <body> may never be executed!
Autumn 2012TPOP
50
<condition>
<body>
False
True
Lilian Blot
General Form
The while loop general form Two new Keywords:
1. break 2. continue
while <condition>: <statements K> if<test A>: break if<test B>: continue <statements M>
Autumn 2012TPOP
51
<condition>
<statements K>
False
True
if<test A>: break if<test B>: continue <statements M>
Lilian Blot
General Form
The while loop general form Two new Keywords:
1. break 2. continue
while <condition>: <statements K> if<test A>: break if<test B>: continue <statements M>
Test A is True
Autumn 2012TPOP
52
<condition>
<statements K>
False
True
if<test A>: break if<test B>: continue <statements M>
Lilian Blot
General Form
The while loop general form Two new Keywords:
1. break 2. continue
while <condition>: <statements K> if<test A>: break if<test B>: continue <statements M>
Test A is False & Test B is True
Autumn 2012TPOP
53
<condition>
<statements K>
False
True
if<test A>: break if<test B>: continue <statements M>
Lilian Blot
General Form
The for loop general form
for <val> in <iterable>: <statements K> if<test A>: break if<test B>: continue <statements M>
Autumn 2012TPOP
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 2012TPOP
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 2012TPOP
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 2012TPOP
57