pnw_f_temps = [111, 115, 108, 93, 93, 97, 95, 97, 99, 91, 91, 93, 97, 95]
print("There are " + str(len(pnw_f_temps)) + " entries in this dataset.")
print("The temperature on June 28th was " + str(pnw_f_temps[0]) + " degrees Fahrenheit.")
print("The temperature on June 29th was " + str(pnw_f_temps[1]) + " degrees Fahrenheit.")
There are 14 entries in this dataset. The temperature on June 28th was 111 degrees Fahrenheit. The temperature on June 29th was 115 degrees Fahrenheit.
def f_to_c(temp):
return (temp - 32) * (5/9)
pnw_c_temps = []
# What happens if you tried to convert the entire list?
# f_to_c(pnw_f_temps)
pnw_c_temps.append(f_to_c(pnw_f_temps[0]))
pnw_c_temps.append(f_to_c(pnw_f_temps[1]))
pnw_c_temps.append(f_to_c(pnw_f_temps[2]))
pnw_c_temps
# There has to be an easier way!
[43.88888888888889, 46.111111111111114, 42.22222222222222]
pnw_c_temps = []
for day in pnw_f_temps:
pnw_c_temps.append(f_to_c(day))
pnw_c_temps
[43.88888888888889, 46.111111111111114, 42.22222222222222, 33.88888888888889, 33.88888888888889, 36.111111111111114, 35.0, 36.111111111111114, 37.22222222222222, 32.77777777777778, 32.77777777777778, 33.88888888888889, 36.111111111111114, 35.0]
for n in [2, 4, 6, 8]:
print(n * 5)
10 20 30 40
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!
def modify_verb(verbs):
for verb in verbs:
print(verb + 'ing')
modify_verb(['eat', 'run'])
eating runing
modify_verb(['cry', 'drink', 'sleep'])
crying drinking sleeping
# 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
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)
Let's implement count_above
using a for loop, to show how much easier it is. We'll call this function count_above_for
.
def count_above_for(fares, threshold):
count = 0
for fare in fares:
if fare > threshold:
count += 1
return count
# Evaluates to 4, since 4 nums are >3
count_above_for([1, 2, 5, 8, 7, 9], 3)
4
# Evaluates to 0, since 0 nums are >10
count_above_for([4, 8, 2, 1], 10)
0
def not_sure(word):
output = ''
for letter in word:
if letter in 'aeiou':
output += letter
return output
#not_sure("sequoia")
for char in 'university':
print(char.upper())
U N I V E R S I T Y
for j in range(10):
print(j)
0 1 2 3 4 5 6 7 8 9
range(10)
range(0, 10)
list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list(range(3, 8))
[3, 4, 5, 6, 7]
# 1 + 2 + 3 + 4 + 5
# + 6 + 7 + 8 + 9 + 10
total = 0
for n in range(1, 11):
total += n
total
55
# 3 + 5 + 7 + 9
total = 0
for n in range(3, 11, 2):
total += n
total
24
for j in range(10, 0, -3):
print(j)
print('happy new year!')
10 7 4 1 happy new year!
# Remember, the + symbol
# concatenates two lists
[1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
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
list_add([1, 2, 3], [4, 5, 6])
[5, 7, 9]
school = 'The University of California'
for p in range(__, 18, __):
print(school[p])
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_98/2432743175.py in <module> 1 school = 'The University of California' ----> 2 for p in range(__, 18, __): 3 print(school[p]) TypeError: 'list' object cannot be interpreted as an integer