Iteration
1
Data 6 Summer 2023
LECTURE 18
Doing things over and over and over again.
Developed by students and faculty at UC Berkeley and Tuskegee University
Week 4
Announcements!
2
3
tinyurl.com/d6summer23
Today’s Roadmap
Lecture 18, Data 6 Summer 2023
4
Motivation
5
1. Motivation
2. For Loops
3. Ranges
➤
Repeating Code
What do we do when we want to run the same line(s) of code multiple times?
For loops allow us to repeat code!
6
for i in np.arange(4):
print(‘hello’)
New (Better) Way
Old Way
print(‘hello’)
print(‘hello’)
print(‘hello’)
print(‘hello’)
Wonderful Berkeley Weather
We have Berkeley high temperatures for the past two weeks (7/11-7/24) in the berk_temps array. In Berkeley, it is considered ‘hot’ if the temperature is >= 70 degrees, ‘cold’ if the temperature is <= 65 degrees, and ‘just right’ otherwise.
7
How can we check the temperature for all 14 days?
This is tedious.
Wonderful Berkeley Weather
We have Berkeley high temperatures for the past two weeks (7/11-7/24) in the berk_temps array. In Berkeley, it is considered ‘hot’ if the temperature is >= 70 degrees, ‘cold’ if the temperature is <= 65 degrees, and ‘just right’ otherwise.
8
Much better.
How can we check the temperature for all 14 days?
For Loops
9
1. Motivation
2. For Loops
3. Ranges
➤
Berkeley Temperatures
Iteration
To “iterate” over a sequence of values means to do something with each value, one at a time.
10
[ 72,
72,
70,
70,
66,
73,
70,
72,
66,
68,
66,
70,
66,
70 ]
First work with this number
Then work with this number
…
Iteration vs. Loops
11
Two Types of Loops
Loops allow us to repeat the execution of code.
There are two types of loops in Python: the while loop and the for loop.
“While this condition is true, repeat this code.”
“For each element of this sequence, repeat this code.”
12
while <boolean expression>:
<while body>
for <elem> in <sequence>:
<for body>
Sequences include arrays, lists, strings and more.
For Loops
The simplest version of a for loop is also referred to as a “for each” loop.
The name after “for” (in this case n) is assigned to each value in the sequence, one at a time.
13
No need to update the value of n
Python also does this for you.
No need to use “n = …”
Python does this for you.
More For Loops
14
The body of a for loop doesn’t have to involve the loop name (i) – but it often does.
The loop name can be anything you want it to be: n, i, verb, etc.
Example: Fares
Suppose we have a data set of Titanic survivors, with information about the fare (ticket price) they paid.
Write a function that, given a list of fares and a threshold, returns the number of fares that are above the threshold.
# Evaluates to 4, since 4 nums are >3
count_above([1, 2, 5, 8, 7, 9], 3)
# Evaluates to 0, since 0 nums are >10
count_above([4, 8, 2, 1], 10)
15
Solution
16
If the fare is greater than the threshold, increment the count
Start the count at 0.
Questions?
17
Quick Check 1
Describe, in one sentence, what the function not_sure does.
def not_sure(word):
output = ''
for letter in word:
if letter in 'aeiou':
output += letter
return output
18
Quick Check
For Loops with Strings
19
Just like we can iterate through each element of an array, we can also iterate through each character of a string.
Ranges and For Loops
20
1. Motivation
2. For Loops
3. Ranges
➤
Recall: np.arange()
The NumPy function np.arange() creates a sequence of numbers.
21
Array ranges work like indexing:�inclusive of the starting position, and exclusive of the ending position.
Format | Returns |
np.arange(n) | An array of all integers from 0 to n-1. |
np.arange(start, stop) | An array of all integers from start to stop-1. |
np.arange(start, stop, step) | An array of all integers from start to stop-1, counting by step. |
Ranges and For Loops
If don’t have an array we’re working with but still want to repeat code a certain number of times, what can we do?
22
Solution: Use array ranges!
Recall:
So this works exactly as we expect.
More Array Range Examples
23
Another Useful Tool: np.append()
Printing from for loops is cool, but the real power is when we use for loops to change or add values in arrays.
We can use np.append(arr, value) to add value onto the end of the array arr.
24
Empty array
Make sure to re-assign introductions.
Questions?
25
Quick Check 2
Fill in the two blanks in np.arange() so that the following code works as displayed.
26
Quick Check
In Conclusion…
27
Summary
For loops perform one iteration for each element of a sequence.
“While this condition is true, repeat this code.”
“For each element of this sequence, repeat this code.”
28
while <boolean expression>:
<while body>
for <elem> in <sequence>:
<for body>
Recap
Next Time
29
Week 4
Announcements!
30