Download Lecture 7: For Loops - CS1068+– Introductory

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lecture 7: For Loops
CS1068+– Introductory Programming in Python
Dr Kieran T. Herley
Department of Computer Science
University College Cork
2016/17
KH (04/10/16)
Lecture 7: For Loops
2016/17
1 / 24
Summary
Repetition using Pythons for loop construct.
KH (04/10/16)
Lecture 7: For Loops
2016/17
2 / 24
The for loop
The for loop
Suppose we wish to print the numbers from zero upwards
Python provides for loop for these situations
KH (04/10/16)
Lecture 7: For Loops
2016/17
3 / 24
The for loop
The for loop
Suppose we wish to print the numbers from zero upwards
Python provides for loop for these situations
Example: code left produces output on right
0
1
2
3
4
5
6
7
8
9
for i in range(10):
print( i )
Specifies ten repetitions of print for diff.
values of i
KH (04/10/16)
Lecture 7: For Loops
2016/17
3 / 24
The for loop
The for loop
Our example
for i in range(10):
print( i )
Structure
loop header ( i in range(10))
Controls number of repetitions (here 10)
loop body ( print (. . .) )
Block of one or more statements
Indented wrt keyword “for” (four spaces)
KH (04/10/16)
Lecture 7: For Loops
2016/17
4 / 24
The for loop
The for loop
Our example
for i in range(10):
print( i )
Meaning
Have i take on each value 0, 1, · · · , 9 in turn
Execute:
loop
loop
loop
...
loop
body once with i = 0
body once with i = 1
body once with i = 2
body once with i = 9
KH (04/10/16)
Lecture 7: For Loops
2016/17
5 / 24
The for loop
Program execution
for i in range(10):
print( i )
KH (04/10/16)
Time
1
2
3
4
5
6
7
8
9
10
Lecture 7: For Loops
i
0
1
2
3
4
5
6
7
8
9
Statement
print(i)
print(i)
print(i)
print(i)
print(i)
print(i)
print(i)
print(i)
print(i)
print(i)
Output
0
1
2
3
4
5
6
7
8
9
2016/17
6 / 24
The for loop
For loop structure– general structure
for var in X :
Body
Structure
var Any variable name
X Any “iterable object” e.g. range, but there are others
Body Any block of Python indented statements
Meaning
Take each item in X in turn and execute loop body once with var set to
that value
KH (04/10/16)
Lecture 7: For Loops
2016/17
7 / 24
The for loop
For loop flowchart
for var in X :
Body
KH (04/10/16)
Lecture 7: For Loops
2016/17
8 / 24
The for loop
Python range function
Function range produces a sequence of numbers
Examples:
Range
range(10)
KH (04/10/16)
Result
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Lecture 7: For Loops
Note
End value not included
2016/17
9 / 24
The for loop
Python range function
Function range produces a sequence of numbers
Examples:
Range
range(10)
range(2, 10)
KH (04/10/16)
Result
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
2, 3, 4, 5, 6, 7, 8, 9
Lecture 7: For Loops
Note
End value not included
2016/17
9 / 24
The for loop
Python range function
Function range produces a sequence of numbers
Examples:
Range
range(10)
range(2, 10)
range(10, 2)
KH (04/10/16)
Result
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
2, 3, 4, 5, 6, 7, 8, 9
(empty)
Lecture 7: For Loops
Note
End value not included
Empty series possible
2016/17
9 / 24
The for loop
Python range function
Function range produces a sequence of numbers
Examples:
Range
range(10)
range(2, 10)
range(10, 2)
range(-3, 3)
KH (04/10/16)
Result
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
2, 3, 4, 5, 6, 7, 8, 9
(empty)
−3, −2, −1, 0, 1, 2
Lecture 7: For Loops
Note
End value not included
Empty series possible
2016/17
9 / 24
The for loop
Python range function
Function range produces a sequence of numbers
Examples:
Range
range(10)
range(2, 10)
range(10, 2)
range(-3, 3)
range(0, 10, 2)
Result
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
2, 3, 4, 5, 6, 7, 8, 9
(empty)
−3, −2, −1, 0, 1, 2
0, 2, 4, 6, 8
Note
End value not included
Empty series possible
third argument specifies
step count
KH (04/10/16)
Lecture 7: For Loops
2016/17
9 / 24
The for loop
Python range function
Function range produces a sequence of numbers
Examples:
Range
range(10)
range(2, 10)
range(10, 2)
range(-3, 3)
range(0, 10, 2)
Result
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
2, 3, 4, 5, 6, 7, 8, 9
(empty)
−3, −2, −1, 0, 1, 2
0, 2, 4, 6, 8
Note
End value not included
Empty series possible
third argument specifies
step count
range(10, 0, -1)
KH (04/10/16)
10, 9, 8, 7, 6, 5, 4, 3, 2, 1
Lecture 7: For Loops
2016/17
9 / 24
The for loop
Summing the first n integers
Suppose we wanted to calculate the sum of the first n integers
1
Idea:
Build up total in variable named sum
Run through numbers 1 to n inclusive and add each to sum
1
Pretending that we don’t know that
KH (04/10/16)
Pn
i=1 i = n(n
Lecture 7: For Loops
+ 1)/2
2016/17
10 / 24
The for loop
Summing cont’d
n = int(input(”Enter value of n: ”))
total = 0
for k in range(1, n + 1):
total = total + k
print(”Sum of first n ints is ”, total )
KH (04/10/16)
Lecture 7: For Loops
2016/17
11 / 24
The for loop
Summing cont’d
n = int(input(”Enter value of n: ”))
total = 0
for k in range(1, n + 1):
total = total + k
print (”Sum of first n ints is ”, total )
KH (04/10/16)
Time
1
2
3
4
5
6
7
8
Lecture 7: For Loops
Statement
n = input()
total = 0
total = total +
total = total +
total = total +
total = total +
total = total +
print(. . .)
k
k
k
k
k
n
5
5
5
5
5
5
5
5
2016/17
k
?
?
1
2
3
4
5
5
to
?
0
1
3
6
10
15
15
12 / 24
The for loop
Generating Fibonacci numbers
Integer sequence where
First two numbers are zero and one
Each subsequent number if the sum of the two immediately preceding
i.e.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, · · ·
Or mathematically

