# Lecture 11 – Iteration, Part 2¶

## For loops¶

In [1]:
for n in [2, 4, 6, 8]:
print(n * 5)

10
20
30
40

In [2]:
tips = [5, 2, 3, 8, 9, 0, 2, 1]
for i in tips:
print('ignorning i!')

ignorning i!
ignorning i!
ignorning i!
ignorning i!
ignorning i!
ignorning i!
ignorning i!
ignorning i!

In [3]:
def modify_verb(verbs):
for verb in verbs:
print(verb + 'ing')

In [4]:
modify_verb(['eat', 'run'])

eating
runing

In [5]:
modify_verb(['cry', 'drink', 'sleep'])

crying
drinking
sleeping


### Example: fares¶

In [6]:
# Ignore this code, just run it
from datascience import *
table = Table.read_table('data/titanic.csv').select(['Name', 'Age', 'Sex', 'Fare', 'Survived'])
titanic_fares = list(table.column('Fare'))[:10]
table

Out[6]:
Name Age Sex Fare Survived
Braund, Mr. Owen Harris 22 male 7.25 0
Cumings, Mrs. John Bradley (Florence Briggs Thayer) 38 female 71.2833 1
Heikkinen, Miss. Laina 26 female 7.925 1
Futrelle, Mrs. Jacques Heath (Lily May Peel) 35 female 53.1 1
Allen, Mr. William Henry 35 male 8.05 0
Moran, Mr. James nan male 8.4583 0
McCarthy, Mr. Timothy J 54 male 51.8625 0
Palsson, Master. Gosta Leonard 2 male 21.075 0
Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg) 27 female 11.1333 1
Nasser, Mrs. Nicholas (Adele Achem) 14 female 30.0708 1

... (881 rows omitted)

First let's implement count_above using a while loop. We'll call this function count_above_while.

In [7]:
def count_above_while(fares, threshold):
count = 0
i = 0
while i < len(fares):
if fares[i] > threshold:
count += 1
i += 1
return count

In [8]:
# Evaluates to 4, since 4 nums are >3
count_above_while([1, 2, 5, 8, 7, 9], 3)

Out[8]:
4
In [9]:
# Evaluates to 0, since 0 nums are >10
count_above_while([4, 8, 2, 1], 10)

Out[9]:
0

Let's now implement count_above using a for loop, to show how much easier it is. We'll call this function count_above_for.

In [10]:
def count_above_for(fares, threshold):
count = 0
for fare in fares:
if fare > threshold:
count += 1
return count

In [11]:
# Evaluates to 4, since 4 nums are >3
count_above_for([1, 2, 5, 8, 7, 9], 3)

Out[11]:
4
In [12]:
# Evaluates to 0, since 0 nums are >10
count_above_for([4, 8, 2, 1], 10)

Out[12]:
0

### Quick Check 1¶

In [ ]:



### String example¶

In [13]:
for char in 'university':
print(char.upper())

U
N
I
V
E
R
S
I
T
Y


## Range¶

In [14]:
for j in range(10):
print(j)

0
1
2
3
4
5
6
7
8
9

In [15]:
range(10)

Out[15]:
range(0, 10)
In [16]:
list(range(10))

Out[16]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [17]:
list(range(3, 8))

Out[17]:
[3, 4, 5, 6, 7]
In [18]:
# 1 + 2 + 3 + 4 + 5
# + 6 + 7 + 8 + 9 + 10
total = 0
for n in range(1, 11):
total += n

total

Out[18]:
55
In [19]:
# 3 + 5 + 7 + 9
total = 0
for n in range(3, 11, 2):
total += n

total

Out[19]:
24
In [20]:
for j in range(10, 0, -3):
print(j)
print('happy new year!')

10
7
4
1
happy new year!


In [21]:
# Remember, the + symbol
# concatenates two lists
[1, 2, 3] + [4, 5, 6]

Out[21]:
[1, 2, 3, 4, 5, 6]
In [22]:
def list_add(a, b):
# Checking to make sure that
# a and b have the same length
if len(a) != len(b):
return None

output = []
for i in range(len(a)):
output.append(a[i] + b[i])
return output

In [23]:
list_add([1, 2, 3], [4, 5, 6])

Out[23]:
[5, 7, 9]

### Quick Check 2¶

In [ ]:
school = 'The University of California'
for p in range(__, 18, __):
print(school[p])


### While vs. for¶

In [25]:
def sum_squares_for(values):
total = 0
for v in values:
total += v**2

# 3^2 + 4^2 + 5^2
sum_squares_for([3, 4, 5])

Out[25]:
50
In [26]:
def sum_squares_while(values):
total = 0
j = 0
while j < len(values):
total += values[j]**2
j += 1

50