Python Functions
Main.PythonFunctions History
Hide minor edits - Show changes to markup
(:html:)
<div id="disqus_thread"></div> <script type="text/javascript"> /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */ var disqus_shortname = 'apmonitor'; // required: replace example with your forum shortname /* * * DON'T EDIT BELOW THIS LINE * * */ (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = 'https://' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })(); </script> <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript> <a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
(:htmlend:)
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))
(:source lang=python:) 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)) (:sourceend:)
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
(:source lang=python:) 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
(:sourceend:)
The following tutorial is an introduction to Python functions such as average, standard deviation, maximum, minimum, and conditional counting.
The following tutorial is an introduction to built-in Python functions such as average, standard deviation, maximum, minimum, and conditional counting.
Non-Ideal Gas Function Example
(:html:) <iframe width="560" height="315" src="https://www.youtube.com/embed/oX-kpj3fAZQ" frameborder="0" allowfullscreen></iframe> (:htmlend:)
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
(:title MATLAB Statistical Functions:) (:keywords big data, data analysis, Mathworks, MATLAB, statistics, average, min, max, stdev, university course:) (:description Introduction to statistical functions in MATLAB, implemented to analyze a data set:)
The following tutorial is an introduction to MATLAB functions such as average, standard deviation, maximum, minimum, and conditional counting.
(:title Python Functions:) (:keywords big data, data analysis, Python, iPython notebook, spreadsheet, average, min, max, stdev, university course:) (:description Introduction to statistical functions in Python, implemented to analyze a data set:)
The following tutorial is an introduction to Python functions such as average, standard deviation, maximum, minimum, and conditional counting.
<iframe width="560" height="315" src="https://www.youtube.com/embed/gHWkS2CyScs" frameborder="0" allowfullscreen></iframe>
<iframe width="560" height="315" src="https://www.youtube.com/embed/ofWU-kNrWMk" frameborder="0" allowfullscreen></iframe>
MATLAB Source Code
% clear session and screen clear all; clc % create a new column vector with 10 elements % with random numbers between 0 and 100 y = rand(10,1) * 100; disp(y) % basic statistics mean(y) max(y) min(y) std(y) % define a new anonymous function avg = @(x) sum(x)/length(x) % check new function disp('Average with avg function: ') avg(y) disp('Average with mean function: ') mean(y) % number of values below 50 % boolean vector of 0 or 1 values disp(y<50) % display result disp('Number of values below 50: ') z = y<50; disp(sum(z))
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 Python 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.
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.
% clear session and screen clear all; clc % create a new column vector with 10 elements % with random numbers between 0 and 100 y = rand(10,1) * 100; disp(y) % basic statistics mean(y) max(y) min(y) std(y) % define a new anonymous function avg = @(x) sum(x)/length(x) % check new function disp('Average with avg function: ') avg(y) disp('Average with mean function: ') mean(y) % number of values below 50 % boolean vector of 0 or 1 values disp(y<50) % display result disp('Number of values below 50: ') z = y<50; disp(sum(z))
(:title Python Functions:) (:keywords big data, data analysis, Python, iPython notebook, spreadsheet, average, min, max, stdev, university course:) (:description Introduction to statistical functions in Python, implemented to analyze a data set:)
The following tutorial is an introduction to Python functions such as average, standard deviation, maximum, minimum, and conditional counting.
(:title MATLAB Statistical Functions:) (:keywords big data, data analysis, Mathworks, MATLAB, statistics, average, min, max, stdev, university course:) (:description Introduction to statistical functions in MATLAB, implemented to analyze a data set:)
The following tutorial is an introduction to MATLAB functions such as average, standard deviation, maximum, minimum, and conditional counting.
<iframe width="560" height="315" src="https://www.youtube.com/embed/ofWU-kNrWMk" frameborder="0" allowfullscreen></iframe>
<iframe width="560" height="315" src="https://www.youtube.com/embed/gHWkS2CyScs" frameborder="0" allowfullscreen></iframe>
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))) icount = sum(1 if i<50.0 else 0 for i in y) print("Numbers Below 50: " + str(icount)) # another method with NumPy import numpy as np print(np.mean(y)) print(np.average(y)) print(np.std(y)) print(np.median(y))
MATLAB Source Code
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.
Data statistics can also be completed with a spreadsheet program like Microsoft Excel and Python 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.
print("Number Below 50: " + str(sum(1 if i<50.0 else 0 for i in y)))
icount = sum(1 if i<50.0 else 0 for i in y) print("Numbers Below 50: " + str(icount))
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))) print("Number Below 50: " + str(sum(1 if i<50.0 else 0 for i in y))) # another method with NumPy import numpy as np print(np.mean(y)) print(np.average(y)) print(np.std(y)) print(np.median(y))
Additional Tutorials
Data statistics can also be completed with a spreadsheet program like Microsoft Excel and Python 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.
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.
(:title Python Functions:) (:keywords big data, data analysis, Python, iPython notebook, spreadsheet, average, min, max, stdev, university course:) (:description Introduction to statistical functions in Python, implemented to analyze a data set:)
The following tutorial is an introduction to Python functions such as average, standard deviation, maximum, minimum, and conditional counting.
(:html:) <iframe width="560" height="315" src="https://www.youtube.com/embed/ofWU-kNrWMk" frameborder="0" allowfullscreen></iframe> (:htmlend:)
Data statistics can also be completed with a spreadsheet program like Microsoft Excel and Python 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.
(:html:)
<div id="disqus_thread"></div> <script type="text/javascript"> /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */ var disqus_shortname = 'apmonitor'; // required: replace example with your forum shortname /* * * DON'T EDIT BELOW THIS LINE * * */ (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = 'https://' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })(); </script> <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript> <a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
(:htmlend:)