if n = 0
 0
1
if n = 1
fn =

fn−2 + fn−1 if n > 1
Many interesting applications
KH (04/10/16)
Lecture 7: For Loops
2016/17
13 / 24
The for loop
Idea
Keep track in two variables of “current” number in sequence and its
predecessor
Update at each step as follows:
previous : 5 current : 8
previous, current = current, current + previous
previous : 8 current : 13
KH (04/10/16)
Lecture 7: For Loops
2016/17
14 / 24
The for loop
Generating Fibonacci numbers cont’d
n = 20
previous , current = 0, 1
print( previous , current )
for i in range(1, n + 1):
previous , current = current, current + previous
print( current )
KH (04/10/16)
Lecture 7: For Loops
2016/17
15 / 24
Nested loops
Nesting for loops
Body of for loop may be arbitrarily complex: may include other loops, if
statements etc.
LIMIT = 10
for row in range(1, LIMIT):
<−−−−−−−−−−−−outer
for col in range(1, LIMIT):
#i
|
product = row ∗ col
#n
|
. . .
#n
|
. . .
#e
|
print(product, ” ”, end = ””) # r
|
print ()
−−−
Inner loop (re-)executed completely once for each run through outer loop
(i.e. 9 times)
Each (re-)execution runs through col = 1, 2, · · · , LIMIT − 1 again
KH (04/10/16)
Lecture 7: For Loops
2016/17
16 / 24
Nested loops
Note on print function
By default each print creates single line of output so
print(17)
prints value 17 and a newline so subsequent output appear on a
separate line
To suppress this line-terminating behaviour use
print (17, end = ””)
Subsequent output will appear on same line after the 17
To terminate a line, use
print ()
KH (04/10/16)
Lecture 7: For Loops
2016/17
17 / 24
Nested loops
Flowchart
KH (04/10/16)
Lecture 7: For Loops
2016/17
18 / 24
Nested loops
Program output
LIMIT = 10
for row in range(1, LIMIT):
for col in range(1, LIMIT):
product = row ∗ col
. . .
print (product, ” ”, end = ””)
print ()
1
2
3
4
5
6
7
8
9
2
4
6
8
10
12
14
16
18
3
6
9
12
15
18
21
24
27
4
8
12
16
20
24
28
32
36
5
10
15
20
25
30
35
40
45
6
12
18
24
30
36
42
48
54
7
14
21
28
35
42
49
56
63
8
16
24
32
40
48
56
64
72
9
18
27
36
45
54
63
72
81
Each run through the loop body (iteration) of outer loop produces one line
of output (incl. newline)
For each such iteration, the 9 iterations of the inner loop produce the nine
valies you see on that line
KH (04/10/16)
Lecture 7: For Loops
2016/17
19 / 24
Nested loops
Program output
LIMIT = 10
for row in range(1, LIMIT):
for col in range(1, LIMIT):
product = row ∗ col
if product < 10:
print (” ”, end = ””)
print (product, ” ”, end = ””)
print ()
KH (04/10/16)
Lecture 7: For Loops
2016/17
20 / 24
Nested loops
Technical note
if product < 10:
print(” ”, end = ””)
This element adds single space before one-digit numbers
This tidies up column alignment of numbers
KH (04/10/16)
Lecture 7: For Loops
2016/17
21 / 24
Nested loops
Program trace (LIMIT = 4)
Time
row
1
1
1
1
1
1
1
2
2
2
2
2
2
2
3
3
3
3
3
3
3
col
1
1
2
2
3
3
3
1
1
2
2
3
3
3
1
1
2
2
3
3
3
KH (04/10/16)
Statement
product = . .
print(product)
product = . .
print(product)
product = . .
print(product)
print()
product = . .
print(product)
product = . .
print(product)
product = . .
print(product)
print()
product = . .
print(product)
product = . .
print(product)
product = . .
print(product)
print()
Output
.
1
.
2
.
3
(newline)
.
2
.
4
.
6
(newline)
.
3
.
6
.
9
(newline)
Lecture 7: For Loops
2016/17
22 / 24
Nested loops
For loop and strings
text = \
”””
The quick brown fox jumped over the lazy dog, so he did .
”””
count = 0
for c in text :
if c. isalpha ():
count = count + 1
print(”Letter count is ”, count)
KH (04/10/16)
Lecture 7: For Loops
2016/17
23 / 24
Back Material
Notes and Acknowledgements
Reading
Code
Acknowledgements
KH (04/10/16)
Lecture 7: For Loops
2016/17
24 / 24
Related documents