Lecture
9
Iterations
1. The 'while' statement
Computers are
often used to automate repetitive tasks. Repeating identical or similar
tasks without making errors is something that computers do well and
people do poorly
Here
is what countdown looks
like with a while statement:
2.
Tables
One
of the things loops are good for is generating tabular data.
We can test if
'math.log()' is calculating the natural log: we put 'math.e' there as
the natural log constant, and we got all the '1's in the results.
Don't forget to
import the 'math' package if you did do this before.
Note that, 'math.e' is the constant, 'math.exp()' is the function which allows you to choose the power.
If you want to
find the log for base 2:
Or an
alternative way:
Since we know:
So the
alternative way to get log base 2 is:
This is verified
when x=2, the result is 1.
3.
The 'for
loop'
Using
an index to traverse a set of values is so common that Python provides
an alternative, simpler syntax - the for
loop:
Traverse a range:
One more
example:
To print out all
the letters in 'fruit' one by one, you can use a 'while loop':
Or a 'for loop':
The 'for loop'
in Python has the index running on the back-end.
Multiple indices:
Copy the following data:
students
= [ ("John", ["CompSci", "Physics"]), ("Vusi", ["Maths", "CompSci",
"Stats"]), ("Jess", ["CompSci", "Accounting", "Economics",
"Management"]), ("Sarah", ["InfSys", "Accounting", "Economics",
"CommLaw"]), ("Zuki", ["Sociology", "Economics", "Law", "Stats",
"Music"])]
4.
Looping and
counting
The
following program counts the
number of times the letter 'a'
appears in a string:
5. Break and Continue
'Break' will quit the loop.
Istead of quiting the loop, Continue will skip it and continue traversing other elements. :
6. Exercise:
Loop
through and print out all even numbers from the numbers list in the
same order they are received. Don't print any numbers that come after
237 in the sequence.
numbers = [
951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544,
615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941,
386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345,
399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217,
815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717,
958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470,
743, 527
]
Solution:
Tasks:
1. The value of
pi can be approximated by:
Wriet a script (must use the 'while loop')
with a user-defined function which asks the user to input the number of
iterations (n), then computes the expression. The larger the number of
iterations, the closer the result should be to pi. Print out the final
result by calling the function. (You may want to force the input into
an 'int' varable)
2. Repeat the
task above using the
'for loop' instead of the 'while loop'.
3. Given that the square root of a number 'n' can be calculated by the following equation:
Better = (approx + n/approx)/2
"Better' means a
better/closer result to the real square root. 'approx' means the
previous approximated value, maybe from the last iteration or from the
initial assigned value.
Design
a function that uses this method to calculate the square root of a
given number. The relative error, abs((approx-better)/approx),should be smaller than 0.001.
Make
sure it'll also return the number of iterations to reach this accuracy.
(Do not use math.sqrt() or any other embedded sqrt() functions)
(Hint, you can start approx with n/2 or something like that)
4. As the last example in the 'For Loop' section, we have the same student information data set:
students
= [ ("John", ["CompSci", "Physics"]), ("Vusi", ["Maths", "CompSci",
"Stats"]), ("Jess", ["CompSci", "Accounting", "Economics",
"Management"]), ("Sarah", ["InfSys", "Accounting", "Economics",
"CommLaw"]), ("Zuki", ["Sociology", "Economics", "Law", "Stats",
"Music"])]
How
many students are taking CompSci? This needs a counter, and for each
student we need a second loop that tests each of the subjects in turn.