Control flow: for loops, while loops, list comprehensions, and if statements
temperatures = [68.5,72.1,74.8,65.3,62.7,58.9]
print(temperatures[0])
print(temperatures[1])
print(temperatures[2])
print(temperatures[3])
print(temperatures[4])
print(temperatures[5])
68.5 72.1 74.8 65.3 62.7 58.9
# Option 1
for item in [4,3,2,1]:
print(item)
4 3 2 1
# Option 2
countdown = [4,3,2,1]
for item in countdown:
print(item)
4 3 2 1
# Option 1
for value in ('pH','puget_sound',7.8):
print(value)
pH puget_sound 7.8
# Option 2
pH_data = ('pH','puget_sound',7.8)
for value in pH_data:
print(value)
pH puget_sound 7.8
# Option 1
for character in 'hello':
print(character)
h e l l o
# Option 2
hello_string = 'hello'
for character in hello_string:
print(character)
h e l l o
# Option 1
for index in range(0,7,2):
print(index)
0 2 4 6
# Option 2
even_numbers = range(0,7,2)
for index in even_numbers:
print(index)
0 2 4 6
# Option 1
numbers = [5,6,7,8]
sum = 0
for value in numbers:
sum = sum + value
print('The sum is',sum)
The sum is 26
# Option 2
numbers = [5,6,7,8]
sum = 0
for value in numbers:
sum += value
print('The sum is',sum)
The sum is 26
# Option 3
numbers = [5,6,7,8]
sum = 0
for index in range(len(numbers)):
sum += numbers[index]
print('The sum is',sum)
The sum is 26
params = ['Temperature','Salinity','Oxygen']
units = ['°C','PSU','µmol/kg']
currents_mix = [[4.4,4.8,4.5], # temp (°C)
[34.5,33.9,33.8], # salinity (PSU)
[230,250,260]] # oxygen (µmol/kg)
n_params = len(currents_mix) # 3 parameters
n_currents = len(currents_mix[0]) # 3 currents
for param_idx in range(n_params):
sum = 0.0
for current_idx in range(n_currents):
sum += currents_mix[param_idx][current_idx]
average_val = sum / n_currents
print(params[param_idx] + ' (' + units[param_idx] + '):',average_val)
Temperature (°C): 4.566666666666666 Salinity (PSU): 34.06666666666667 Oxygen (µmol/kg): 246.66666666666666
x = [1,2,3,4]
y = ['A','B','C','D']
zip(x,y) # Notice that you cannot index into this using [] .
# In other words, zip() is not useful unless you iterate over it.
<zip at 0x7f55b9813548>
params = ['Temperature','Salinity','Oxygen']
units = ['°C','PSU','µmol/kg']
for param, unit in zip(params,units):
print(param,'has units of',unit)
Temperature has units of °C Salinity has units of PSU Oxygen has units of µmol/kg
x = ['A','B','C','D','E']
enumerate(x)
<enumerate at 0x7f55b9812e10>
abbrevs = ['POC','DOC','DIC']
names = ['particulate organic carbon',
'dissolved organic carbon',
'dissolved inorganic carbon']
for index, abbrev in enumerate(abbrevs):
print(abbrev,'stands for',names[index])
POC stands for particulate organic carbon DOC stands for dissolved organic carbon DIC stands for dissolved inorganic carbon
base = 2
exponent = 0
result = base**exponent
while result < 1000:
print(result)
exponent += 1
result = base**exponent
1 2 4 8 16 32 64 128 256 512
# Option 1: using a for loop
squares = []
for num in range(10):
squares.append(num * num)
print(squares)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# Option 2: using a list comprehension:
squares = [num * num for num in range(10)]
print(squares)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
x = 5
if x > 0:
print('x is positive!')
x is positive!
x = -3
if x > 0:
print('x is positive!') # This won't get printed.
x = 5
if x > 6:
print('hi')
elif x >= 5:
print('hello')
elif x >= 4:
print('oops')
hello
likelihood_precip = 80 # i.e. 80% chance of rain
if likelihood_precip > 50:
print('Ugh... I better wear a rain jacket.')
elif likelihood_precip < 20:
print("I'll be okay without a rain jacket.")
else:
print("I don't know what to do.")
Ugh... I better wear a rain jacket.
likelihood_precip = 5 # i.e. 5% chance of rain
if likelihood_precip > 50:
print('Ugh... I better wear a rain jacket.')
elif likelihood_precip < 20:
print("I'll be okay without a rain jacket.")
else:
print("I don't know what to do.")
I'll be okay without a rain jacket.
likelihood_precip = 30 # i.e. 30% chance of rain
if likelihood_precip > 50:
print('Ugh... I better wear a rain jacket.')
elif likelihood_precip < 20:
print("I'll be okay without a rain jacket.")
else:
print("I don't know what to do.")
I don't know what to do.