## Python Functions

The following tutorial is an introduction to built-in Python functions such as average, standard deviation, maximum, minimum, and conditional counting.

#### Python Source Code

import random
y = [random.random()*100.0 for i in range(10)]
print("Print y")
print(y)

print("Sorted List")
for i in range(len(y)):
print("%.2f" % y[i])

def avg(x):
return sum(x) / len(x)

print("Avg: " + str(avg(y)))
print("Max: " + str(max(y)))
print("Min: " + str(min(y)))
z = sum(1 if i<50.0 else 0 for i in y)
print("Number Below 50: " + str(z))

# another method with NumPy
import numpy as np
print(np.mean(y))
print(np.average(y))
print(np.std(y))
print(np.median(y))

Data statistics can also be completed with a spreadsheet program like Microsoft Excel and MATLAB where the data sets are arrays or matrices instead of tables in a spreadsheet. Click on the appropriate link for additional information and source code.

#### Source Code

def P_RK_IG(V, T, do_ideal_gas=False):
R = 0.0821  # L-atm/K
Pc = 37.2   # atm
Tc = 132.5  # K

a = 0.427 * pow(R,2) * pow(Tc,2.5) / Pc
b = 0.0866 * R * Tc / Pc

# Compute in atm
P_ig = R * T / V
P_rk = R * T / (V-b) - a/(V*(V+b)*pow(T,0.5))

# Convert to Pascals
if do_ideal_gas:
return P_ig * 101325
else:
return P_rk * 101325

for T in range(490,511,10):
V = 4.0
while V < 8:
print("----- Temperature: " + str(T) + " K")
print("P_ig: " + str(P_RK_IG(V,T,True)) + " Pa")
print("P_rk: " + str(P_RK_IG(V,T)) + " Pa")
V = V + 2.0