1 of 30

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

data6.org/su23/syllabus/#acknowledgements-

2 of 30

Week 4

Announcements!

  • Homework 3 has been released and will be due on 7/27 @ 11 PM
  • Homework 2 grades have been released on Gradescope. Regrade requests are due by 7/27 @ 11 PM
  • Remember that you can get 2% extra credit if you have a mid-semester check-in with a course staff member
    • See Ed for more details
  • Lab 6 must be submitted to Gradescope

2

3 of 30

3

tinyurl.com/d6summer23

4 of 30

Today’s Roadmap

Lecture 18, Data 6 Summer 2023

  1. Motivation
  2. For Loops
  3. Ranges

4

5 of 30

Motivation

5

1. Motivation

2. For Loops

3. Ranges

6 of 30

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’)

7 of 30

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.

8 of 30

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?

9 of 30

For Loops

9

1. Motivation

2. For Loops

3. Ranges

10 of 30

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

11 of 30

Iteration vs. Loops

11

12 of 30

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.

13 of 30

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.

  • In the first iteration, n is 2.
  • In the second iteration, n is 4.
  • In the third iteration, n is 6.
  • In the fourth iteration, n is 8.

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.

14 of 30

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.

15 of 30

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

16 of 30

Solution

16

If the fare is greater than the threshold, increment the count

Start the count at 0.

17 of 30

Questions?

17

18 of 30

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

19 of 30

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.

20 of 30

Ranges and For Loops

20

1. Motivation

2. For Loops

3. Ranges

21 of 30

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.

22 of 30

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.

23 of 30

More Array Range Examples

23

24 of 30

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.

25 of 30

Questions?

25

26 of 30

Quick Check 2

Fill in the two blanks in np.arange() so that the following code works as displayed.

26

Quick Check

27 of 30

In Conclusion…

27

28 of 30

Summary

For loops perform one iteration for each element of a sequence.

  • This is very useful when working with arrays
  • If we don’t have an array we’re working with, we can use np.arange() to make a range to iterate over
  • We can use np.append() to add onto the end of an array

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>

29 of 30

Recap

Next Time

29

  • Iteration
    • For Loops
  • Iteration
    • While Loops
    • Dealing with Missing Values

30 of 30

Week 4

Announcements!

  • Homework 3 has been released and will be due on 7/27 @ 11 PM
  • Homework 2 grades have been released on Gradescope. Regrade requests are due by 7/27 @ 11 PM
  • Remember that you can get 2% extra credit if you have a mid-semester check-in with a course staff member
    • See Ed for more details

30