LESSON NOTE/GUIDE
PROGRAM
1 – LOOPING OVER A LIST (REVIEW)
- The
following code is review from the previous lesson. It is simply to remind students that
we can loop over a list.
THE CODE
numbers = [4,5,1,2,6]
for x in numbers:
print(x)
|
EXPLANATION
If you need an explanation of the
above code, please refer to the previous lesson.
|
PROGRAM
2 – PROCESS LIST CONTENTS - TOTAL
- Create a
list that contains numbers. Add
up all the numbers and output their sum to the screen using a for loop.
THE CODE
numbers = [4,5,1,2,6]
sum = 0
for x in numbers:
sum = sum + x
print(sum)
|
EXPLANATION
The first line creates a list of
numbers.
The second line of code creates a
variable that will hold the sum of the numbers. Initially, it is set to zero as we
haven’t started adding yet.
The third line creates a loop. It will loop over each element in the
list – so it will loop five times.
During the first pass in the loop,
x is 4 and sum is still 0. To
execute the statement sum =
sum + x, Python will execute the right side first and then
store that value in sum. So, 0 + 4
is 4. Therefore, sum gets the value
4.
In the second pass, sum is now 4
and x is now 5. We add them up to 9
and that gets stored in sum.
In the third pass, sum is now 9 and
x is now 1. Adding them up, we get
10. Sum now gets the value 10.
In the fourth pass, sum will get 10
+ 2 = 12. And in the fifth pass, sum
will get 12 + 6 = 18.
After the loop, the print statement
is executed and the value 18 is displayed.
|
PROGRAM
3 – PROCESS LIST CONTENTS – COUNT CHARACTERS
- Create a
list that contains words. Use a
for loop to count the total number of characters in the list.
THE CODE
words = ["hockey", "pumpkin",
"desk", "stratosphere"]
totalCharacters = 0
for w in words:
wordLength = len(w)
totalCharacters = totalCharacters
+ wordLength
print(totalCharacters)
|
EXPLANATION
The first line creates a list of
four words.
The second line creates a variable
called totalCharacters in which we will store the
number of characters that we count.
We haven’t started counting yet so we set it to zero.
The third line creates a loop. We will loop for each element in the list
– so we will loop 4 times.
In the first pass in the loop, w is
equal to “hockey”. The variable wordLength is set to 6 because that is the length of
“hockey”. Then, we add 6 to totalCharacters, giving it a value of 6.
In the next pass, w is
“pumpkin”. The variable wordLength gets a value of 7 this time. And then totalLength,
which already had a value of 6, gets the value 7 added to it. So totalLength
is now 13.
In the next two passes in the loop,
we continue added the length of last two words to totalCharacters.
Once the loop in completed, the
print statement is executed outputting the total number of characters in
all words.
|
PROGRAM
4 – PROCESS LIST CONTENTS – MAX VALUE
- Create a
list that contains numbers. Use a
for loop to go over the entire list and find which value is the maximum
value in the list.
THE CODE
numberlist = [4, 3, 1, 8, 2]
max = numberlist[0]
for val in numberlist:
if
val > max:
max = val
print(max)
|
EXPLANATION
The first line creates a list of five
numbers.
The second line creates a variable
called max that is initially set to the first value in the list. This variable will be set to any other
larger value found in the list.
The next creates a for loop that
loops over each element of the list.
Inside the loop, we compare the
current value in the list (val) with max. If val is
larger, then that is the new maximum value in the list (that we have found
so far).
After looping through the entire
list, then max will hold the maximum value in the list.
After the loop, we print max in the
final line.
NOTE
It is also possible to find the max
value of a list my using the max method.
That would be much easier but wouldn’t test a student’s for loop
knowledge. J
|
PROGRAM
5 – PHONE NUMBERS (TIME PERMITING)
- Use nested
for loops to output every single possible phone number ABC-DEFG for a
single area code.
- Note that A
cannot be 0 or 1.
- Note that B
and C cannot both be 1s.
THE CODE
Click here.
|
EXPLANATION
Coming soon.
|
|